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: 33 additions & 3 deletions src/Analyser/StatementContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private function __construct(
private bool $resolveTemplateArguments = true,
private ?Type $expectedReturnType = null,
private ?Type $nativeExpectedReturnType = null,
private bool $convergesLoops = false,
)
{
}
Expand All @@ -48,6 +49,35 @@ public function isTopLevel(): bool
return $this->isTopLevel;
}

/**
* Whether loops walk their body until the types stabilize. Besides the
* top level, the loops nested in a convergence pass of a top-level loop
* do, and so do the loops nested in their final walks - otherwise a
* variable a nested loop changes over several iterations keeps the type
* of its first iteration in the outer loop's passes.
*/
public function shouldConvergeLoops(): bool
{
return $this->isTopLevel || $this->convergesLoops;
}

/**
* The context of a loop's convergence pass. The loops nested in a pass of
* a top-level loop converge themselves, their passes' nested loops are
* walked once.
*/
public function enterLoopPass(): self
{
if ($this->isTopLevel) {
return new self(false, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType, true);
}
if ($this->convergesLoops) {
return new self(false, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType);
}

return $this;
}

public function getForeachUnrollFactor(): int
{
return $this->foreachUnrollFactor;
Expand All @@ -64,7 +94,7 @@ public function withoutTemplateArgumentResolution(): self
return $this;
}

return new self($this->isTopLevel, $this->foreachUnrollFactor, false, $this->expectedReturnType, $this->nativeExpectedReturnType);
return new self($this->isTopLevel, $this->foreachUnrollFactor, false, $this->expectedReturnType, $this->nativeExpectedReturnType, $this->convergesLoops);
}

