From e34364412b783710720738a9b53eac4034cc7126 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 2 Nov 2021 16:05:54 +0000 Subject: [PATCH] Replace InvalidStateException usages with InvalidArgument or LogicException --- src/Server.php | 2 +- src/block/Block.php | 2 +- src/crafting/CraftingGrid.php | 2 +- src/entity/Entity.php | 2 +- src/event/HandlerList.php | 2 +- src/event/entity/EntityEffectRemoveEvent.php | 2 +- src/network/mcpe/compression/CompressBatchPromise.php | 6 +++--- src/permission/PermissibleInternal.php | 2 +- src/player/Player.php | 4 ++-- src/scheduler/AsyncWorker.php | 6 +++--- src/scheduler/TaskScheduler.php | 5 +---- src/utils/Config.php | 5 ++--- src/utils/PromiseResolver.php | 4 ++-- src/utils/Terminal.php | 2 +- src/world/World.php | 8 +++----- 15 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/Server.php b/src/Server.php index 277a56ad6..afe240446 100644 --- a/src/Server.php +++ b/src/Server.php @@ -739,7 +739,7 @@ class Server{ public function __construct(\DynamicClassLoader $autoloader, \AttachableThreadedLogger $logger, string $dataPath, string $pluginPath){ if(self::$instance !== null){ - throw new \InvalidStateException("Only one server instance can exist at once"); + throw new \LogicException("Only one server instance can exist at once"); } self::$instance = $this; $this->startTime = microtime(true); diff --git a/src/block/Block.php b/src/block/Block.php index 2de12ce58..414d9b649 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -493,7 +493,7 @@ class Block{ return $this->position->getWorld()->getBlock($this->position->getSide($side, $step)); } - throw new \InvalidStateException("Block does not have a valid world"); + throw new \LogicException("Block does not have a valid world"); } /** diff --git a/src/crafting/CraftingGrid.php b/src/crafting/CraftingGrid.php index 5c84c9b47..a8d4f6ebe 100644 --- a/src/crafting/CraftingGrid.php +++ b/src/crafting/CraftingGrid.php @@ -111,7 +111,7 @@ class CraftingGrid extends SimpleInventory{ return $this->getItem(($y + $this->startY) * $this->gridWidth + ($x + $this->startX)); } - throw new \InvalidStateException("No ingredients found in grid"); + throw new \LogicException("No ingredients found in grid"); } /** diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b4cc59c17..e483b4348 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -980,7 +980,7 @@ abstract class Entity{ final public function scheduleUpdate() : void{ if($this->closed){ - throw new \InvalidStateException("Cannot schedule update on garbage entity " . get_class($this)); + throw new \LogicException("Cannot schedule update on garbage entity " . get_class($this)); } $this->getWorld()->updateEntities[$this->id] = $this; } diff --git a/src/event/HandlerList.php b/src/event/HandlerList.php index b452a382c..b135a53cd 100644 --- a/src/event/HandlerList.php +++ b/src/event/HandlerList.php @@ -47,7 +47,7 @@ class HandlerList{ */ public function register(RegisteredListener $listener) : void{ if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){ - throw new \InvalidStateException("This listener is already registered to priority {$listener->getPriority()} of event {$this->class}"); + throw new \InvalidArgumentException("This listener is already registered to priority {$listener->getPriority()} of event {$this->class}"); } $this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener; } diff --git a/src/event/entity/EntityEffectRemoveEvent.php b/src/event/entity/EntityEffectRemoveEvent.php index afd2ef631..35e59023c 100644 --- a/src/event/entity/EntityEffectRemoveEvent.php +++ b/src/event/entity/EntityEffectRemoveEvent.php @@ -29,7 +29,7 @@ namespace pocketmine\event\entity; class EntityEffectRemoveEvent extends EntityEffectEvent{ public function cancel() : void{ if($this->getEffect()->getDuration() <= 0){ - throw new \InvalidStateException("Removal of expired effects cannot be cancelled"); + throw new \LogicException("Removal of expired effects cannot be cancelled"); } parent::cancel(); } diff --git a/src/network/mcpe/compression/CompressBatchPromise.php b/src/network/mcpe/compression/CompressBatchPromise.php index 8fa8b12ae..c62388f6a 100644 --- a/src/network/mcpe/compression/CompressBatchPromise.php +++ b/src/network/mcpe/compression/CompressBatchPromise.php @@ -59,7 +59,7 @@ class CompressBatchPromise{ public function resolve(string $result) : void{ if(!$this->cancelled){ if($this->result !== null){ - throw new \InvalidStateException("Cannot resolve promise more than once"); + throw new \LogicException("Cannot resolve promise more than once"); } $this->result = $result; foreach($this->callbacks as $callback){ @@ -80,7 +80,7 @@ class CompressBatchPromise{ public function getResult() : string{ $this->checkCancelled(); if($this->result === null){ - throw new \InvalidStateException("Promise has not yet been resolved"); + throw new \LogicException("Promise has not yet been resolved"); } return $this->result; } @@ -95,7 +95,7 @@ class CompressBatchPromise{ public function cancel() : void{ if($this->hasResult()){ - throw new \InvalidStateException("Cannot cancel a resolved promise"); + throw new \LogicException("Cannot cancel a resolved promise"); } $this->cancelled = true; } diff --git a/src/permission/PermissibleInternal.php b/src/permission/PermissibleInternal.php index c9484085e..0d14d7943 100644 --- a/src/permission/PermissibleInternal.php +++ b/src/permission/PermissibleInternal.php @@ -146,7 +146,7 @@ class PermissibleInternal implements Permissible{ foreach($this->rootPermissions as $name => $isGranted){ $perm = $permManager->getPermission($name); if($perm === null){ - throw new \InvalidStateException("Unregistered root permission $name"); + throw new \LogicException("Unregistered root permission $name"); } $this->permissions[$name] = new PermissionAttachmentInfo($name, null, $isGranted, null); $permManager->subscribeToPermission($name, $this); diff --git a/src/player/Player.php b/src/player/Player.php index a568ba644..1ca3faee8 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -475,7 +475,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ public function getNetworkSession() : NetworkSession{ if($this->networkSession === null){ - throw new \InvalidStateException("Player is not connected"); + throw new \LogicException("Player is not connected"); } return $this->networkSession; } @@ -1941,7 +1941,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ */ public function onPostDisconnect(string $reason, Translatable|string|null $quitMessage) : void{ if($this->isConnected()){ - throw new \InvalidStateException("Player is still connected"); + throw new \LogicException("Player is still connected"); } //prevent the player receiving their own disconnect message diff --git a/src/scheduler/AsyncWorker.php b/src/scheduler/AsyncWorker.php index eedb7ea5c..3dc8c2e68 100644 --- a/src/scheduler/AsyncWorker.php +++ b/src/scheduler/AsyncWorker.php @@ -92,7 +92,7 @@ class AsyncWorker extends Worker{ */ public function saveToThreadStore(string $identifier, $value) : void{ if(\Thread::getCurrentThread() !== $this){ - throw new \InvalidStateException("Thread-local data can only be stored in the thread context"); + throw new \LogicException("Thread-local data can only be stored in the thread context"); } self::$store[$identifier] = $value; } @@ -109,7 +109,7 @@ class AsyncWorker extends Worker{ */ public function getFromThreadStore(string $identifier){ if(\Thread::getCurrentThread() !== $this){ - throw new \InvalidStateException("Thread-local data can only be fetched in the thread context"); + throw new \LogicException("Thread-local data can only be fetched in the thread context"); } return self::$store[$identifier] ?? null; } @@ -119,7 +119,7 @@ class AsyncWorker extends Worker{ */ public function removeFromThreadStore(string $identifier) : void{ if(\Thread::getCurrentThread() !== $this){ - throw new \InvalidStateException("Thread-local data can only be removed in the thread context"); + throw new \LogicException("Thread-local data can only be removed in the thread context"); } unset(self::$store[$identifier]); } diff --git a/src/scheduler/TaskScheduler.php b/src/scheduler/TaskScheduler.php index 22317573d..a1ed6a261 100644 --- a/src/scheduler/TaskScheduler.php +++ b/src/scheduler/TaskScheduler.php @@ -88,12 +88,9 @@ class TaskScheduler{ return $this->tasks->contains($task); } - /** - * @throws \InvalidStateException - */ private function addTask(Task $task, int $delay, int $period) : TaskHandler{ if(!$this->enabled){ - throw new \InvalidStateException("Tried to schedule task to disabled scheduler"); + throw new \LogicException("Tried to schedule task to disabled scheduler"); } if($delay <= 0){ diff --git a/src/utils/Config.php b/src/utils/Config.php index f65f77190..562d614f8 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -144,8 +144,7 @@ class Config{ * @param mixed[] $default * @phpstan-param array $default * - * @throws \InvalidArgumentException if config type could not be auto-detected - * @throws \InvalidStateException if config type is invalid + * @throws \InvalidArgumentException if config type is invalid or could not be auto-detected */ private function load(string $file, int $type = Config::DETECT, array $default = []) : void{ $this->file = $file; @@ -187,7 +186,7 @@ class Config{ $config = array_fill_keys(self::parseList($content), true); break; default: - throw new \InvalidStateException("Config type is unknown"); + throw new \InvalidArgumentException("Invalid config type specified"); } $this->config = is_array($config) ? $config : $default; if($this->fillDefaults($default, $this->config) > 0){ diff --git a/src/utils/PromiseResolver.php b/src/utils/PromiseResolver.php index 860d683b0..81ed05201 100644 --- a/src/utils/PromiseResolver.php +++ b/src/utils/PromiseResolver.php @@ -43,7 +43,7 @@ final class PromiseResolver{ */ public function resolve($value) : void{ if($this->shared->resolved){ - throw new \InvalidStateException("Promise has already been resolved/rejected"); + throw new \LogicException("Promise has already been resolved/rejected"); } $this->shared->resolved = true; $this->shared->result = $value; @@ -56,7 +56,7 @@ final class PromiseResolver{ public function reject() : void{ if($this->shared->resolved){ - throw new \InvalidStateException("Promise has already been resolved/rejected"); + throw new \LogicException("Promise has already been resolved/rejected"); } $this->shared->resolved = true; foreach($this->shared->onFailure as $c){ diff --git a/src/utils/Terminal.php b/src/utils/Terminal.php index 4280f94d8..73e350a2c 100644 --- a/src/utils/Terminal.php +++ b/src/utils/Terminal.php @@ -64,7 +64,7 @@ abstract class Terminal{ public static function hasFormattingCodes() : bool{ if(self::$formattingCodes === null){ - throw new \InvalidStateException("Formatting codes have not been initialized"); + throw new \LogicException("Formatting codes have not been initialized"); } return self::$formattingCodes; } diff --git a/src/world/World.php b/src/world/World.php index 246799a85..bdb8d9fbb 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -516,7 +516,7 @@ class World implements ChunkManager{ */ public function onUnload() : void{ if($this->unloaded){ - throw new \InvalidStateException("Tried to close a world which is already closed"); + throw new \LogicException("Tried to close a world which is already closed"); } foreach($this->unloadCallbacks as $callback){ @@ -770,7 +770,7 @@ class World implements ChunkManager{ */ public function doTick(int $currentTick) : void{ if($this->unloaded){ - throw new \InvalidStateException("Attempted to tick a world which has been closed"); + throw new \LogicException("Attempted to tick a world which has been closed"); } $this->timings->doTick->startTiming(); @@ -2374,7 +2374,7 @@ class World implements ChunkManager{ if(isset($this->chunks[$hash = World::chunkHash($chunkX, $chunkZ)])){ $this->chunks[$hash]->addTile($tile); }else{ - throw new \InvalidStateException("Attempted to create tile " . get_class($tile) . " in unloaded chunk $chunkX $chunkZ"); + throw new \InvalidArgumentException("Attempted to create tile " . get_class($tile) . " in unloaded chunk $chunkX $chunkZ"); } //delegate tile ticking to the corresponding block @@ -2411,8 +2411,6 @@ class World implements ChunkManager{ * returned directly. * * @return Chunk|null the requested chunk, or null on failure. - * - * @throws \InvalidStateException */ public function loadChunk(int $x, int $z) : ?Chunk{ if(isset($this->chunks[$chunkHash = World::chunkHash($x, $z)])){