Skip to content

Commit 95ad39c

Browse files
committed
feat: type narrowing for ReflectionClass::
1 parent 1f6cc71 commit 95ad39c

File tree

5 files changed

+55
-28
lines changed

5 files changed

+55
-28
lines changed

stubs/ReflectionClass.stub

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ class ReflectionClass
4949
{
5050
}
5151

52-
/**
53-
* @phpstan-assert-if-true class-string<\UnitEnum> $this->name
54-
* @phpstan-assert-if-true ReflectionClass<\UnitEnum> $this
55-
* @phpstan-assert-if-false !class-string<\UnitEnum> $this->name
56-
*/
57-
public function isEnum(): bool
58-
{
59-
}
52+
/**
53+
* @phpstan-assert-if-true class-string<\UnitEnum> $this->name
54+
* @phpstan-assert-if-true class-string<\UnitEnum> $this->getName()
55+
* @phpstan-assert-if-true ReflectionClass<UnitEnum> $this
56+
* @return (T is \UnitEnum ? true: false)
57+
*/
58+
public function isEnum(): bool
59+
{
60+
}
61+
6062
}

stubs/ReflectionClassWithLazyObjects.stub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,13 @@ class ReflectionClass
110110
public function isUninitializedLazyObject(object $object): bool
111111
{
112112
}
113+
114+
/**
115+
* @phpstan-assert-if-true class-string<\UnitEnum> $this->name
116+
* @phpstan-assert-if-true class-string<\UnitEnum> $this->getName()
117+
* @return (T is \UnitEnum ? true: false)
118+
*/
119+
public function isEnum(): bool
120+
{
121+
}
113122
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ private static function findTestFiles(): iterable
238238
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
239239
yield __DIR__ . '/../Rules/Arrays/data/narrow-superglobal.php';
240240
yield __DIR__ . '/../Rules/Methods/data/bug-12927.php';
241-
yield __DIR__ . '/data/reflectionclass-isEnum.php';
242241
}
243242

244243
/**

tests/PHPStan/Analyser/data/reflectionclass-isEnum.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php // lint >= 8.1
2+
3+
namespace ReflectionClassIsEnum;
4+
5+
use ReflectionClass;
6+
use function PHPStan\Testing\assertType;
7+
8+
/**
9+
* @param class-string $class
10+
*/
11+
function testNarrowClassAfterIsEnum(string $class): void {
12+
$r = new ReflectionClass($class);
13+
if ($r->isEnum()) {
14+
assertType('class-string<UnitEnum>', $r->name);
15+
assertType('class-string<UnitEnum>', $r->getName());
16+
17+
18+
// Todo:
19+
//assertType('ReflectionClass<UnitEnum>', $r);
20+
21+
}
22+
23+
24+
25+
}
26+
27+
function testTemplateAssertions(): void {
28+
$enumR = new ReflectionClass(Foo::class);
29+
assertType('ReflectionClass<ReflectionClassIsEnum\\Foo>', $enumR);
30+
assertType('true', $enumR->isEnum());
31+
}
32+
33+
34+
enum Foo {
35+
case Bar;
36+
}

0 commit comments

Comments
 (0)