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
14 changes: 13 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4112,7 +4112,19 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
}

if ($gotGreater && $gotSmaller) {
$resultTypes[] = new IntegerType();
$newMin = $min;
$newMax = $max;
foreach ($constantIntegers['b'] as $int) {
if ($int->getValue() < $newMin) {
$newMin = $int->getValue();
}
if ($int->getValue() <= $newMax) {
continue;
}

$newMax = $int->getValue();
}
$resultTypes[] = IntegerRangeType::fromInterval($newMin, $newMax);
} elseif ($gotGreater) {
$resultTypes[] = IntegerRangeType::fromInterval($min, null);
} elseif ($gotSmaller) {
Expand Down
71 changes: 71 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12163.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace Bug12163;

use function PHPStan\Testing\assertType;

class Test
{
public function iterateRowColumnIndicesIncrementing(int $rows, int $columns): void
{
if ($rows < 1 || $columns < 1) return;
$size = $rows * $columns;

$rowIndex = 0;
$columnIndex = 0;
for ($i = 0; $i < $size; $i++) {
assertType('int<0, max>', $rowIndex);
assertType('int<0, max>', $columnIndex);
if ($columnIndex < $columns) {
$columnIndex++;
} else {
$columnIndex = 0;
$rowIndex++;
}
}
}
}

class Test2
{
public function iterateRowColumnIndicesDecrementing(int $rows, int $columns): void
{
if ($rows < 1 || $columns < 1) return;
$size = $rows * $columns;

$rowIndex = 0;
$columnIndex = 0;
for ($i = 0; $i < $size; $i++) {
assertType('0', $rowIndex);
assertType('int<min, 0>', $columnIndex);
if ($columnIndex < $columns) {
$columnIndex--;
} else {
$columnIndex = 0;
$rowIndex++;
}
}
}
}

class Test3
{
/**
* @param int<0, 30> $columnIndex
*/
public function iterateRowColumnIndicesDecrementing(int $rows, int $columns, int $columnIndex): void
{
if ($rows < 1 || $columns < 1) return;
$size = $rows * $columns;

for ($i = 0; $i < $size; $i++) {
assertType('int<min, 30>', $columnIndex);
if ($columnIndex < 3) {
$columnIndex--;
} else {
$columnIndex = 0;
}
assertType('int<min, 1>', $columnIndex);
}
}
}
Loading