Skip to content
Snippets Groups Projects
Commit a62d0be1 authored by Jan Kuchař's avatar Jan Kuchař
Browse files

finished rename optional -> nullable

parent 980bcfc4
Branches
Tags
No related merge requests found
......@@ -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) {
......
......@@ -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."
);
}
......
......@@ -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 */);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment