Skip to content

Commit 39c396e

Browse files
committed
Remember class_exists() from constructor
1 parent ba01cb2 commit 39c396e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Diff for: src/Analyser/MutatingScope.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,14 @@ private function rememberConstructorExpressions(array $currentExpressionTypes):
303303
$expressionTypes = [];
304304
foreach ($currentExpressionTypes as $exprString => $expressionTypeHolder) {
305305
$expr = $expressionTypeHolder->getExpr();
306-
if ($expr instanceof PropertyFetch) {
306+
if ($expr instanceof FuncCall) {
307+
if (
308+
!$expr->name instanceof Name
309+
|| !in_array($expr->name->name, ['class_exists'], true)
310+
) {
311+
continue;
312+
}
313+
} elseif ($expr instanceof PropertyFetch) {
307314
if (
308315
!$expr->name instanceof Node\Identifier
309316
|| !$expr->var instanceof Variable

Diff for: tests/PHPStan/Rules/Classes/ExistingClassInInstanceOfRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ public function testBug7720(): void
8181
]);
8282
}
8383

84+
public function testRememberClassExistsFromConstructor(): void
85+
{
86+
$this->analyse([__DIR__ . '/data/remember-class-exists-from-constructor.php'], []);
87+
}
88+
8489
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php // lint >= 7.4
2+
3+
namespace RememberClassExistsFromConstructor;
4+
5+
use SomeUnknownClass;
6+
7+
class User
8+
{
9+
public function __construct(
10+
) {
11+
if (!class_exists('SomeUnknownClass')) {
12+
throw new \LogicException();
13+
}
14+
}
15+
16+
public function doFoo($m): bool
17+
{
18+
if ($m instanceof SomeUnknownClass) {
19+
return false;
20+
}
21+
return true;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)