Skip to content

Commit 6bf405c

Browse files
committed
Narrow type of reflection get*Type() after calling has*Type()
1 parent 9054120 commit 6bf405c

File tree

7 files changed

+111
-15
lines changed

7 files changed

+111
-15
lines changed

stubs/ReflectionFunctionAbstract.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,19 @@ abstract class ReflectionFunctionAbstract
1414
public function getAttributes(?string $name = null, int $flags = 0)
1515
{
1616
}
17+
18+
/**
19+
* @phpstan-assert-if-true !null $this->getReturnType()
20+
*/
21+
public function hasReturnType(): bool
22+
{
23+
}
24+
25+
/**
26+
* @phpstan-assert-if-true !null $this->getTentativeReturnType()
27+
*/
28+
public function hasTentativeReturnType(): bool
29+
{
30+
}
31+
1732
}

stubs/ReflectionParameter.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ class ReflectionParameter
88
public function getAttributes(?string $name = null, int $flags = 0)
99
{
1010
}
11+
12+
/**
13+
* @phpstan-assert-if-true !null $this->getType()
14+
*/
15+
public function hasType(): bool
16+
{
17+
}
18+
1119
}

stubs/ReflectionProperty.stub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22

33
class ReflectionProperty
44
{
5+
56
/**
67
* @return list<ReflectionAttribute<object>>
78
*/
89
public function getAttributes(?string $name = null, int $flags = 0)
910
{
1011
}
12+
13+
/**
14+
* @phpstan-assert-if-true !null $this->getType()
15+
*/
16+
public function hasType(): bool
17+
{
18+
}
19+
1120
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ private static function findTestFiles(): iterable
7777
yield __DIR__ . '/data/explode-php80.php';
7878
}
7979

80+
if (PHP_VERSION_ID >= 80000) {
81+
yield __DIR__ . '/data/reflection-type-php8.php';
82+
} else {
83+
yield __DIR__ . '/data/reflection-type-php7.php';
84+
}
85+
8086
if (PHP_VERSION_ID >= 80000) {
8187
yield __DIR__ . '/../Reflection/data/unionTypes.php';
8288
yield __DIR__ . '/../Reflection/data/mixedType.php';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ReflectionTypeTestPhp7;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function test(
8+
\ReflectionProperty $reflectionProperty,
9+
\ReflectionFunctionAbstract $reflectionFunctionAbstract,
10+
\ReflectionParameter $reflectionParameter
11+
){
12+
assertType('ReflectionType|null', $reflectionProperty->getType());
13+
assertType('ReflectionType|null', $reflectionFunctionAbstract->getReturnType());
14+
assertType('ReflectionType|null', $reflectionParameter->getType());
15+
16+
if ($reflectionProperty->hasType()) {
17+
assertType('ReflectionType', $reflectionProperty->getType());
18+
} else {
19+
assertType('null', $reflectionProperty->getType());
20+
}
21+
22+
if ($reflectionFunctionAbstract->hasReturnType()) {
23+
assertType('ReflectionType', $reflectionFunctionAbstract->getReturnType());
24+
} else {
25+
assertType('null', $reflectionFunctionAbstract->getReturnType());
26+
}
27+
28+
if ($reflectionParameter->hasType()) {
29+
assertType('ReflectionType', $reflectionParameter->getType());
30+
} else {
31+
assertType('null', $reflectionParameter->getType());
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ReflectionTypeTestPhp8;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function test(
8+
\ReflectionProperty $reflectionProperty,
9+
\ReflectionFunctionAbstract $reflectionFunctionAbstract,
10+
\ReflectionParameter $reflectionParameter
11+
){
12+
assertType('ReflectionType|null', $reflectionProperty->getType());
13+
assertType('ReflectionType|null', $reflectionFunctionAbstract->getReturnType());
14+
assertType('ReflectionType|null', $reflectionFunctionAbstract->getTentativeReturnType());
15+
assertType('ReflectionType|null', $reflectionParameter->getType());
16+
17+
if ($reflectionProperty->hasType()) {
18+
assertType('ReflectionType', $reflectionProperty->getType());
19+
} else {
20+
assertType('null', $reflectionProperty->getType());
21+
}
22+
23+
if ($reflectionFunctionAbstract->hasReturnType()) {
24+
assertType('ReflectionType', $reflectionFunctionAbstract->getReturnType());
25+
} else {
26+
assertType('null', $reflectionFunctionAbstract->getReturnType());
27+
}
28+
29+
if ($reflectionFunctionAbstract->hasTentativeReturnType()) {
30+
assertType('ReflectionType', $reflectionFunctionAbstract->getTentativeReturnType());
31+
} else {
32+
assertType('null', $reflectionFunctionAbstract->getTentativeReturnType());
33+
}
34+
35+
if ($reflectionParameter->hasType()) {
36+
assertType('ReflectionType', $reflectionParameter->getType());
37+
} else {
38+
assertType('null', $reflectionParameter->getType());
39+
}
40+
}

tests/PHPStan/Analyser/nsrt/reflection-type.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)