|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug13705; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +function whileLoop(): void |
| 8 | +{ |
| 9 | + $quantity = random_int(1, 42); |
| 10 | + $codes = []; |
| 11 | + while (count($codes) < $quantity) { |
| 12 | + assertType('list<non-empty-string>', $codes); |
| 13 | + $code = random_bytes(16); |
| 14 | + if (!in_array($code, $codes, true)) { |
| 15 | + $codes[] = $code; |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @param list<string> $arr |
| 22 | + * @param int<2, 5> $boundedRange |
| 23 | + * @param int<2, max> $unboundedMaxRange |
| 24 | + * @param int<min, 5> $unboundedMinRange |
| 25 | + */ |
| 26 | +function countLessThanRange(array $arr, int $boundedRange, int $unboundedMaxRange, int $unboundedMinRange): void |
| 27 | +{ |
| 28 | + // count($arr) < $range → inverted to NOT($range <= count($arr)) |
| 29 | + // Inner: orEqual=true, false context → falsey + max !== null + orEqual (branch 1) |
| 30 | + // Else: orEqual=true, true context → truthy + min !== null + orEqual (branch 3) |
| 31 | + if (count($arr) < $boundedRange) { |
| 32 | + assertType('list<string>', $arr); |
| 33 | + } else { |
| 34 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr); |
| 35 | + } |
| 36 | + |
| 37 | + // count($arr) < unbounded max range → falsey + max is null → fallback via min (branch 3/4) |
| 38 | + if (count($arr) < $unboundedMaxRange) { |
| 39 | + assertType('list<string>', $arr); |
| 40 | + } else { |
| 41 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr); |
| 42 | + } |
| 43 | + |
| 44 | + // count($arr) < unbounded min range → fallback branch (min is null) |
| 45 | + if (count($arr) < $unboundedMinRange) { |
| 46 | + assertType('list<string>', $arr); |
| 47 | + } else { |
| 48 | + assertType('list<string>', $arr); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * @param list<string> $arr |
| 54 | + * @param int<2, 5> $boundedRange |
| 55 | + */ |
| 56 | +function countLessThanOrEqualRange(array $arr, int $boundedRange): void |
| 57 | +{ |
| 58 | + // count($arr) <= $range → inverted to NOT($range < count($arr)) |
| 59 | + // Inner: orEqual=false, false context → falsey + max !== null + !orEqual (branch 2) |
| 60 | + // Else: orEqual=false, true context → truthy + min !== null + !orEqual (branch 4) |
| 61 | + if (count($arr) <= $boundedRange) { |
| 62 | + assertType('list<string>', $arr); |
| 63 | + } else { |
| 64 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * @param list<string> $arr |
| 70 | + * @param int<2, 5> $boundedRange |
| 71 | + */ |
| 72 | +function rangeGreaterThanOrEqualCount(array $arr, int $boundedRange): void |
| 73 | +{ |
| 74 | + // $range >= count($arr) → same as count($arr) <= $range |
| 75 | + if ($boundedRange >= count($arr)) { |
| 76 | + assertType('list<string>', $arr); |
| 77 | + } else { |
| 78 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @param list<string> $arr |
| 84 | + * @param int<2, 5> $boundedRange |
| 85 | + */ |
| 86 | +function rangeLessThanOrEqualCount(array $arr, int $boundedRange): void |
| 87 | +{ |
| 88 | + // $range <= count($arr) → direct, orEqual=true |
| 89 | + // True context: truthy + orEqual + min !== null (branch 3) |
| 90 | + // False context: falsey + orEqual + max !== null (branch 1) |
| 91 | + if ($boundedRange <= count($arr)) { |
| 92 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr); |
| 93 | + } else { |
| 94 | + assertType('list<string>', $arr); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @param list<string> $arr |
| 100 | + * @param int<2, 5> $boundedRange |
| 101 | + */ |
| 102 | +function rangeLessThanCount(array $arr, int $boundedRange): void |
| 103 | +{ |
| 104 | + // $range < count($arr) → direct, orEqual=false |
| 105 | + // True context: truthy + !orEqual + min !== null (branch 4) |
| 106 | + // False context: falsey + !orEqual + max !== null (branch 2) |
| 107 | + if ($boundedRange < count($arr)) { |
| 108 | + assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr); |
| 109 | + } else { |
| 110 | + assertType('list<string>', $arr); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function whileLoopOriginal(int $length, int $quantity): void |
| 115 | +{ |
| 116 | + if ($length < 8) { |
| 117 | + throw new \InvalidArgumentException(); |
| 118 | + } |
| 119 | + $codes = []; |
| 120 | + while ($quantity >= 1 && count($codes) < $quantity) { |
| 121 | + $code = ''; |
| 122 | + for ($i = 0; $i < $length; $i++) { |
| 123 | + $code .= 'x'; |
| 124 | + } |
| 125 | + if (!in_array($code, $codes, true)) { |
| 126 | + $codes[] = $code; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +class HelloWorld |
| 132 | +{ |
| 133 | + private const MIN_LENGTH = 8; |
| 134 | + |
| 135 | + /** |
| 136 | + * @return list<non-empty-string> |
| 137 | + */ |
| 138 | + public function generatePlainRecoveryCodes(int $length = 8, int $quantity = 8): array |
| 139 | + { |
| 140 | + if ($length < self::MIN_LENGTH) { |
| 141 | + throw new \InvalidArgumentException( |
| 142 | + $length . ' is not allowed as length for recovery codes. Must be at least ' . self::MIN_LENGTH, |
| 143 | + 1613666803 |
| 144 | + ); |
| 145 | + } |
| 146 | + $codes = []; |
| 147 | + while ($quantity >= 1 && count($codes) < $quantity) { |
| 148 | + $code = ''; |
| 149 | + for ($i = 0; $i < $length; $i++) { |
| 150 | + $code .= 'x'; |
| 151 | + } |
| 152 | + if (!in_array($code, $codes, true)) { |
| 153 | + $codes[] = $code; |
| 154 | + } |
| 155 | + } |
| 156 | + return $codes; |
| 157 | + } |
| 158 | +} |
0 commit comments