diff --git a/src/PhpDoc/ReflectionEnumStubFilesExtension.php b/src/PhpDoc/ReflectionEnumStubFilesExtension.php index d48519b735..0854173b94 100644 --- a/src/PhpDoc/ReflectionEnumStubFilesExtension.php +++ b/src/PhpDoc/ReflectionEnumStubFilesExtension.php @@ -19,11 +19,16 @@ public function getFiles(): array return []; } + $files = [ + __DIR__ . '/../../stubs/UnitEnum.stub', + __DIR__ . '/../../stubs/BackedEnum.stub', + ]; + if (!$this->phpVersion->supportsLazyObjects()) { - return [__DIR__ . '/../../stubs/ReflectionEnum.stub']; + return [__DIR__ . '/../../stubs/ReflectionEnum.stub', ...$files]; } - return [__DIR__ . '/../../stubs/ReflectionEnumWithLazyObjects.stub']; + return [__DIR__ . '/../../stubs/ReflectionEnumWithLazyObjects.stub', ...$files]; } } diff --git a/stubs/BackedEnum.stub b/stubs/BackedEnum.stub new file mode 100644 index 0000000000..680c687e29 --- /dev/null +++ b/stubs/BackedEnum.stub @@ -0,0 +1,16 @@ + + * @pure + */ + public static function cases(): array; +} diff --git a/tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php b/tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php index 44e326a0d6..66d1cec464 100644 --- a/tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php +++ b/tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php @@ -174,4 +174,10 @@ public function testBug12224(): void ]); } + #[RequiresPhp('>= 8.1')] + public function testBug13201(): void + { + $this->analyse([__DIR__ . '/data/bug-13201.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Pure/data/bug-13201.php b/tests/PHPStan/Rules/Pure/data/bug-13201.php new file mode 100644 index 0000000000..09ed46ef53 --- /dev/null +++ b/tests/PHPStan/Rules/Pure/data/bug-13201.php @@ -0,0 +1,19 @@ += 8.1 + +namespace PHPStan\Rules\Pure\data\Bug13201; + +enum Foo: string +{ + + case Bar = 'bar'; + case Unknown = 'unknown'; + +} + +/** + * @pure + */ +function createWithFallback(string $type): Foo +{ + return Foo::tryFrom($type) ?? Foo::Unknown; +}