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,17 +301,27 @@ 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
* 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 */
try{
$class = new \ReflectionClass($className);
if(is_a($className, Entity::class, true) and !$class->isAbstract()){
}catch(\ReflectionException $e){
throw new \InvalidArgumentException("Class $className does not exist");
}
if(!$class->isSubclassOf(Entity::class)){
throw new \InvalidArgumentException("Class $className does not extend " . Entity::class);
}
if(!$class->isInstantiable()){
throw new \InvalidArgumentException("Class $className cannot be constructed");
}
if($className::NETWORK_ID !== -1){
self::$knownEntities[$className::NETWORK_ID] = $className;
}elseif(!$force){
return false;
throw new \InvalidArgumentException("Class $className does not declare a valid NETWORK_ID and not force-registering");
}
$shortName = $class->getShortName();
@ -324,11 +334,6 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
self::$saveNames[$className] = $saveNames;
return true;
}
return false;
}
/**

View File

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