EntityFactory: Avoid code duplication on validation of creation functions (#5294)

This commit is contained in:
IvanCraft623 2022-09-24 07:55:24 -05:00 committed by GitHub
parent e4fc523251
commit 83a136a176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -175,20 +175,6 @@ final class EntityFactory{
}, ['Human']);
}
/**
* @phpstan-param \Closure(World, CompoundTag) : Entity $creationFunc
*/
private static function validateCreationFunc(\Closure $creationFunc) : void{
$sig = new CallbackType(
new ReturnType(Entity::class),
new ParameterType("world", World::class),
new ParameterType("nbt", CompoundTag::class)
);
if(!$sig->isSatisfiedBy($creationFunc)){
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($creationFunc) . "` must be compatible with `" . $sig . "`");
}
}
/**
* Registers an entity type into the index.
*
@ -207,7 +193,11 @@ final class EntityFactory{
throw new \InvalidArgumentException("At least one save name must be provided");
}
Utils::testValidInstance($className, Entity::class);
self::validateCreationFunc($creationFunc);
Utils::validateCallableSignature(new CallbackType(
new ReturnType(Entity::class),
new ParameterType("world", World::class),
new ParameterType("nbt", CompoundTag::class)
), $creationFunc);
foreach($saveNames as $name){
$this->creationFuncs[$name] = $creationFunc;