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

Support PHP8.1 intersection types #1164

Merged
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
2 changes: 2 additions & 0 deletions .styleci.yml
@@ -1,5 +1,7 @@
preset: psr12

version: 8.1

enabled:
- symfony_braces

Expand Down
4 changes: 2 additions & 2 deletions library/Mockery/Reflector.php
Expand Up @@ -147,8 +147,8 @@ private static function typeToString(\ReflectionType $type, \ReflectionClass $de
*/
private static function getTypeInformation(\ReflectionType $type, \ReflectionClass $declaringClass)
{
// PHP 8 union types can be recursively processed
if ($type instanceof \ReflectionUnionType) {
// PHP 8 union types and PHP 8.1 intersection types can be recursively processed
if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
$types = [];

foreach ($type->getTypes() as $innterType) {
Expand Down
24 changes: 24 additions & 0 deletions tests/PHP81/Php81LanguageFeaturesTest.php
Expand Up @@ -72,6 +72,16 @@ public function testMockingClassWithNewInInitializer()

$this->assertInstanceOf(ClassWithNewInInitializer::class, $mock);
}

/** @test */
public function it_can_mock_a_class_with_an_intersection_argument_type_hint()
{
$mock = Mockery::mock(ArgumentIntersectionTypeHint::class);
$object = new IntersectionTypeHelperClass();
$mock->allows()->foo($object);

$mock->foo($object);
}
}

interface LoggerInterface
Expand Down Expand Up @@ -124,3 +134,17 @@ public function getTimestamp(): float
{
}
}

class IntersectionTypeHelperClass
{
}
interface IntersectionTypeHelperInterface
{
}

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