Skip to content

Commit f21b1d3

Browse files
committed
Support nested assigns
1 parent 092d7b6 commit f21b1d3

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,22 @@ public function findSpecifiedType(
282282
$assignedInCallVars = [];
283283
if ($node instanceof Expr\CallLike) {
284284
foreach ($node->getArgs() as $arg) {
285-
if (!$arg->value instanceof Expr\Assign) {
286-
continue;
285+
$expr = $arg->value;
286+
while ($expr instanceof Expr\Assign) {
287+
$expr = $expr->expr;
287288
}
289+
$assignedExpr = $expr;
290+
291+
$expr = $arg->value;
292+
while ($expr instanceof Expr\Assign) {
293+
$assignedInCallVars[] = new Expr\Assign(
294+
$expr->var,
295+
$assignedExpr,
296+
$expr->getAttributes()
297+
);
288298

289-
$assignedInCallVars[] = $arg->value;
299+
$expr = $expr->expr;
300+
}
290301
}
291302
}
292303
foreach ($sureTypes as $sureType) {

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,21 @@ public function testBug12087(): void
10711071
]);
10721072
}
10731073

1074+
#[RequiresPhp('>= 8.1')]
1075+
public function testBug12087c(): void
1076+
{
1077+
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
1078+
1079+
$this->treatPhpDocTypesAsCertain = true;
1080+
$this->analyse([__DIR__ . '/data/bug-12087c.php'], [
1081+
[
1082+
'Call to function is_null() with null will always evaluate to true.',
1083+
17,
1084+
$tipText,
1085+
],
1086+
]);
1087+
}
1088+
10741089
public function testBug9666(): void
10751090
{
10761091
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

tests/PHPStan/Rules/Comparison/data/bug-12087c.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ enum Button: int
99
case Off = 0;
1010
}
1111

12-
function doFoo() {
13-
$value = 10;
14-
15-
is_null($foo = $value = Button::tryFrom($value));
16-
}
17-
18-
function doFoo2() {
12+
function doFoo()
13+
{
14+
$foo = 'abc';
1915
$value = 10;
2016

21-
is_null($foo ??= Button::tryFrom($value));
17+
is_null($value = $foo = Button::tryFrom($value));
2218
}

0 commit comments

Comments
 (0)