|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug12163; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class Test |
| 8 | +{ |
| 9 | + public function iterateRowColumnIndicesIncrementing(int $rows, int $columns): void |
| 10 | + { |
| 11 | + if ($rows < 1 || $columns < 1) return; |
| 12 | + $size = $rows * $columns; |
| 13 | + |
| 14 | + $rowIndex = 0; |
| 15 | + $columnIndex = 0; |
| 16 | + for ($i = 0; $i < $size; $i++) { |
| 17 | + assertType('int<0, max>', $rowIndex); |
| 18 | + assertType('int<0, max>', $columnIndex); |
| 19 | + if ($columnIndex < $columns) { |
| 20 | + $columnIndex++; |
| 21 | + } else { |
| 22 | + $columnIndex = 0; |
| 23 | + $rowIndex++; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class Test2 |
| 30 | +{ |
| 31 | + public function iterateRowColumnIndicesDecrementing(int $rows, int $columns): void |
| 32 | + { |
| 33 | + if ($rows < 1 || $columns < 1) return; |
| 34 | + $size = $rows * $columns; |
| 35 | + |
| 36 | + $rowIndex = 0; |
| 37 | + $columnIndex = 0; |
| 38 | + for ($i = 0; $i < $size; $i++) { |
| 39 | + assertType('0', $rowIndex); // `0`, because the IF in line 41 is always TRUE |
| 40 | + assertType('int<min, 0>', $columnIndex); |
| 41 | + if ($columnIndex < $columns) { |
| 42 | + $columnIndex--; |
| 43 | + } else { |
| 44 | + $columnIndex = 0; |
| 45 | + $rowIndex++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class Test3 |
| 52 | +{ |
| 53 | + /** |
| 54 | + * @param int<0, 30> $columnIndex |
| 55 | + */ |
| 56 | + public function iterateRowColumnIndicesDecrementing(int $rows, int $columns, int $columnIndex): void |
| 57 | + { |
| 58 | + if ($rows < 1 || $columns < 1) return; |
| 59 | + $size = $rows * $columns; |
| 60 | + |
| 61 | + for ($i = 0; $i < $size; $i++) { |
| 62 | + assertType('int<min, 30>', $columnIndex); |
| 63 | + if ($columnIndex < 3) { |
| 64 | + $columnIndex--; |
| 65 | + } else { |
| 66 | + $columnIndex = 0; |
| 67 | + } |
| 68 | + assertType('int<min, 1>', $columnIndex); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class Test4 |
| 74 | +{ |
| 75 | + /** |
| 76 | + * @param int<0, 10> $columnIndex |
| 77 | + */ |
| 78 | + public function integerRangeBothDirections(int $rows, int $columns, int $columnIndex): void |
| 79 | + { |
| 80 | + if ($rows < 1 || $columns < 1) return; |
| 81 | + $size = $rows * $columns; |
| 82 | + |
| 83 | + for ($i = 0; $i < $size; $i++) { |
| 84 | + assertType('int<0, max>', $columnIndex); |
| 85 | + if ($columnIndex < $columns) { |
| 86 | + $columnIndex++; |
| 87 | + } else { |
| 88 | + $columnIndex--; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +class Test5 |
| 95 | +{ |
| 96 | + /** |
| 97 | + * @param int<0, 10> $columnIndex |
| 98 | + */ |
| 99 | + public function integerRangeOnlyGreater(int $rows, int $columns, int $columnIndex): void |
| 100 | + { |
| 101 | + if ($rows < 1 || $columns < 1) return; |
| 102 | + $size = $rows * $columns; |
| 103 | + |
| 104 | + for ($i = 0; $i < $size; $i++) { |
| 105 | + assertType('int<0, max>', $columnIndex); |
| 106 | + if ($columnIndex < $columns) { |
| 107 | + $columnIndex++; |
| 108 | + } else { |
| 109 | + $columnIndex = 5; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class Test6 |
| 116 | +{ |
| 117 | + /** |
| 118 | + * @param int<5, 10> $value |
| 119 | + */ |
| 120 | + public function integerRangeGrowsBothDirections(int $value): void |
| 121 | + { |
| 122 | + for ($i = 0; $i < 10; $i++) { |
| 123 | + assertType('int<min, 10>', $value); |
| 124 | + if ($value > 0) { |
| 125 | + $value = $value - 2; |
| 126 | + } else { |
| 127 | + $value = $value + 3; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class Bug12163 |
| 134 | +{ |
| 135 | + /** |
| 136 | + * @param non-negative-int $value |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + private function checkNonNegative(int $value): void |
| 140 | + { |
| 141 | + sleep(1); |
| 142 | + } |
| 143 | + |
| 144 | + public function iterateRowColumnIndices(int $rows, int $columns): void |
| 145 | + { |
| 146 | + if ($rows < 1 || $columns < 1) return; |
| 147 | + $size = $rows * $columns; |
| 148 | + |
| 149 | + $rowIndex = 0; |
| 150 | + $columnIndex = 0; |
| 151 | + for ($i = 0; $i < $size; $i++) { |
| 152 | + $this->checkNonNegative($rowIndex); |
| 153 | + $this->checkNonNegative($columnIndex); |
| 154 | + if ($columnIndex < $columns) { |
| 155 | + $columnIndex++; |
| 156 | + } else { |
| 157 | + $columnIndex = 0; |
| 158 | + $rowIndex++; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments