diff --git a/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php b/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php new file mode 100644 index 00000000000..5cc7f9b2943 --- /dev/null +++ b/src/Type/Php/ReflectionEnumGetCaseDynamicReturnTypeExtension.php @@ -0,0 +1,43 @@ +getName() === 'getCase'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type + { + $backingType = $scope->getType(new MethodCall($methodCall->var, new Identifier('getBackingType'))); + if (!$backingType->isNull()->no()) { + return null; + } + + return new ObjectType('ReflectionEnumBackedCase'); + } + +} diff --git a/stubs/ReflectionEnumWithLazyObjects.stub b/stubs/ReflectionEnumWithLazyObjects.stub index 92ec6c660b6..811f785cc22 100644 --- a/stubs/ReflectionEnumWithLazyObjects.stub +++ b/stubs/ReflectionEnumWithLazyObjects.stub @@ -19,7 +19,9 @@ class ReflectionEnum extends ReflectionClass public function getCase(string $name): ReflectionEnumUnitCase {} /** - * @phpstan-assert-if-true self $this + * @phpstan-assert-if-true class-string $this->name + * @phpstan-assert-if-true class-string $this->getName() + * @phpstan-assert-if-true array $this->getCases() * @phpstan-assert-if-true !null $this->getBackingType() */ public function isBacked(): bool {} diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index fd6133805c5..71ee0a86c8f 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -52,7 +52,7 @@ private static function findTestFiles(): iterable yield __DIR__ . '/data/enum-reflection-php81.php'; } - if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80400) { + if (PHP_VERSION_ID >= 80100) { yield __DIR__ . '/data/enum-reflection-backed.php'; } diff --git a/tests/PHPStan/Analyser/data/enum-reflection-backed.php b/tests/PHPStan/Analyser/data/enum-reflection-backed.php index 00f1b9634fd..54d3f2ea433 100644 --- a/tests/PHPStan/Analyser/data/enum-reflection-backed.php +++ b/tests/PHPStan/Analyser/data/enum-reflection-backed.php @@ -1,4 +1,4 @@ -= 8.1 namespace EnumReflection; diff --git a/tests/PHPStan/Analyser/nsrt/bug-15165.php b/tests/PHPStan/Analyser/nsrt/bug-15165.php new file mode 100644 index 00000000000..59fe159006d --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15165.php @@ -0,0 +1,40 @@ += 8.4 + +namespace Bug15165Types; + +use ReflectionEnum; +use UnitEnum; +use function PHPStan\Testing\assertType; + +/** @param class-string $class */ +function testNarrowGetNameTypeAfterIsBacked(string $class): void +{ + $r = new ReflectionEnum($class); + assertType('class-string', $r->getName()); + assertType('class-string', $r->name); + assertType('ReflectionNamedType|null', $r->getBackingType()); + assertType('array', $r->getCases()); + assertType('ReflectionEnumUnitCase', $r->getCase('A')); + if ($r->isBacked()) { + assertType('array', $r->getCases()); + assertType('ReflectionEnumBackedCase', $r->getCase('A')); + assertType('class-string', $r->getName()); + assertType('class-string', $r->name); + assertType('ReflectionNamedType', $r->getBackingType()); + } +} + +/** + * @template T of UnitEnum + * @param ReflectionEnum $enum + */ +function testTemplate(ReflectionEnum $enum): void +{ + assertType('ReflectionEnumUnitCase', $enum->getCase('A')); + if ($enum->isBacked()) { + assertType('ReflectionEnum', $enum); + assertType('ReflectionNamedType', $enum->getBackingType()); + assertType('array', $enum->getCases()); + assertType('ReflectionEnumBackedCase', $enum->getCase('A')); + } +} diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php index 076a291bb79..0f07c7e1ac1 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php @@ -7,6 +7,7 @@ use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\RequiresPhp; +use const PHP_VERSION_ID; /** * @extends RuleTestCase @@ -346,6 +347,19 @@ public function testBug14705(): void ]); } + #[RequiresPhp('>= 8.1.0')] + public function testBug15165(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-15165.php'], [ + [ + 'Call to method ReflectionEnum::isBacked() will always evaluate to false.', + 58, + PHP_VERSION_ID >= 80400 ? 'Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.' : null, + ], + ]); + } + public function testInTrait(): void { $this->treatPhpDocTypesAsCertain = true; diff --git a/tests/PHPStan/Rules/Comparison/data/bug-15165.php b/tests/PHPStan/Rules/Comparison/data/bug-15165.php new file mode 100644 index 00000000000..5ee1e8fb635 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-15165.php @@ -0,0 +1,66 @@ += 8.1 + +namespace Bug15165; + +use BackedEnum; +use ReflectionEnum; +use UnitEnum; + +/** + * @template T of UnitEnum + * @param ReflectionEnum $enum + * @return void + */ +function testUnit(ReflectionEnum $enum): void +{ + if ($enum->isBacked()) { + echo "Backed!"; + } +} + +/** + * @template T of BackedEnum + * @param ReflectionEnum $enum + * @return void + */ +function testBacked(ReflectionEnum $enum): void +{ + if (!$enum->isBacked()) { + echo "Unit!"; + } +} + +/** + * @template T of BackedEnum|UnitEnum + * @param ReflectionEnum $enum + * @return void + */ +function testAny(ReflectionEnum $enum): void +{ + if ($enum->isBacked()) { + echo "Backed!"; + } +} + +enum UnitFoo +{ + case A; +} + +enum BackedFoo: int +{ + case A = 1; +} + +function testConcrete(): void +{ + $unit = new ReflectionEnum(UnitFoo::class); + if ($unit->isBacked()) { + echo "Backed!"; + } + + $backed = new ReflectionEnum(BackedFoo::class); + if ($backed->isBacked()) { + echo "Backed!"; + } +}