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
8 changes: 4 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ public function processStmtNode(

$originalStorage = $storage;
$unrolledEndScope = null;
if ($context->isTopLevel()) {
if ($context->shouldRunLoopConvergence()) {
$storage = $originalStorage->duplicate();

$originalScope = $this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope;
Expand Down Expand Up @@ -1673,7 +1673,7 @@ public function processStmtNode(
}
$bodyScope = $condResult->getTruthyScope();

if ($context->isTopLevel()) {
if ($context->shouldRunLoopConvergence()) {
$count = 0;
do {
$prevScope = $bodyScope;
Expand Down Expand Up @@ -1766,7 +1766,7 @@ public function processStmtNode(
$impurePoints = [];
$originalStorage = $storage;

if ($context->isTopLevel()) {
if ($context->shouldRunLoopConvergence()) {
do {
$prevScope = $bodyScope;
$bodyScope = $bodyScope->mergeWith($scope);
Expand Down Expand Up @@ -1886,7 +1886,7 @@ public function processStmtNode(
}
}

if ($context->isTopLevel()) {
if ($context->shouldRunLoopConvergence()) {
$count = 0;
do {
$prevScope = $bodyScope;
Expand Down
23 changes: 15 additions & 8 deletions src/Analyser/StatementContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
final class StatementContext
{

private const LOOP_CONVERGENCE_DEPTH_LIMIT = 2;

private function __construct(
private bool $isTopLevel,
private int $depth,
private int $foreachUnrollFactor = 1,
)
{
Expand All @@ -25,20 +27,25 @@ private function __construct(
*/
public static function createTopLevel(): self
{
return new self(true);
return new self(0);
}

/**
* @api
*/
public static function createDeep(): self
{
return new self(false);
return new self(self::LOOP_CONVERGENCE_DEPTH_LIMIT);
}

public function isTopLevel(): bool
{
return $this->isTopLevel;
return $this->depth === 0;
}

public function shouldRunLoopConvergence(): bool
{
return $this->depth < self::LOOP_CONVERGENCE_DEPTH_LIMIT;
}

public function getForeachUnrollFactor(): int
Expand All @@ -48,16 +55,16 @@ public function getForeachUnrollFactor(): int

public function enterDeep(): self
{
if ($this->isTopLevel) {
return new self(false, $this->foreachUnrollFactor);
if ($this->depth >= self::LOOP_CONVERGENCE_DEPTH_LIMIT) {
return $this;
}

return $this;
return new self($this->depth + 1, $this->foreachUnrollFactor);
}

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

}
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
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10666.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace Bug10666;

use function PHPStan\Testing\assertType;

function popInNestedLoop(int $n): string
{
$text = [];
while (empty($text)) {
$i = 0;
while ($i < $n) {
$text[] = 'x';
$i++;
}
assertType("list<'x'>", $text);
array_pop($text);
assertType("list<'x'>", $text);
}
assertType("non-empty-list<'x'>", $text);
return implode('', $text);
}

function shiftInNestedLoop(int $n): string
{
$text = [];
while (empty($text)) {
$i = 0;
while ($i < $n) {
$text[] = 'x';
$i++;
}
array_shift($text);
}
assertType("non-empty-list<'x'>", $text);
return implode('', $text);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/generalize-scope-recursive.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function doFoo(array $array, array $values)
}
}

assertType('array{}|array{foo?: array}', $data);
assertType('array{}|array{foo: array<array>}', $data);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/EmptyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ public function testBug7806(): void
$this->analyse([__DIR__ . '/data/bug-7806.php'], []);
}

public function testBug10666(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-10666.php'], []);
}

public function testBug14393(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
60 changes: 60 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10666.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Bug10666;

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

function withArrayShift(int $n): string
{
$text = [];
while (empty($text)) {
$i = 0;
while ($i < $n) {
$text[] = 'x';
$i++;
}
array_shift($text);
}
return implode('', $text);
}

function withDoWhile(int $n): string
{
$text = [];
while (empty($text)) {
$i = 0;
do {
$text[] = 'x';
$i++;
} while ($i < $n);
array_pop($text);
}
return implode('', $text);
}

function withForLoop(int $n): string
{
$text = [];
while (empty($text)) {
for ($i = 0; $i < $n; $i++) {
$text[] = 'x';
}
array_pop($text);
}
return implode('', $text);
}
108 changes: 108 additions & 0 deletions tests/bench/data/deeply-nested-loops.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php declare(strict_types = 1);

namespace BenchDeeplyNestedLoops;

class Foo
{
/**
* @param list<string> $items
*/
public function fiveLevelWhile(array $items): int
{
$count = 0;
while ($items !== []) {
$item = array_shift($items);
$parts = explode('.', $item);
while ($parts !== []) {
$part = array_shift($parts);
$chars = str_split($part);
while ($chars !== []) {
$char = array_shift($chars);
$codes = [ord($char)];
while ($codes !== []) {
$code = array_shift($codes);
$bits = [];
while ($code > 0) {
$bits[] = $code & 1;
$code >>= 1;
}
$count += count($bits);
}
}
}
}
return $count;
}

/**
* @param list<string> $items
*/
public function fiveLevelFor(array $items): int
{
$count = 0;
for ($a = 0; $a < count($items); $a++) {
$parts = explode('.', $items[$a]);
for ($b = 0; $b < count($parts); $b++) {
$chars = str_split($parts[$b]);
for ($c = 0; $c < count($chars); $c++) {
$codes = [ord($chars[$c])];
for ($d = 0; $d < count($codes); $d++) {
$code = $codes[$d];
for ($e = 0; $e < 8; $e++) {
$count += ($code >> $e) & 1;
}
}
}
}
}
return $count;
}

/**
* @param list<string> $items
*/
public function fiveLevelForeach(array $items): int
{
$count = 0;
foreach ($items as $item) {
$parts = explode('.', $item);
foreach ($parts as $part) {
$chars = str_split($part);
foreach ($chars as $char) {
$codes = [ord($char)];
foreach ($codes as $code) {
$bits = [];
foreach (range(0, 7) as $i) {
$bits[] = ($code >> $i) & 1;
}
$count += array_sum($bits);
}
}
}
}
return $count;
}

/**
* @param array<string, list<int>> $data
*/
public function mixedLoopTypes(array $data): int
{
$total = 0;
foreach ($data as $key => $values) {
$i = 0;
while ($i < count($values)) {
$n = $values[$i];
for ($j = 0; $j < $n; $j++) {
$k = $j;
do {
$total += $k;
$k--;
} while ($k > 0);
}
$i++;
}
}
return $total;
}
}
Loading