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
13 changes: 12 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,10 @@ private function processStmtNode(
$loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $nodeCallback, ExpressionContext::createTopLevel())->getScope();
}
$finalScope = $finalScope->generalizeWith($loopScope);

$alwaysIterates = TrinaryLogic::createFromBoolean($context->isTopLevel());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something fail if you said $alwaysIterates = TrinaryLogic::createYes() here?

Copy link
Contributor Author

@herndlm herndlm Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, apparently not. this comes basically from the other loops which have code like the following

$alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel();

but tbh, I didn't fully understand what the level here changes or does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense to be consistent.

This condition asks about whether it's the "analysis iteration", or one of the "let's find out if types stabilize in the loop before we generalize them" that's done for all loops in a do-while like this:

do {
$prevScope = $bodyScope;
$bodyScope = $bodyScope->mergeWith($initScope);
if ($lastCondExpr !== null) {
$bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, static function (): void {
}, ExpressionContext::createDeep())->getTruthyScope();
}
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
}, $context->enterDeep())->filterOutLoopExitPoints();
$bodyScope = $bodyScopeResult->getScope();
foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) {
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
}
foreach ($stmt->loop as $loopExpr) {
$exprResult = $this->processExprNode($stmt, $loopExpr, $bodyScope, static function (): void {
}, ExpressionContext::createTopLevel());
$bodyScope = $exprResult->getScope();
$hasYield = $hasYield || $exprResult->hasYield();
$throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints());
}
if ($bodyScope->equals($prevScope)) {
break;
}
if ($count >= self::GENERALIZE_AFTER_ITERATION) {
$bodyScope = $prevScope->generalizeWith($bodyScope);
}
$count++;
} while ($count < self::LOOP_SCOPE_ITERATIONS);

if ($lastCondExpr !== null) {
$alwaysIterates = $alwaysIterates->and($finalScope->getType($lastCondExpr)->toBoolean()->isTrue());
$finalScope = $finalScope->filterByFalseyValue($lastCondExpr);
}

Expand All @@ -1411,10 +1414,18 @@ private function processStmtNode(
}
}

if ($alwaysIterates->yes()) {
$isAlwaysTerminating = count($finalScopeResult->getExitPointsByType(Break_::class)) === 0;
} elseif ($isIterableAtLeastOnce->yes()) {
$isAlwaysTerminating = $finalScopeResult->isAlwaysTerminating();
} else {
$isAlwaysTerminating = false;
}

return new StatementResult(
$finalScope,
$finalScopeResult->hasYield() || $hasYield,
false/* $finalScopeResult->isAlwaysTerminating() && $isAlwaysIterable*/,
$isAlwaysTerminating,
$finalScopeResult->getExitPointsForOuterLoop(),
array_merge($throwPoints, $finalScopeResult->getThrowPoints()),
array_merge($impurePoints, $finalScopeResult->getImpurePoints()),
Expand Down
126 changes: 125 additions & 1 deletion tests/PHPStan/Analyser/StatementResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,130 @@ public function dataIsAlwaysTerminating(): array
'while (true) { break; }',
false,
],
[
'while (true) { exit; }',
true,
],
[
'while (true) { while (true) { } }',
true,
],
[
'while (true) { while (true) { return; } }',
true,
],
[
'while (true) { while (true) { break; } }',
true,
],
[
'while (true) { while (true) { exit; } }',
true,
],
[
'while (true) { while (true) { break 2; } }',
false,
],
[
'while (true) { while ($x) { } }',
true,
],
[
'while (true) { while ($x) { return; } }',
true,
],
[
'while (true) { while ($x) { break; } }',
true,
],
[
'while (true) { while ($x) { exit; } }',
true,
],
[
'while (true) { while ($x) { break 2; } }',
false,
],
[
'for (;;) { }',
true,
],
[
'for (;;) { return; }',
true,
],
[
'for (;;) { break; }',
false,
],
[
'for (;;) { exit; }',
true,
],
[
'for (;;) { for (;;) { } }',
true,
],
[
'for (;;) { for (;;) { return; } }',
true,
],
[
'for (;;) { for (;;) { break; } }',
true,
],
[
'for (;;) { for (;;) { exit; } }',
true,
],
[
'for (;;) { for (;;) { break 2; } }',
false,
],
[
'for (;;) { for ($i = 0; $i< 5; $i++) { } }',
true,
],
[
'for (;;) { for ($i = 0; $i< 5; $i++) { return; } }',
true,
],
[
'for (;;) { for ($i = 0; $i< 5; $i++) { break; } }',
true,
],
[
'for (;;) { for ($i = 0; $i< 5; $i++) { exit; } }',
true,
],
[
'for (;;) { for ($i = 0; $i< 5; $i++) { break 2; } }',
false,
],
[
'for ($i = 0; $i < 5;) { }',
true,
],
[
'for ($i = 0; $i < 5; $i--) { }',
true,
],
[
'for (; 0, 1;) { }',
true,
],
[
'for (; 1, 0;) { }',
false,
],
[
'for (; "", "a";) { }',
true,
],
[
'for (; "a", "";) { }',
false,
],
[
'do { } while (doFoo());',
false,
Expand Down Expand Up @@ -231,7 +355,7 @@ public function dataIsAlwaysTerminating(): array
],
[
'for ($i = 0; $i < 10; $i++) { return; }',
false, // will be true with range types
true,
],
[
'for ($i = 0; $i < 0; $i++) { return; }',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public function testStrictComparison(): void
140,
],
[
'Strict comparison using !== between StrictComparison\Foo|null and 1 will always evaluate to true.',
154,
'Strict comparison using === between non-empty-array and null will always evaluate to false.',
150,
],
[
'Strict comparison using === between non-empty-array and null will always evaluate to false.',
164,
'Strict comparison using !== between StrictComparison\Foo|null and 1 will always evaluate to true.',
161,
],
[
'Strict comparison using !== between StrictComparison\Node|null and false will always evaluate to true.',
Expand Down Expand Up @@ -352,7 +352,7 @@ public function testStrictComparisonWithoutAlwaysTrue(): void
],
[
'Strict comparison using === between non-empty-array and null will always evaluate to false.',
164,
150,
],
[
'Strict comparison using === between 1 and 2 will always evaluate to false.',
Expand Down
14 changes: 7 additions & 7 deletions tests/PHPStan/Rules/Comparison/data/strict-comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function whileWithTypeChange()

public function forWithTypeChange()
{
for (; $val = $this->returnArray();) {
if ($val === null) {

}
$val = null;
}

$foo = null;
for (;;) {
if ($foo !== null) {
Expand All @@ -159,13 +166,6 @@ public function forWithTypeChange()
$foo = new self();
}
}

for (; $val = $this->returnArray();) {
if ($val === null) {

}
$val = null;
}
}

private function returnArray(): array
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,22 @@ public function testBug9309(): void
$this->analyse([__DIR__ . '/data/bug-9309.php'], []);
}

