Skip to content

Commit 2f976a8

Browse files
authored
Code around enum_exists() does not complain about nonexistent classes (#5751)
1 parent 244b8d4 commit 2f976a8

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ private function rememberConstructorExpressions(array $currentExpressionTypes):
309309
if ($expr instanceof FuncCall) {
310310
if (
311311
!$expr->name instanceof Name
312+
// interface_exists() etc. imply class_exists() therefore not listed here
312313
|| !in_array($expr->name->name, ['class_exists', 'function_exists'], true)
313314
) {
314315
continue;
@@ -1376,10 +1377,13 @@ public function isInClassExists(string $className): bool
13761377
'class_exists',
13771378
'interface_exists',
13781379
'trait_exists',
1380+
'enum_exists',
13791381
], true)) {
13801382
return true;
13811383
}
13821384
}
1385+
1386+
// interface_exists() etc. imply class_exists() therefore not listed here
13831387
$expr = new FuncCall(new FullyQualified('class_exists'), [
13841388
new Arg(new String_(ltrim($className, '\\'))),
13851389
]);

tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ public function testClassExists(): void
206206
]);
207207
}
208208

209+
#[RequiresPhp('>= 8.0.0')]
210+
public function testEnumExists(): void
211+
{
212+
$this->phpVersion = PHP_VERSION_ID;
213+
$this->analyse([__DIR__ . '/data/enum-exists.php'], [
214+
[
215+
'Class UnknownEnum\Foo not found.',
216+
7,
217+
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
218+
],
219+
]);
220+
}
221+
209222
public static function dataClassConstantOnExpression(): array
210223
{
211224
return [
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
function () {
4+
if (enum_exists(\UnknownEnum\Foo::class)) {
5+
echo \UnknownEnum\Foo::class;
6+
}
7+
echo \UnknownEnum\Foo::class;
8+
};

0 commit comments

Comments
 (0)