Make Tile::registerTile() and Entity::registerEntity() throw exceptions instead of returning false

This commit is contained in:
Dylan K. Taylor 2018-07-26 14:55:55 +01:00
parent b9769c407b
commit d305a1342f
2 changed files with 51 additions and 45 deletions

View File

@ -301,34 +301,39 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
* NOTE: The first save name in the $saveNames array will be used when saving the entity to disk. The reflection * NOTE: The first save name in the $saveNames array will be used when saving the entity to disk. The reflection
* name of the class will be appended to the end and only used if no other save names are specified. * name of the class will be appended to the end and only used if no other save names are specified.
* *
* @return bool * @throws \InvalidArgumentException
*/ */
public static function registerEntity(string $className, bool $force = false, array $saveNames = []) : bool{ public static function registerEntity(string $className, bool $force = false, array $saveNames = []) : void{
/** @var Entity $className */ /** @var Entity $className */
$class = new \ReflectionClass($className); try{
if(is_a($className, Entity::class, true) and !$class->isAbstract()){ $class = new \ReflectionClass($className);
if($className::NETWORK_ID !== -1){ }catch(\ReflectionException $e){
self::$knownEntities[$className::NETWORK_ID] = $className; throw new \InvalidArgumentException("Class $className does not exist");
}elseif(!$force){ }
return false; if(!$class->isSubclassOf(Entity::class)){
} throw new \InvalidArgumentException("Class $className does not extend " . Entity::class);
}
$shortName = $class->getShortName(); if(!$class->isInstantiable()){
if(!in_array($shortName, $saveNames, true)){ throw new \InvalidArgumentException("Class $className cannot be constructed");
$saveNames[] = $shortName;
}
foreach($saveNames as $name){
self::$knownEntities[$name] = $className;
}
self::$saveNames[$className] = $saveNames;
return true;
} }
return false; if($className::NETWORK_ID !== -1){
self::$knownEntities[$className::NETWORK_ID] = $className;
}elseif(!$force){
throw new \InvalidArgumentException("Class $className does not declare a valid NETWORK_ID and not force-registering");
}
$shortName = $class->getShortName();
if(!in_array($shortName, $saveNames, true)){
$saveNames[] = $shortName;
}
foreach($saveNames as $name){
self::$knownEntities[$name] = $className;
}
self::$saveNames[$className] = $saveNames;
} }
/** /**

View File

@ -110,31 +110,32 @@ abstract class Tile extends Position{
} }
/** /**
* @param string $className * @param string $className
* @param array $saveNames * @param string[] $saveNames
*
* @return bool
* @throws \ReflectionException
*/ */
public static function registerTile(string $className, array $saveNames = []) : bool{ public static function registerTile(string $className, array $saveNames = []) : void{
$class = new \ReflectionClass($className); try{
if(is_a($className, Tile::class, true) and !$class->isAbstract()){ $class = new \ReflectionClass($className);
$shortName = $class->getShortName(); }catch(\ReflectionException $e){
if(!in_array($shortName, $saveNames, true)){ throw new \InvalidArgumentException("Class $className does not exist");
$saveNames[] = $shortName; }
} if(!$class->isSubclassOf(Tile::class)){
throw new \InvalidArgumentException("Class $className does not extend " . Tile::class);
foreach($saveNames as $name){ }
self::$knownTiles[$name] = $className; if(!$class->isInstantiable()){
} throw new \InvalidArgumentException("Class $className cannot be constructed");
self::$saveNames[$className] = $saveNames;
return true;
} }
return false; $shortName = $class->getShortName();
if(!in_array($shortName, $saveNames, true)){
$saveNames[] = $shortName;
}
foreach($saveNames as $name){
self::$knownTiles[$name] = $className;
}
self::$saveNames[$className] = $saveNames;
} }
/** /**