From 37a500eaf49c9fac31b7c403d38a0f34abf40186 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 24 May 2026 15:21:44 +0200 Subject: [PATCH] Code around enum_exists() does not complain about nonexistent classes --- src/Analyser/MutatingScope.php | 4 ++++ .../PHPStan/Rules/Classes/ClassConstantRuleTest.php | 13 +++++++++++++ tests/PHPStan/Rules/Classes/data/enum-exists.php | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/PHPStan/Rules/Classes/data/enum-exists.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index f7de71487c6..3b98fa813a3 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 9c128b1f093..7e2e6d8b86e 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 00000000000..c217c2eb1e3 --- /dev/null +++ b/tests/PHPStan/Rules/Classes/data/enum-exists.php @@ -0,0 +1,8 @@ +