Skip to content
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
36 changes: 32 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1502,9 +1502,10 @@ public function processStmtNode(
$bodyScope = $bodyScope->mergeWith($scope);
$bodyScopeMaybeRan = $bodyScope;
$storage = $originalStorage;
$bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope();
$condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep());
$bodyScope = $condResult->getTruthyScope();
$finalScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, $storage, $nodeCallback, $context)->filterOutLoopExitPoints();
$finalScope = $finalScopeResult->getScope()->filterByFalseyValue($stmt->cond);
$finalScope = $this->getWhileLoopExitScope($stmt->cond, $condResult, $finalScopeResult->getScope());

$alwaysIterates = false;
$neverIterates = false;
Expand Down Expand Up @@ -1727,9 +1728,11 @@ public function processStmtNode(
$bodyScope = $bodyScope->mergeWith($initScope);

$alwaysIterates = TrinaryLogic::createFromBoolean($context->isTopLevel());
$lastCondResult = null;
if ($lastCondExpr !== null) {
$alwaysIterates = $alwaysIterates->and($bodyScope->getType($lastCondExpr)->toBoolean()->isTrue());
$bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope();
$lastCondResult = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep());
$bodyScope = $lastCondResult->getTruthyScope();
$bodyScope = $this->inferForLoopExpressions($stmt, $lastCondExpr, $bodyScope);
}

Expand All @@ -1746,7 +1749,7 @@ public function processStmtNode(
$finalScope = $finalScope->generalizeWith($loopScope);

if ($lastCondExpr !== null) {
$finalScope = $finalScope->filterByFalseyValue($lastCondExpr);
$finalScope = $this->getWhileLoopExitScope($lastCondExpr, $lastCondResult, $finalScope);
}

$breakExitPoints = $finalScopeResult->getExitPointsByType(Break_::class);
Expand Down Expand Up @@ -4819,6 +4822,31 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding):
return $stmts;
}

private function conditionHasDirectIncDec(Expr $cond): bool
{
if (!$cond instanceof BinaryOp\Smaller && !$cond instanceof BinaryOp\SmallerOrEqual && !$cond instanceof BinaryOp\Greater && !$cond instanceof BinaryOp\GreaterOrEqual) {
return false;
}

return $cond->left instanceof Expr\PreInc
|| $cond->left instanceof Expr\PreDec
|| $cond->left instanceof Expr\PostInc
|| $cond->left instanceof Expr\PostDec
|| $cond->right instanceof Expr\PreInc
|| $cond->right instanceof Expr\PreDec
|| $cond->right instanceof Expr\PostInc
|| $cond->right instanceof Expr\PostDec;
}

private function getWhileLoopExitScope(Expr $cond, ExpressionResult $condResult, MutatingScope $bodyOutputScope): MutatingScope
{
if ($this->conditionHasDirectIncDec($cond)) {
return $condResult->getFalseyScope();
}

return $bodyOutputScope->filterByFalseyValue($cond);
}

private function inferForLoopExpressions(For_ $stmt, Expr $lastCondExpr, MutatingScope $bodyScope): MutatingScope
{
// infer $items[$i] type from for ($i = 0; $i < count($items); $i++) {...}
Expand Down
82 changes: 82 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7230.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types = 1);

namespace Bug7230;

use function PHPStan\Testing\assertType;

function preIncWhile(): void
{
$counter = 0;
while (++$counter < 100) {
assertType('int<1, 99>', $counter);
}
assertType('int<100, max>', $counter);

if ($counter === 100) {
assertType('100', $counter);
}
}

function preIncWhileWithBody(): void
{
$tmp = '';
$counter = 0;
while (++$counter < 100) {
$tmp = 'tmp_' . (string) $counter;
}
assertType('int<100, max>', $counter);

if ($counter === 100) {
$tmp = 'tmp_hardcoded';
}

assertType("''|(literal-string&lowercase-string&non-falsy-string)", $tmp);
}

function postIncWhile(): void
{
$counter = 0;
while ($counter++ < 100) {
assertType('int<1, 100>', $counter);
}
assertType('int<101, max>', $counter);

if ($counter === 101) {
assertType('101', $counter);
}
}

function preDecWhile(): void
{
$counter = 100;
while (--$counter > 0) {
assertType('int<1, 99>', $counter);
}
assertType('int<min, 0>', $counter);
}

function preIncFor(): void
{
$counter = 0;
for (; ++$counter < 5; ) {
}
assertType('int<5, max>', $counter);
}

function postIncFor(): void
{
$counter = 0;
for (; $counter++ < 5; ) {
}
assertType('int<6, max>', $counter);
}

function preDecFor(): void
{
$counter = 10;
for (; --$counter > 0; ) {
}
assertType('int<min, 0>', $counter);
}
Loading