Skip to content

Commit 6dffb12

Browse files
authored
Merge pull request #5 from jdecool/fix-get-variants
Fix data return by EnumMethodReflection::getVariants
2 parents cc4dce0 + 8f9f18a commit 6dffb12

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

src/Reflection/EnumMethodReflection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use PHPStan\Reflection\ClassMemberReflection;
88
use PHPStan\Reflection\ClassReflection;
9+
use PHPStan\Reflection\FunctionVariant;
910
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Type\ObjectType;
1012

1113
class EnumMethodReflection implements MethodReflection
1214
{
@@ -58,6 +60,8 @@ public function getPrototype(): ClassMemberReflection
5860

5961
public function getVariants(): array
6062
{
61-
return [];
63+
return [
64+
new FunctionVariant([], false, new ObjectType($this->classReflection->getName())),
65+
];
6266
}
6367
}

tests/Reflection/EnumMethodsClassReflectionExtensionTest.php

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
use MyCLabs\Enum\Enum;
6-
use PHPStan\Reflection\ClassReflection;
7-
use PHPUnit\Framework\TestCase;
5+
use PHPStan\Reflection\ParametersAcceptorSelector;
6+
use PHPStan\Testing\TestCase;
7+
use PHPStan\Type\VerbosityLevel;
88
use Timeweb\PHPStan\Reflection\EnumMethodReflection;
99
use Timeweb\PHPStan\Reflection\EnumMethodsClassReflectionExtension;
1010

@@ -13,13 +13,19 @@
1313
*/
1414
class EnumMethodsClassReflectionExtensionTest extends TestCase
1515
{
16+
/**
17+
* @var \PHPStan\Broker\Broker
18+
*/
19+
protected $broker;
20+
1621
/**
1722
* @var EnumMethodsClassReflectionExtension
1823
*/
1924
protected $reflectionExtension;
2025

2126
public function setUp()
2227
{
28+
$this->broker = $this->createBroker();
2329
$this->reflectionExtension = new EnumMethodsClassReflectionExtension();
2430
}
2531

@@ -29,19 +35,7 @@ public function setUp()
2935
*/
3036
public function testEnumMethodsCanBeFoundInEnumSubclasses(bool $expected, string $methodName)
3137
{
32-
$classReflection = $this->createMock(ClassReflection::class);
33-
34-
$classReflection->expects($this->once())
35-
->method('getNativeReflection')
36-
->will($this->returnValue(new ReflectionClass(EnumFixture::class)))
37-
;
38-
39-
$classReflection->expects($this->once())
40-
->method('isSubclassOf')
41-
->with($this->equalTo(Enum::class))
42-
->will($this->returnValue(true))
43-
;
44-
38+
$classReflection = $this->broker->getClass(EnumFixture::class);
4539
$hasMethod = $this->reflectionExtension->hasMethod($classReflection, $methodName);
4640

4741
$this->assertEquals($expected, $hasMethod);
@@ -60,14 +54,7 @@ public function methodNameDataProvider(): array
6054
*/
6155
public function testEnumMethodsCannotBeFoundInNonEnumSubclasses()
6256
{
63-
$classReflection = $this->createMock(ClassReflection::class);
64-
65-
$classReflection->expects($this->once())
66-
->method('isSubclassOf')
67-
->with($this->equalTo(Enum::class))
68-
->will($this->returnValue(false))
69-
;
70-
57+
$classReflection = $this->broker->getClass(EnumFixture::class);
7158
$hasMethod = $this->reflectionExtension->hasMethod($classReflection, 'SOME_NAME');
7259

7360
$this->assertFalse($hasMethod);
@@ -79,10 +66,43 @@ public function testEnumMethodsCannotBeFoundInNonEnumSubclasses()
7966
*/
8067
public function testEnumMethodReflectionCanBeObtained()
8168
{
82-
$classReflection = $this->createMock(ClassReflection::class);
83-
69+
$classReflection = $this->broker->getClass(EnumFixture::class);
8470
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'SOME_NAME');
8571

8672
$this->assertInstanceOf(EnumMethodReflection::class, $methodReflection);
8773
}
74+
75+
/**
76+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getName
77+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getDeclaringClass
78+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isStatic
79+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isPrivate
80+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isPublic
81+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getPrototype
82+
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getVariants
83+
* @uses Timeweb\PHPStan\Reflection\EnumMethodReflection
84+
* @dataProvider enumFixtureProperties
85+
*/
86+
public function testEnumMethodProperties(string $propertyName)
87+
{
88+
$classReflection = $this->broker->getClass(EnumFixture::class);
89+
$methodReflection = $this->reflectionExtension->getMethod($classReflection, $propertyName);
90+
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
91+
92+
$this->assertSame($propertyName, $methodReflection->getName());
93+
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
94+
$this->assertTrue($methodReflection->isStatic());
95+
$this->assertFalse($methodReflection->isPrivate());
96+
$this->assertTrue($methodReflection->isPublic());
97+
$this->assertSame(EnumFixture::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
98+
}
99+
100+
public function enumFixtureProperties(): array
101+
{
102+
return [
103+
['MEMBER'],
104+
['PUBLIC_CONST'],
105+
['PRIVATE_CONST'],
106+
];
107+
}
88108
}

tests/_fixture/EnumFixture.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
class EnumFixture extends MyCLabs\Enum\Enum
44
{
55
const MEMBER = 'member';
6+
7+
public const PUBLIC_CONST = 'public';
8+
private const PRIVATE_CONST = 'private';
69
}

0 commit comments

Comments
 (0)