/**
Expand All @@ -78,7 +108,7 @@ public function withExpectedReturnType(?Type $expectedReturnType, ?Type $nativeE
return $this;
}

return new self($this->isTopLevel, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $expectedReturnType, $nativeExpectedReturnType);
return new self($this->isTopLevel, $this->foreachUnrollFactor, $this->resolveTemplateArguments, $expectedReturnType, $nativeExpectedReturnType, $this->convergesLoops);
}

public function getExpectedReturnType(): ?Type
Expand All @@ -102,7 +132,7 @@ public function enterDeep(): self

public function enterUnrolledForeach(int $totalKeys): self
{
return new self($this->isTopLevel, $this->foreachUnrollFactor * $totalKeys, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType);
return new self($this->isTopLevel, $this->foreachUnrollFactor * $totalKeys, $this->resolveTemplateArguments, $this->expectedReturnType, $this->nativeExpectedReturnType, $this->convergesLoops);
}

}
4 changes: 2 additions & 2 deletions src/Analyser/StmtHandler/DoWhileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function processStmt(
$prevEntryScope = null;
$previousPass = null;
$previousPassStorage = null;
if ($context->isTopLevel()) {
if ($context->shouldConvergeLoops()) {
$bodyIsReplayable = $nodeScopeResolver->isReplayableConvergenceBody($stmt, $stmt->stmts);
// each pass after the first re-walks only the statements whose
// entry state differs from the previous pass's (StatementsHandler::processLoopPassStmtNodes())
Expand All @@ -91,7 +91,7 @@ public function processStmt(
$scope->pushExpressionResultStorage($storage);
try {
$passRecord = null;
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterDeep()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterLoopPass()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$previousPass = $passRecord;
$previousPassStorage = $storage;
$backEdgeScope = $bodyScopeResult->getLoopBackEdgeScope();
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/StmtHandler/ForHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function processStmt(
}
}

if ($context->isTopLevel()) {
if ($context->shouldConvergeLoops()) {
$count = 0;
$prevEntryScope = null;
// each pass after the first re-walks only the statements whose
Expand Down Expand Up @@ -226,7 +226,7 @@ public function processStmt(
$passFlows[] = $passCondResult->getVariableFlow();
}
$passRecord = null;
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, new NoopNodeCallback(), $context->enterDeep()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, new NoopNodeCallback(), $context->enterLoopPass()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$previousPass = $passRecord;
$previousPassStorage = $storage;
$passFlows[] = $bodyScopeResult->getVariableFlow();
Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/StmtHandler/ForeachHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static function () use ($condResult, $emptyArrayType): Type {
$replayEntryScope = null;
$unrolledEndScope = null;
$unrolledTotalKeys = null;
if ($context->isTopLevel()) {
if ($context->shouldConvergeLoops()) {
$storage = $originalStorage->duplicate();

$originalScope = $nonEmptyIterateeScope;
Expand All @@ -213,7 +213,7 @@ static function () use ($condResult, $emptyArrayType): Type {
$foreachIterateeType = $iterateeResult->getType();
$foreachNativeIterateeType = $iterateeResult->getNativeType();
}
$unrolledResult = $this->tryProcessUnrolledConstantArrayForeach($nodeScopeResolver, $stmt, $originalScope, $originalStorage, $context, $foreachIterateeType, $foreachNativeIterateeType);
$unrolledResult = $context->isTopLevel() ? $this->tryProcessUnrolledConstantArrayForeach($nodeScopeResolver, $stmt, $originalScope, $originalStorage, $context, $foreachIterateeType, $foreachNativeIterateeType) : null;
if ($unrolledResult !== null) {
$bodyScope = $unrolledResult['bodyScope'];
$unrolledEndScope = $unrolledResult['endScope'];
Expand Down Expand Up @@ -250,7 +250,7 @@ static function () use ($condResult, $emptyArrayType): Type {
try {
$bodyScope = $this->enterForeach($nodeScopeResolver, $bodyScope, $storage, $originalScope, $stmt, $foreachIterateeType, $foreachNativeIterateeType, $nodeCallback);
$passRecord = null;
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterDeep()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterLoopPass()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$previousPass = $passRecord;
$previousPassStorage = $storage;
$backEdgeScope = $bodyScopeResult->getLoopBackEdgeScope();
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/StmtHandler/WhileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function processStmt(
$prevEntryScope = null;
$previousPass = null;
$previousPassStorage = null;
if ($context->isTopLevel()) {
if ($context->shouldConvergeLoops()) {
$count = 0;
$bodyIsReplayable = $nodeScopeResolver->isReplayableConvergenceBody($stmt, $stmt->stmts);
do {
Expand All @@ -119,7 +119,7 @@ public function processStmt(
$passCondResult = $nodeScopeResolver->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $condRecording, ExpressionContext::createDeep(resolveTemplateArguments: false));
$bodyScope = $passCondResult->getTruthyScope();
$passRecord = null;
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterDeep()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterLoopPass()->withoutTemplateArgumentResolution(), $previousPass, $passRecord)->filterOutLoopExitPoints();
$previousPass = $passRecord;
$previousPassStorage = $storage;
$backEdgeScope = $bodyScopeResult->getLoopBackEdgeScope();
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-10438.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function extract(SimpleXMLElement $data, string $type = 'Meta'): array
$meta[$key] = (string)$tag->{$valueName};
continue;
}
assertType('array<string, array{}|array{string}|string>', $meta);
assertType('array<string, list<string>|string>', $meta);
$meta[$key] = [];
assertType('array{}', $meta[$key]);
foreach ($tag->{$valueName} as $value) {
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10666.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug10666;

use function PHPStan\Testing\assertType;

class Foo
{
public function text(int $maxNbChars = 200): string
{
$text = [];
while (empty($text)) {
$size = 0;
while ($size < $maxNbChars) {
$text[] = 'x';
$size++;
}

assertType("list<'x'>", $text);
array_pop($text);
assertType("list<'x'>", $text);
}

return implode('', $text);
}
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14935.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug14935;

use function PHPStan\Testing\assertType;

/**
* @param list<list<float>> $groups
*/
function countRejected(array $groups): int
{
$rejected = 0;

foreach ($groups as $readings) {
$copy = [];
foreach ($readings as $reading) {
$copy[] = $reading;
}
assertType('list<float>', $copy);

if (count($copy) > 1) {
$rejected++;
}
}

assertType('int<0, max>', $rejected);

return $rejected;
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15420.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug15420;

use function PHPStan\Testing\assertType;

class Test {
/** @param string[] $cards */
public function f(array $cards): void
{
for ($i = 0; $i < 10; $i++) {
$seen = [];
$later = [];
foreach ($cards as $card) {
if (array_key_exists($card, $seen)) {
$later[] = $card;
continue;
}
$seen[$card] = true;
}
assertType('int<0, 9>', $i);
if ($later !== []) {
$cards = $later;
} else {
break;
}
}
}
}
Loading
Loading