diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index f7de71487c..3b98fa813a 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -309,6 +309,7 @@ private function rememberConstructorExpressions(array $currentExpressionTypes): if ($expr instanceof FuncCall) { if ( !$expr->name instanceof Name + // interface_exists() etc. imply class_exists() therefore not listed here || !in_array($expr->name->name, ['class_exists', 'function_exists'], true) ) { continue; @@ -1376,10 +1377,13 @@ public function isInClassExists(string $className): bool 'class_exists', 'interface_exists', 'trait_exists', + 'enum_exists', ], true)) { return true; } } + + // interface_exists() etc. imply class_exists() therefore not listed here $expr = new FuncCall(new FullyQualified('class_exists'), [ new Arg(new String_(ltrim($className, '\\'))), ]); diff --git a/tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php b/tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php index 9c128b1f09..7e2e6d8b86 100644 --- a/tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php @@ -206,6 +206,19 @@ public function testClassExists(): void ]); } + #[RequiresPhp('>= 8.0.0')] + public function testEnumExists(): void + { + $this->phpVersion = PHP_VERSION_ID; + $this->analyse([__DIR__ . '/data/enum-exists.php'], [ + [ + 'Class UnknownEnum\Foo not found.', + 7, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + ]); + } + public static function dataClassConstantOnExpression(): array { return [ diff --git a/tests/PHPStan/Rules/Classes/data/enum-exists.php b/tests/PHPStan/Rules/Classes/data/enum-exists.php new file mode 100644 index 0000000000..c217c2eb1e --- /dev/null +++ b/tests/PHPStan/Rules/Classes/data/enum-exists.php @@ -0,0 +1,8 @@ +