public function testBug6807(): void
{
$this->checkExplicitMixedMissingReturn = true;
$this->analyse([__DIR__ . '/data/bug-6807.php'], []);
}

public function testBug8463(): void
{
$this->checkExplicitMixedMissingReturn = true;
$this->analyse([__DIR__ . '/data/bug-8463.php'], []);
}

public function testBug9374(): void
{
$this->checkExplicitMixedMissingReturn = true;
$this->analyse([__DIR__ . '/data/bug-9374.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-6807.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug6807;

/** @return int */
function test()
{
for ($attempts = 0; ; $attempts++)
{
if ($attempts > 5)
throw new Exception();

if (rand() == 1)
return 5;
}
}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-8463.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug8463;

function f1() : int
{
while(true)
{
if(rand() === rand())
{
return 1;
}
}
}


function f2() : int
{
for(;;)
{
if(rand() === rand())
{
return 1;
}
}
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-9374.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Bug9374;

function bar(): string {
for ($i = 0; ; ++$i)
return "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function () use (&$errorHandler) {
$onlyInIf = 1;
}

for ($forI = 0; $forI < 10, $anotherVariableFromForCond = 1; $forI++, $forJ = $forI) {
for ($forI = 0; $anotherVariableFromForCond = 1, $forI < 10; $forI++, $forJ = $forI) {

}

Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Variables/data/defined-variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function () {

}

for ($forI = 0; $forI < 10, $forK = 5; $forI++, $forK++, $forJ = $forI) {
for ($forI = 0; $forK = 5, $forI < 10; $forI++, $forK++, $forJ = $forI) {
echo $forI;
}

Expand Down Expand Up @@ -322,7 +322,7 @@ function () {
include($fileB='includeB.php');
echo $fileB;

for ($forLoopVariableInit = 0; $forLoopVariableInit < 5; $forLoopVariableInit = $forLoopVariable, $anotherForLoopVariable = 1) {
for ($forLoopVariableInit = 0; $forLoopVariableInit < 5 && rand(0, 1); $forLoopVariableInit = $forLoopVariable, $anotherForLoopVariable = 1) {
$forLoopVariable = 2;
}
echo $anotherForLoopVariable;
Expand Down Expand Up @@ -357,7 +357,7 @@ function () {

}

for (; $forVariableUsedAndThenDefined && $forVariableUsedAndThenDefined = 1;) {
for (; $forVariableUsedAndThenDefined && $forVariableUsedAndThenDefined = 1 && rand(0, 1);) {

}

Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ parameters:
count: 1
path: PHP-Parser/lib/PhpParser/ParserAbstract.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: PHP-Parser/lib/PhpParser/ParserAbstract.php

-
message: "#^Variable \\$action might not be defined\\.$#"
count: 1
Expand Down
Loading