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

Fixes code generation for intersection types #1216

Merged
merged 2 commits into from
Feb 23, 2023
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
3 changes: 2 additions & 1 deletion library/Mockery/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public static function getSimplestReturnType(\ReflectionMethod $method)
*/
private static function typeToString(\ReflectionType $type, \ReflectionClass $declaringClass)
{
return \implode('|', \array_map(function (array $typeInformation) {
$char = $type instanceof \ReflectionIntersectionType ? "&" : "|";
return \implode($char, \array_map(function (array $typeInformation) {
return $typeInformation['typeHint'];
}, self::getTypeInformation($type, $declaringClass)));
}
Expand Down
18 changes: 14 additions & 4 deletions tests/PHP81/Php81LanguageFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ public function testMockingClassWithNewInInitializer()
/** @test */
public function it_can_mock_a_class_with_an_intersection_argument_type_hint()
{
$mock = Mockery::mock(ArgumentIntersectionTypeHint::class);
$mock = Mockery::spy(ArgumentIntersectionTypeHint::class);
$object = new IntersectionTypeHelperClass();
$mock->allows()->foo($object);

$mock->foo($object);

$this->expectException(\TypeError::class);
$mock->foo(Mockery::mock(IntersectionTypeHelper1Interface::class));
}

/** @test */
Expand Down Expand Up @@ -184,16 +187,23 @@ public function exits(): never
exit(123);
}
}
class IntersectionTypeHelperClass
class IntersectionTypeHelperClass implements IntersectionTypeHelper1Interface, IntersectionTypeHelper2Interface
{
function foo(): int { return 123; }
function bar(): int { return 123; }
}
interface IntersectionTypeHelper2Interface
{
function foo(): int;
}
interface IntersectionTypeHelperInterface
interface IntersectionTypeHelper1Interface
{
function bar(): int;
}

class ArgumentIntersectionTypeHint
{
public function foo(IntersectionTypeHelperClass&IntersectionTypeHelperInterface $foo)
public function foo(IntersectionTypeHelper1Interface&IntersectionTypeHelper2Interface $foo)
{
}
}