Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on a union type including null #1106

Merged
merged 1 commit into from Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions library/Mockery/Reflector.php
Expand Up @@ -192,9 +192,11 @@ private static function typeToString(\ReflectionType $type, \ReflectionClass $de
{
// PHP 8 union types can be recursively processed
if ($type instanceof \ReflectionUnionType) {
return \implode('|', \array_map(function (\ReflectionType $type) use ($declaringClass) {
return self::typeToString($type, $declaringClass);
}, $type->getTypes()));
return \implode('|', \array_filter(\array_map(function (\ReflectionType $type) use ($declaringClass) {
$typeHint = self::typeToString($type, $declaringClass);

return $typeHint === 'null' ? null : $typeHint;
}, $type->getTypes())));
}

// PHP 7.0 doesn't have named types, but 7.1+ does
Expand Down
16 changes: 16 additions & 0 deletions tests/PHP80/Php80LanguageFeaturesTest.php
Expand Up @@ -29,6 +29,15 @@ public function it_can_mock_a_class_with_a_union_argument_type_hint()
$mock->foo($object);
}

/** @test */
public function it_can_mock_a_class_with_a_union_argument_type_hint_including_null()
{
$mock = mock(ArgumentUnionTypeHintWithNull::class);
$mock->allows()->foo(null);

$mock->foo(null);
}

/** @test */
public function it_can_mock_a_class_with_a_parent_argument_type_hint()
{
Expand Down Expand Up @@ -78,6 +87,13 @@ public function foo(string|array|self $foo)
}
}

class ArgumentUnionTypeHintWithNull
{
public function foo(string|array|null $foo)
{
}
}

class ArgumentParentTypeHint extends \stdClass
{
public function foo(parent $foo)
Expand Down