From a62d0be16b9557d223b437392f4f3184e2661f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= <honza.kuchar@grifart.cz> Date: Fri, 28 Apr 2017 08:04:29 +0200 Subject: [PATCH] finished rename optional -> nullable --- src/SignatureAssertionUtil.php | 16 ++++++++-------- src/exceptions.php | 6 +++--- ....phpt => fn.assertSignature.allNullable.phpt} | 10 +++++----- 3 files changed, 16 insertions(+), 16 deletions(-) rename tests/{fn.assertSignature.allOptional.phpt => fn.assertSignature.allNullable.phpt} (83%) diff --git a/src/SignatureAssertionUtil.php b/src/SignatureAssertionUtil.php index 05c74b9..4a7f4e8 100644 --- a/src/SignatureAssertionUtil.php +++ b/src/SignatureAssertionUtil.php @@ -71,32 +71,32 @@ final class SignatureAssertionUtil private static function parseType(string $type): array { $cleanType = $type; // e.g. ?Namespace\Class - $optional = FALSE; + $nullable = FALSE; if ($type[0] === '?') { - $optional = TRUE; + $nullable = TRUE; $cleanType = substr($type, 1); // without leading '?' } - return [$cleanType, $optional]; + return [$cleanType, $nullable]; } private static function checkFunctionParameter( \ReflectionFunction $functionReflection, \ReflectionParameter $parameterReflection, string $expectedParameterType, - bool $expectedOptional + bool $expectedNullable ): void { $reflectionType = $parameterReflection->getType(); assert($reflectionType instanceof \ReflectionType); // checks: - $actuallyOptional = $reflectionType->allowsNull(); - if ($expectedOptional !== $actuallyOptional) { + $actuallyNullable = $reflectionType->allowsNull(); + if ($expectedNullable !== $actuallyNullable) { throw FunctionSignatureAssertionError::parameterNullability( $functionReflection, $parameterReflection, - $expectedOptional, - $actuallyOptional + $expectedNullable, + $actuallyNullable ); } if ($expectedParameterType !== (string) $reflectionType) { diff --git a/src/exceptions.php b/src/exceptions.php index c227449..d2075fd 100644 --- a/src/exceptions.php +++ b/src/exceptions.php @@ -50,12 +50,12 @@ final class FunctionSignatureAssertionError extends \AssertionError { } - public static function parameterNullability(\ReflectionFunction $functionReflection, \ReflectionParameter $param, bool $expectedOptional, bool $actuallyOptional): self + public static function parameterNullability(\ReflectionFunction $functionReflection, \ReflectionParameter $param, bool $expectedNullable, bool $actuallyNullable): self { return new self( $functionReflection, - $expectedOptional - ? "Function should have parameter {$param->getName()} optional but it is not." + $expectedNullable + ? "Function should have parameter {$param->getName()} nullable but it is not." : "Function should have parameter {$param->getName()} required but it is not." ); } diff --git a/tests/fn.assertSignature.allOptional.phpt b/tests/fn.assertSignature.allNullable.phpt similarity index 83% rename from tests/fn.assertSignature.allOptional.phpt rename to tests/fn.assertSignature.allNullable.phpt index 9f845dc..a89737a 100644 --- a/tests/fn.assertSignature.allOptional.phpt +++ b/tests/fn.assertSignature.allNullable.phpt @@ -8,20 +8,20 @@ use Tester\Assert; $fn = function(?T1 $t1, ?T2 $t2): ?T3 {return new T3;}; assertSignature($fn, params(nullable(T1::class), nullable(T2::class)), nullable(T3::class)); -// missing optional check -Assert::exception(function () use ($fn) { +// parameter is nullable, non-nullable is expected +Assert::exception(function () use ($fn) { // 1st param assertSignature($fn, params(T1::class, nullable(T2::class)), nullable(T3::class)); }, \AssertionError::class /* todo: assert message */); -Assert::exception(function () use ($fn) { +Assert::exception(function () use ($fn) { // 2nd param assertSignature($fn, params(nullable(T1::class), T2::class), nullable(T3::class)); }, \AssertionError::class /* todo: assert message */); -Assert::exception(function () use ($fn) { +Assert::exception(function () use ($fn) { // return type assertSignature($fn, params(nullable(T1::class), nullable(T2::class)), T3::class); }, \AssertionError::class /* todo: assert message */); -// Wrong return type type +// Wrong return type Assert::exception(function () use ($fn) { assertSignature($fn, params(nullable(T1::class), nullable(T2::class)), nullable(T2::class)); }, \AssertionError::class /* todo: assert message */); -- GitLab