Upgrade CallbackValidator

closes #6343
This commit is contained in:
Dylan K. Taylor
2025-08-30 19:23:38 +01:00
parent 06b48d97e9
commit 9a9506b793
7 changed files with 25 additions and 45 deletions

View File

@ -38,7 +38,7 @@
"pocketmine/bedrock-item-upgrade-schema": "~1.15.0+bedrock-1.21.100", "pocketmine/bedrock-item-upgrade-schema": "~1.15.0+bedrock-1.21.100",
"pocketmine/bedrock-protocol": "~40.0.0+bedrock-1.21.100", "pocketmine/bedrock-protocol": "~40.0.0+bedrock-1.21.100",
"pocketmine/binaryutils": "^0.2.1", "pocketmine/binaryutils": "^0.2.1",
"pocketmine/callback-validator": "^1.0.2", "pocketmine/callback-validator": "dev-rewrite",
"pocketmine/color": "^0.3.0", "pocketmine/color": "^0.3.0",
"pocketmine/errorhandler": "^0.7.0", "pocketmine/errorhandler": "^0.7.0",
"pocketmine/locale-data": "~2.25.0", "pocketmine/locale-data": "~2.25.0",

23
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "326e0882230a0614d3dd2f0dce8e2efc", "content-hash": "deb7c003ba4a6101153256d5d590da42",
"packages": [ "packages": [
{ {
"name": "adhocore/json-comment", "name": "adhocore/json-comment",
@ -344,30 +344,30 @@
}, },
{ {
"name": "pocketmine/callback-validator", "name": "pocketmine/callback-validator",
"version": "1.0.3", "version": "dev-rewrite",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/CallbackValidator.git", "url": "https://github.com/pmmp/CallbackValidator.git",
"reference": "64787469766bcaa7e5885242e85c23c25e8c55a2" "reference": "4b9b375590872cdd98a2a07c5aa0e1e99af5ceda"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/CallbackValidator/zipball/64787469766bcaa7e5885242e85c23c25e8c55a2", "url": "https://api.github.com/repos/pmmp/CallbackValidator/zipball/4b9b375590872cdd98a2a07c5aa0e1e99af5ceda",
"reference": "64787469766bcaa7e5885242e85c23c25e8c55a2", "reference": "4b9b375590872cdd98a2a07c5aa0e1e99af5ceda",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-reflection": "*", "ext-reflection": "*",
"php": "^7.1 || ^8.0" "php": "^8.1"
}, },
"replace": { "replace": {
"daverandom/callback-validator": "*" "daverandom/callback-validator": "*"
}, },
"require-dev": { "require-dev": {
"phpstan/extension-installer": "^1.0", "phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "0.12.59", "phpstan/phpstan": "2.1.0",
"phpstan/phpstan-strict-rules": "^0.12.4", "phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.0" "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -388,9 +388,9 @@
"description": "Fork of daverandom/callback-validator - Tools for validating callback signatures", "description": "Fork of daverandom/callback-validator - Tools for validating callback signatures",
"support": { "support": {
"issues": "https://github.com/pmmp/CallbackValidator/issues", "issues": "https://github.com/pmmp/CallbackValidator/issues",
"source": "https://github.com/pmmp/CallbackValidator/tree/1.0.3" "source": "https://github.com/pmmp/CallbackValidator/tree/rewrite"
}, },
"time": "2020-12-11T01:45:37+00:00" "time": "2025-01-03T17:50:24+00:00"
}, },
{ {
"name": "pocketmine/color", "name": "pocketmine/color",
@ -2734,6 +2734,7 @@
], ],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": {
"pocketmine/callback-validator": 20,
"pocketmine/math": 20 "pocketmine/math": 20
}, },
"prefer-stable": false, "prefer-stable": false,

View File

