diff --git a/src/FunctionSignatureAssertionError.php b/src/FunctionSignatureAssertionError.php index b5d141456e57b6ee5624f7ce13243462037507bd..8969048f1a570af9efa6bf3cbd8a779129f1ad3b 100644 --- a/src/FunctionSignatureAssertionError.php +++ b/src/FunctionSignatureAssertionError.php @@ -91,4 +91,12 @@ final class FunctionSignatureAssertionError extends \AssertionError { "Expected return type of type '$expectedReturnType', but given function declares '$actualReturnType'." ); } + + public static function parameterDoesNotAcceptAllRequiredValues(\ReflectionFunction $reflection, \ReflectionType $reflectionType): self + { + return new self( + $reflection, + "Parameter of given function does not accept all required values. Parameter restricts input values to {$reflectionType}, assertion requires mixed. (=must accept everything)" + ); + } }; diff --git a/src/SignatureAssertionUtil.php b/src/SignatureAssertionUtil.php index e468c8a0ccca85240660d09ce7ec9cf62a54660b..9efb028031faa1f1cca1b5f2ea41597ad765a25c 100644 --- a/src/SignatureAssertionUtil.php +++ b/src/SignatureAssertionUtil.php @@ -55,6 +55,17 @@ final class SignatureAssertionUtil ): void { $reflectionType = $parameterReflection->getType(); + if ($expectedParameterType === 'mixed') { + // must accept everything + if ($reflectionType === NULL) { + return; + } + + throw FunctionSignatureAssertionError::parameterDoesNotAcceptAllRequiredValues( + $functionReflection, + $reflectionType + ); + } assert($reflectionType instanceof \ReflectionType); // checks: @@ -80,7 +91,7 @@ final class SignatureAssertionUtil // Return-type: private static function checkReturnType(?string $expectedReturnType, \ReflectionFunction $reflection): void { - if ($expectedReturnType === NULL) { + if ($expectedReturnType === NULL || $expectedReturnType === 'mixed') { return; } diff --git a/tests/fn.assertSignature.mixed-pseudotype.phpt b/tests/fn.assertSignature.mixed-pseudotype.phpt new file mode 100644 index 0000000000000000000000000000000000000000..913756ee6f77a5c9d6818bf9236fb9ba33f0c4ea --- /dev/null +++ b/tests/fn.assertSignature.mixed-pseudotype.phpt @@ -0,0 +1,14 @@ +<?php declare(strict_types=1); +namespace MyTestNamespace; +require __DIR__ . '/bootstrap.php'; +require __DIR__ . '/testClasses.php'; +use function Grifart\AssertFunction\{assertSignature, nullable, params}; +use Grifart\AssertFunction\FunctionSignatureAssertionError; +use Tester\Assert; +$place = 'tests/' . basename(__FILE__) . ':' . (__LINE__ +2) . ' '; + +$fn = function(string $p1, $p2) {}; + +assertSignature($fn, params('string', 'mixed'), 'mixed'); + +Assert::true(true); // if assertion do not fail, we are OK