|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bug14651; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | +use function str_contains; |
| 7 | +use function str_ends_with; |
| 8 | +use function str_starts_with; |
| 9 | + |
| 10 | +/** |
| 11 | + * @return list<string> |
| 12 | + */ |
| 13 | +function readLines(string $buffer): array |
| 14 | +{ |
| 15 | + $result = []; |
| 16 | + |
| 17 | + while (str_contains($buffer, "\n")) { |
| 18 | + assertType('array{string, string}', explode("\n", $buffer, 2)); |
| 19 | + [$first, $rest] = explode("\n", $buffer, 2); |
| 20 | + $result[] = $first; |
| 21 | + $buffer = $rest; |
| 22 | + } |
| 23 | + |
| 24 | + return $result; |
| 25 | +} |
| 26 | + |
| 27 | +function inIf(string $s): void |
| 28 | +{ |
| 29 | + if (str_contains($s, "\n")) { |
| 30 | + assertType('non-falsy-string', $s); |
| 31 | + assertType('array{string, string}', explode("\n", $s, 2)); |
| 32 | + assertType('array{string, string, ...<string>}', explode("\n", $s)); |
| 33 | + assertType('array{0: string, 1: string, 2?: string}', explode("\n", $s, 3)); |
| 34 | + } else { |
| 35 | + assertType('string', $s); |
| 36 | + } |
| 37 | + |
| 38 | + assertType('string', $s); |
| 39 | +} |
| 40 | + |
| 41 | +function startsWith(string $s): void |
| 42 | +{ |
| 43 | + if (str_starts_with($s, "\n")) { |
| 44 | + assertType('non-falsy-string', $s); |
| 45 | + assertType('array{string, string}', explode("\n", $s, 2)); |
| 46 | + } else { |
| 47 | + assertType('string', $s); |
| 48 | + } |
| 49 | + |
| 50 | + assertType('string', $s); |
| 51 | +} |
| 52 | + |
| 53 | +function endsWith(string $s): void |
| 54 | +{ |
| 55 | + if (str_ends_with($s, "\n")) { |
| 56 | + assertType('non-falsy-string', $s); |
| 57 | + assertType('array{string, string}', explode("\n", $s, 2)); |
| 58 | + } else { |
| 59 | + assertType('string', $s); |
| 60 | + } |
| 61 | + |
| 62 | + assertType('string', $s); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param non-empty-string $needle |
| 67 | + */ |
| 68 | +function variableNeedle(string $s, string $needle): void |
| 69 | +{ |
| 70 | + if (str_contains($s, $needle)) { |
| 71 | + assertType('non-empty-string', $s); |
| 72 | + assertType('array{string, string}', explode($needle, $s, 2)); |
| 73 | + } else { |
| 74 | + assertType('string', $s); |
| 75 | + } |
| 76 | + |
| 77 | + assertType('string', $s); |
| 78 | +} |
| 79 | + |
| 80 | +function notGuaranteed(string $s): void |
| 81 | +{ |
| 82 | + // no guard at all |
| 83 | + assertType('non-empty-list<string>', explode("\n", $s, 2)); |
| 84 | + |
| 85 | + if (str_contains($s, "\n")) { |
| 86 | + assertType('non-falsy-string', $s); |
| 87 | + // different delimiter than the proven substring |
| 88 | + assertType('non-empty-list<string>', explode(",", $s, 2)); |
| 89 | + // limit 1 always yields a single element |
| 90 | + assertType('non-empty-list<string>', explode("\n", $s, 1)); |
| 91 | + // negative limit may drop the guaranteed parts |
| 92 | + assertType('list<string>', explode("\n", $s, -1)); |
| 93 | + } else { |
| 94 | + assertType('string', $s); |
| 95 | + } |
| 96 | + |
| 97 | + assertType('string', $s); |
| 98 | +} |
| 99 | + |
| 100 | +function unknownLimit(string $s, int $limit): void |
| 101 | +{ |
| 102 | + // a limit that is not proven to be >= 2 falls back regardless of the guard |
| 103 | + assertType('list<string>', explode("\n", $s, $limit)); |
| 104 | + |
| 105 | + if (str_contains($s, "\n")) { |
| 106 | + assertType('non-falsy-string', $s); |
| 107 | + assertType('list<string>', explode("\n", $s, $limit)); |
| 108 | + } else { |
| 109 | + assertType('string', $s); |
| 110 | + } |
| 111 | + |
| 112 | + assertType('string', $s); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @param int<2, 4> $bounded |
| 117 | + * @param int<5, max> $unbounded |
| 118 | + * @param int<-2, 3> $negativeAndPositive |
| 119 | + * @param int<-5, -1> $alwaysNegative |
| 120 | + * @param int<min, -1> $unboundedNegative |
| 121 | + */ |
| 122 | +function rangeLimit(string $s, int $bounded, int $unbounded, int $negativeAndPositive, int $alwaysNegative, int $unboundedNegative): void |
| 123 | +{ |
| 124 | + if (str_contains($s, "\n")) { |
| 125 | + assertType('non-falsy-string', $s); |
| 126 | + // a bounded range yields optional keys up to its maximum |
| 127 | + assertType('array{0: string, 1: string, 2?: string, 3?: string}', explode("\n", $s, $bounded)); |
| 128 | + // an unbounded range stays a >= 2 list |
| 129 | + assertType('array{string, string, ...<string>}', explode("\n", $s, $unbounded)); |
| 130 | + // ranges that may be negative are not proven to yield two elements |
| 131 | + assertType('list<string>', explode("\n", $s, $negativeAndPositive)); |
| 132 | + assertType('list<string>', explode("\n", $s, $alwaysNegative)); |
| 133 | + assertType('list<string>', explode("\n", $s, $unboundedNegative)); |
| 134 | + } else { |
| 135 | + assertType('string', $s); |
| 136 | + } |
| 137 | + |
| 138 | + assertType('string', $s); |
| 139 | +} |
| 140 | + |
| 141 | +function zeroAndNegativeLimit(string $s): void |
| 142 | +{ |
| 143 | + if (str_contains($s, "\n")) { |
| 144 | + assertType('non-falsy-string', $s); |
| 145 | + // limit 0 is treated as 1, so a single non-empty element |
| 146 | + assertType('non-empty-list<string>', explode("\n", $s, 0)); |
| 147 | + // a negative limit drops trailing components and may empty the result |
| 148 | + assertType('list<string>', explode("\n", $s, -1)); |
| 149 | + assertType('list<string>', explode("\n", $s, -2)); |
| 150 | + } else { |
| 151 | + assertType('string', $s); |
| 152 | + } |
| 153 | + |
| 154 | + assertType('string', $s); |
| 155 | +} |
| 156 | + |
| 157 | +function largeConstantLimit(string $s): void |
| 158 | +{ |
| 159 | + if (str_contains($s, "\n")) { |
| 160 | + assertType('non-falsy-string', $s); |
| 161 | + // a limit larger than the array builder handles stays a >= 2 list |
| 162 | + assertType('array{string, string, ...<string>}', explode("\n", $s, 100000)); |
| 163 | + } else { |
| 164 | + assertType('string', $s); |
| 165 | + } |
| 166 | + |
| 167 | + assertType('string', $s); |
| 168 | +} |
0 commit comments