@ -23,9 +23,6 @@ declare(strict_types=1);
namespace pocketmine\entity; namespace pocketmine\entity;
use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\ParameterType;
use DaveRandom\CallbackValidator\ReturnType;
use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap;
use pocketmine\data\bedrock\PotionTypeIdMap; use pocketmine\data\bedrock\PotionTypeIdMap;
@ -206,11 +203,7 @@ final class EntityFactory{
throw new \InvalidArgumentException("At least one save name must be provided"); throw new \InvalidArgumentException("At least one save name must be provided");
} }
Utils::testValidInstance($className, Entity::class); Utils::testValidInstance($className, Entity::class);
Utils::validateCallableSignature(new CallbackType( Utils::validateCallableSignature(fn(World $world, CompoundTag $nbt) : Entity => die(), $creationFunc);
new ReturnType(Entity::class),
new ParameterType("world", World::class),
new ParameterType("nbt", CompoundTag::class)
), $creationFunc);
foreach($saveNames as $name){ foreach($saveNames as $name){
$this->creationFuncs[$name] = $creationFunc; $this->creationFuncs[$name] = $creationFunc;

View File

@ -23,9 +23,6 @@ declare(strict_types=1);
namespace pocketmine\item\enchantment; namespace pocketmine\item\enchantment;
use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\ParameterType;
use DaveRandom\CallbackValidator\ReturnType;
use pocketmine\lang\Translatable; use pocketmine\lang\Translatable;
use pocketmine\utils\NotCloneable; use pocketmine\utils\NotCloneable;
use pocketmine\utils\NotSerializable; use pocketmine\utils\NotSerializable;
@ -55,10 +52,7 @@ class Enchantment{
){ ){
$this->minEnchantingPower = $minEnchantingPower ?? fn(int $level) : int => 1; $this->minEnchantingPower = $minEnchantingPower ?? fn(int $level) : int => 1;
Utils::validateCallableSignature(new CallbackType( Utils::validateCallableSignature(fn(int $level) : int => die(), $this->minEnchantingPower);
new ReturnType("int"),
new ParameterType("level", "int")
), $this->minEnchantingPower);
} }
/** /**

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler; namespace pocketmine\scheduler;
use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\ReturnType;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
/** /**
@ -46,7 +44,7 @@ class ClosureTask extends Task{
public function __construct( public function __construct(
private \Closure $closure private \Closure $closure
){ ){
Utils::validateCallableSignature(new CallbackType(new ReturnType()), $closure); Utils::validateCallableSignature(function() : void{}, $closure);
} }
public function getName() : string{ public function getName() : string{

View File

@ -27,7 +27,7 @@ declare(strict_types=1);
namespace pocketmine\utils; namespace pocketmine\utils;
use DaveRandom\CallbackValidator\CallbackType; use DaveRandom\CallbackValidator\Prototype;
use pocketmine\entity\Location; use pocketmine\entity\Location;
use pocketmine\errorhandler\ErrorTypeToStringMap; use pocketmine\errorhandler\ErrorTypeToStringMap;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
@ -554,20 +554,14 @@ final class Utils{
* Verifies that the given callable is compatible with the desired signature. Throws a TypeError if they are * Verifies that the given callable is compatible with the desired signature. Throws a TypeError if they are
* incompatible. * incompatible.
* *
* @param callable|CallbackType $signature Dummy callable with the required parameters and return type * @param \Closure $signature Dummy callable with the required parameters and return type
* @param callable $subject Callable to check the signature of * @param \Closure $subject Callable to check the signature of
* @phpstan-param anyCallable|CallbackType $signature * @phpstan-param anyClosure $signature
* @phpstan-param anyCallable $subject * @phpstan-param anyClosure $subject
*
* @throws \DaveRandom\CallbackValidator\InvalidCallbackException
* @throws \TypeError
*/ */
public static function validateCallableSignature(callable|CallbackType $signature, callable $subject) : void{ public static function validateCallableSignature(\Closure $signature, \Closure $subject) : void{
if(!($signature instanceof CallbackType)){ if(!Prototype::isSatisfiedBy($signature, $subject)){
$signature = CallbackType::createFromCallable($signature); throw new \TypeError("Declaration of callable `" . Prototype::print($subject) . "` must be compatible with `" . Prototype::print($signature) . "`");
}
if(!$signature->isSatisfiedBy($subject)){
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($subject) . "` must be compatible with `" . $signature . "`");
} }
} }

View File

@ -127,7 +127,7 @@ class BlockTransaction{
* @phpstan-param \Closure(ChunkManager $world, int $x, int $y, int $z) : bool $validator * @phpstan-param \Closure(ChunkManager $world, int $x, int $y, int $z) : bool $validator
*/ */
public function addValidator(\Closure $validator) : void{ public function addValidator(\Closure $validator) : void{
Utils::validateCallableSignature([$this, 'dummyValidator'], $validator); Utils::validateCallableSignature($this->dummyValidator(...), $validator);
$this->validators[] = $validator; $this->validators[] = $validator;
} }