Utils: allow validateCallableSignature() to accept a manually constructed CallbackType instead of a closure

this allows more fine-grained control without PHPStan yelling at us.
This commit is contained in:
Dylan K. Taylor 2021-10-10 23:27:09 +01:00
parent fd2df637b6
commit 912e612743
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -488,17 +488,20 @@ final class Utils{
* Verifies that the given callable is compatible with the desired signature. Throws a TypeError if they are
* incompatible.
*
* @param callable $signature Dummy callable with the required parameters and return type
* @param callable $subject Callable to check the signature of
* @phpstan-param anyCallable $signature
* @phpstan-param anyCallable $subject
* @param callable|CallbackType $signature Dummy callable with the required parameters and return type
* @param callable $subject Callable to check the signature of
* @phpstan-param anyCallable|CallbackType $signature
* @phpstan-param anyCallable $subject
*
* @throws \DaveRandom\CallbackValidator\InvalidCallbackException
* @throws \TypeError
*/
public static function validateCallableSignature(callable $signature, callable $subject) : void{
if(!($sigType = CallbackType::createFromCallable($signature))->isSatisfiedBy($subject)){
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($subject) . "` must be compatible with `" . $sigType . "`");
public static function validateCallableSignature(callable|CallbackType $signature, callable $subject) : void{
if(!($signature instanceof CallbackType)){
$signature = CallbackType::createFromCallable($signature);
}
if(!$signature->isSatisfiedBy($subject)){
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($subject) . "` must be compatible with `" . $signature . "`");
}
}