Skip to content

Commit

Permalink
Merge pull request #1216 from mockery/intersection-types
Browse files Browse the repository at this point in the history
Fixes code generation for intersection types
  • Loading branch information
davedevelopment committed Feb 23, 2023
2 parents 7eb2e0e + 52c3aff commit e653053
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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)
{
}
}

0 comments on commit e653053

Please sign in to comment.