Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '\\'))),
]);
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Classes/data/enum-exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function () {
if (enum_exists(\UnknownEnum\Foo::class)) {
echo \UnknownEnum\Foo::class;
}
echo \UnknownEnum\Foo::class;
};
Loading