Skip to content

Commit

Permalink
Merge pull request #543 from ciaranmcnulty/disallow-intersection-types
Browse files Browse the repository at this point in the history
Disallow intersection types with a nicer error message
  • Loading branch information
ciaranmcnulty authored Sep 9, 2021
2 parents 68a1fab + 030281e commit 8fb3150
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fixtures/IntersectionArgumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Fixtures\Prophecy;

class IntersectionArgumentType
{
public function doSomething (Bar&Baz $foo)
{

}
}
11 changes: 11 additions & 0 deletions fixtures/IntersectionReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Fixtures\Prophecy;

class IntersectionReturnType
{
public function doSomething () : Bar&Baz
{

}
}
7 changes: 7 additions & 0 deletions src/Prophecy/Doubler/Generator/ClassMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Prophecy\Exception\InvalidArgumentException;
use Prophecy\Exception\Doubler\ClassMirrorException;
use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
Expand Down Expand Up @@ -223,6 +224,12 @@ private function getTypeHints(?ReflectionType $type, ?ReflectionClass $class, bo
elseif ($type instanceof ReflectionUnionType) {
$types = $type->getTypes();
}
elseif ($type instanceof ReflectionIntersectionType) {
throw new ClassMirrorException('Doubling intersection types is not supported', $class);
}
elseif(is_object($type)) {
throw new ClassMirrorException('Unknown reflection type ' . get_class($type), $class);
}

$types = array_map(
function(string $type) use ($class) {
Expand Down
28 changes: 28 additions & 0 deletions tests/Doubler/Generator/ClassMirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,32 @@ public function it_can_double_never_return_type()
$this->assertEquals(new ReturnTypeNode('never'), $methodNode->getReturnTypeNode());

}

/**
* @test
*/
public function it_can_not_double_intersection_return_types()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Intersection types are not supported in this PHP version');
}

$this->expectException(ClassMirrorException::class);

$classNode = (new ClassMirror())->reflect(new \ReflectionClass('Fixtures\Prophecy\IntersectionReturnType'), []);
}

/**
* @test
*/
public function it_can_not_double_intersection_argument_types()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Intersection types are not supported in this PHP version');
}

$this->expectException(ClassMirrorException::class);

$classNode = (new ClassMirror())->reflect(new \ReflectionClass('Fixtures\Prophecy\IntersectionArgumentType'), []);
}
}

0 comments on commit 8fb3150

Please sign in to comment.