Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for endless loops #3573

Open
wants to merge 6 commits into
base: 1.12.x
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ private function processStmtNode(
}

$bodyScope = $initScope;
$alwaysIterates = $stmt->cond === [] && $context->isTopLevel();
$isIterableAtLeastOnce = TrinaryLogic::createYes();
foreach ($stmt->cond as $condExpr) {
$condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void {
Expand Down Expand Up @@ -1410,10 +1411,18 @@ private function processStmtNode(
}
}

if ($alwaysIterates) {
$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,
Copy link
Member

Choose a reason for hiding this comment

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

I feel like I'm missing $finalScopeResult->isAlwaysTerminating() here. It's related to the failing test we talk above.

Copy link
Member

Choose a reason for hiding this comment

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

Also please update the E2E test so it's not failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adapted I think. not sure if I did it the right way, but I manually checked-out PHP-Parser and manually run PHPStan like in the test and let it update the baseline there.

$finalScopeResult->getExitPointsForOuterLoop(),
array_merge($throwPoints, $finalScopeResult->getThrowPoints()),
array_merge($impurePoints, $finalScopeResult->getImpurePoints()),
Expand Down
102 changes: 101 additions & 1 deletion tests/PHPStan/Analyser/StatementResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,106 @@ 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; }',
herndlm marked this conversation as resolved.
Show resolved Hide resolved
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,
],
[
'do { } while (doFoo());',
false,
Expand Down Expand Up @@ -231,7 +331,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 @@ -107,12 +107,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 @@ -333,7 +333,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 "";
}
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