Replace InvalidStateException usages with InvalidArgument or LogicException

This commit is contained in:
Dylan K. Taylor 2021-11-02 16:05:54 +00:00
parent 4eef458d29
commit e34364412b
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
15 changed files with 24 additions and 30 deletions

View File

@ -739,7 +739,7 @@ class Server{
public function __construct(\DynamicClassLoader $autoloader, \AttachableThreadedLogger $logger, string $dataPath, string $pluginPath){ public function __construct(\DynamicClassLoader $autoloader, \AttachableThreadedLogger $logger, string $dataPath, string $pluginPath){
if(self::$instance !== null){ 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; self::$instance = $this;
$this->startTime = microtime(true); $this->startTime = microtime(true);

View File

@ -493,7 +493,7 @@ class Block{
return $this->position->getWorld()->getBlock($this->position->getSide($side, $step)); 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");
} }
/** /**

View File

@ -111,7 +111,7 @@ class CraftingGrid extends SimpleInventory{
return $this->getItem(($y + $this->startY) * $this->gridWidth + ($x + $this->startX)); 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");
} }
/** /**

View File

@ -980,7 +980,7 @@ abstract class Entity{
final public function scheduleUpdate() : void{ final public function scheduleUpdate() : void{
if($this->closed){ 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; $this->getWorld()->updateEntities[$this->id] = $this;
} }

View File

@ -47,7 +47,7 @@ class HandlerList{
*/ */
public function register(RegisteredListener $listener) : void{ public function register(RegisteredListener $listener) : void{
if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){ 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; $this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener;
} }

View File

@ -29,7 +29,7 @@ namespace pocketmine\event\entity;
class EntityEffectRemoveEvent extends EntityEffectEvent{ class EntityEffectRemoveEvent extends EntityEffectEvent{
public function cancel() : void{ public function cancel() : void{
if($this->getEffect()->getDuration() <= 0){ 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(); parent::cancel();
} }

View File

@ -59,7 +59,7 @@ class CompressBatchPromise{
public function resolve(string $result) : void{ public function resolve(string $result) : void{
if(!$this->cancelled){ if(!$this->cancelled){
if($this->result !== null){ 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; $this->result = $result;
foreach($this->callbacks as $callback){ foreach($this->callbacks as $callback){
@ -80,7 +80,7 @@ class CompressBatchPromise{
public function getResult() : string{ public function getResult() : string{
$this->checkCancelled(); $this->checkCancelled();
if($this->result === null){ 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; return $this->result;
} }
@ -95,7 +95,7 @@ class CompressBatchPromise{
public function cancel() : void{ public function cancel() : void{
if($this->hasResult()){ if($this->hasResult()){
throw new \InvalidStateException("Cannot cancel a resolved promise"); throw new \LogicException("Cannot cancel a resolved promise");
} }
$this->cancelled = true; $this->cancelled = true;
} }

View File

@ -146,7 +146,7 @@ class PermissibleInternal implements Permissible{
foreach($this->rootPermissions as $name => $isGranted){ foreach($this->rootPermissions as $name => $isGranted){
$perm = $permManager->getPermission($name); $perm = $permManager->getPermission($name);
if($perm === null){ 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); $this->permissions[$name] = new PermissionAttachmentInfo($name, null, $isGranted, null);
$permManager->subscribeToPermission($name, $this); $permManager->subscribeToPermission($name, $this);

View File

@ -475,7 +475,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
public function getNetworkSession() : NetworkSession{ public function getNetworkSession() : NetworkSession{
if($this->networkSession === null){ if($this->networkSession === null){
throw new \InvalidStateException("Player is not connected"); throw new \LogicException("Player is not connected");
} }
return $this->networkSession; 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{ public function onPostDisconnect(string $reason, Translatable|string|null $quitMessage) : void{
if($this->isConnected()){ 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 //prevent the player receiving their own disconnect message

View File

@ -92,7 +92,7 @@ class AsyncWorker extends Worker{
*/ */
public function saveToThreadStore(string $identifier, $value) : void{ public function saveToThreadStore(string $identifier, $value) : void{
if(\Thread::getCurrentThread() !== $this){ 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; self::$store[$identifier] = $value;
} }
@ -109,7 +109,7 @@ class AsyncWorker extends Worker{
*/ */
public function getFromThreadStore(string $identifier){ public function getFromThreadStore(string $identifier){
if(\Thread::getCurrentThread() !== $this){ 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; return self::$store[$identifier] ?? null;
} }
@ -119,7 +119,7 @@ class AsyncWorker extends Worker{
*/ */
public function removeFromThreadStore(string $identifier) : void{ public function removeFromThreadStore(string $identifier) : void{
if(\Thread::getCurrentThread() !== $this){ 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]); unset(self::$store[$identifier]);
} }

View File

@ -88,12 +88,9 @@ class TaskScheduler{
return $this->tasks->contains($task); return $this->tasks->contains($task);
} }
/**
* @throws \InvalidStateException
*/
private function addTask(Task $task, int $delay, int $period) : TaskHandler{ private function addTask(Task $task, int $delay, int $period) : TaskHandler{
if(!$this->enabled){ 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){ if($delay <= 0){

View File

@ -144,8 +144,7 @@ class Config{
* @param mixed[] $default * @param mixed[] $default
* @phpstan-param array<string, mixed> $default * @phpstan-param array<string, mixed> $default
* *
* @throws \InvalidArgumentException if config type could not be auto-detected * @throws \InvalidArgumentException if config type is invalid or could not be auto-detected
* @throws \InvalidStateException if config type is invalid
*/ */
private function load(string $file, int $type = Config::DETECT, array $default = []) : void{ private function load(string $file, int $type = Config::DETECT, array $default = []) : void{
$this->file = $file; $this->file = $file;
@ -187,7 +186,7 @@ class Config{
$config = array_fill_keys(self::parseList($content), true); $config = array_fill_keys(self::parseList($content), true);
break; break;
default: default:
throw new \InvalidStateException("Config type is unknown"); throw new \InvalidArgumentException("Invalid config type specified");
} }
$this->config = is_array($config) ? $config : $default; $this->config = is_array($config) ? $config : $default;
if($this->fillDefaults($default, $this->config) > 0){ if($this->fillDefaults($default, $this->config) > 0){

View File

@ -43,7 +43,7 @@ final class PromiseResolver{
*/ */
public function resolve($value) : void{ public function resolve($value) : void{
if($this->shared->resolved){ 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->resolved = true;
$this->shared->result = $value; $this->shared->result = $value;
@ -56,7 +56,7 @@ final class PromiseResolver{
public function reject() : void{ public function reject() : void{
if($this->shared->resolved){ 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->resolved = true;
foreach($this->shared->onFailure as $c){ foreach($this->shared->onFailure as $c){

View File

@ -64,7 +64,7 @@ abstract class Terminal{
public static function hasFormattingCodes() : bool{ public static function hasFormattingCodes() : bool{
if(self::$formattingCodes === null){ 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; return self::$formattingCodes;
} }

View File

@ -516,7 +516,7 @@ class World implements ChunkManager{
*/ */
public function onUnload() : void{ public function onUnload() : void{
if($this->unloaded){ 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){ foreach($this->unloadCallbacks as $callback){
@ -770,7 +770,7 @@ class World implements ChunkManager{
*/ */
public function doTick(int $currentTick) : void{ public function doTick(int $currentTick) : void{
if($this->unloaded){ 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(); $this->timings->doTick->startTiming();
@ -2374,7 +2374,7 @@ class World implements ChunkManager{
if(isset($this->chunks[$hash = World::chunkHash($chunkX, $chunkZ)])){ if(isset($this->chunks[$hash = World::chunkHash($chunkX, $chunkZ)])){
$this->chunks[$hash]->addTile($tile); $this->chunks[$hash]->addTile($tile);
}else{ }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 //delegate tile ticking to the corresponding block
@ -2411,8 +2411,6 @@ class World implements ChunkManager{
* returned directly. * returned directly.
* *
* @return Chunk|null the requested chunk, or null on failure. * @return Chunk|null the requested chunk, or null on failure.
*
* @throws \InvalidStateException
*/ */
public function loadChunk(int $x, int $z) : ?Chunk{ public function loadChunk(int $x, int $z) : ?Chunk{
if(isset($this->chunks[$chunkHash = World::chunkHash($x, $z)])){ if(isset($this->chunks[$chunkHash = World::chunkHash($x, $z)])){