|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug14336; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +/** |
| 8 | + * @param list<string> $list |
| 9 | + */ |
| 10 | +function test(array $list, int $int): void { |
| 11 | + $list[$int] = 'foo'; |
| 12 | + assertType('non-empty-array<int, string>', $list); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @param array<string, list<array{xmlNamespace: string, namespace: string, name: string}>> $xsdFiles |
| 17 | + * @param array<string, list<array{xmlNamespace: string, namespace: string, name: string}>> $groupedByNamespace |
| 18 | + * @param array<string, list<string>> $extraNamespaces |
| 19 | + */ |
| 20 | +function test2(array $xsdFiles, array $groupedByNamespace, array $extraNamespaces, int $int): void { |
| 21 | + foreach ($extraNamespaces as $mergedNamespace) { |
| 22 | + if (count($mergedNamespace) < 2) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + $targetNamespace = end($mergedNamespace); |
| 27 | + if (!isset($groupedByNamespace[$targetNamespace])) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + $xmlNamespace = $groupedByNamespace[$targetNamespace][0]['xmlNamespace']; |
| 31 | + |
| 32 | + $xsdFiles[$xmlNamespace] = []; |
| 33 | + assertType('array{}', $xsdFiles[$xmlNamespace]); |
| 34 | + foreach ($mergedNamespace as $namespace) { |
| 35 | + foreach ($groupedByNamespace[$namespace] ?? [] as $viewHelper) { |
| 36 | + assertType('array{xmlNamespace: string, namespace: string, name: string}', $viewHelper); |
| 37 | + $xsdFiles[$xmlNamespace][$int] = $viewHelper; |
| 38 | + assertType('non-empty-array<int, array{xmlNamespace: string, namespace: string, name: string}>', $xsdFiles[$xmlNamespace]); |
| 39 | + } |
| 40 | + assertType('array<int, array{xmlNamespace: string, namespace: string, name: string}>', $xsdFiles[$xmlNamespace]); |
| 41 | + } |
| 42 | + assertType('array<int, array{xmlNamespace: string, namespace: string, name: string}>', $xsdFiles[$xmlNamespace]); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @param list<string> $list |
| 48 | + */ |
| 49 | +function testInLoop(array $list, int $int): void { |
| 50 | + foreach ([1, 2, 3] as $item) { |
| 51 | + $list[$int] = 'foo'; |
| 52 | + } |
| 53 | + assertType('non-empty-array<int, string>', $list); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @param array<string, list<string>> $map |
| 58 | + */ |
| 59 | +function testNestedDimFetchInLoop(array $map, string $key, int $int): void { |
| 60 | + $map[$key] = []; |
| 61 | + foreach ([1, 2, 3] as $item) { |
| 62 | + $map[$key][$int] = 'foo'; |
| 63 | + } |
| 64 | + assertType('non-empty-array<int, string>', $map[$key]); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @param array<string, list<string>> $map |
| 69 | + * @param list<string> $items |
| 70 | + * @param list<string> $items2 |
| 71 | + */ |
| 72 | +function testDoubleNestedForeachDimFetch(array $map, string $key, int $int, array $items, array $items2): void { |
| 73 | + $map[$key] = []; |
| 74 | + foreach ($items as $item) { |
| 75 | + foreach ($items2 as $item2) { |
| 76 | + $map[$key][$int] = $item2; |
| 77 | + } |
| 78 | + } |
| 79 | + assertType('array<int, string>', $map[$key]); |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @param array<string, list<string>> $map |
| 84 | + * @param list<string> $items |
| 85 | + */ |
| 86 | +function testSingleVariableForeach(array $map, string $key, int $int, array $items): void { |
| 87 | + $map[$key] = []; |
| 88 | + foreach ($items as $item) { |
| 89 | + $map[$key][$int] = $item; |
| 90 | + } |
| 91 | + assertType('array<int, string>', $map[$key]); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @param array<string, list<string>> $map |
| 96 | + * @param list<string> $items |
| 97 | + * @param list<string> $outerItems |
| 98 | + */ |
| 99 | +function testOuterForeach(array $map, string $key, int $int, array $items, array $outerItems): void { |
| 100 | + foreach ($outerItems as $outerItem) { |
| 101 | + $map[$key] = []; |
| 102 | + foreach ($items as $item) { |
| 103 | + $map[$key][$int] = $item; |
| 104 | + } |
| 105 | + assertType('array<int, string>', $map[$key]); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * @param array<string, list<string>> $map |
| 111 | + * @param list<string> $items |
| 112 | + * @param list<string> $outerItems |
| 113 | + */ |
| 114 | +function testOuterForeachWithContinue(array $map, string $key, int $int, array $items, array $outerItems): void { |
| 115 | + foreach ($outerItems as $outerItem) { |
| 116 | + if (strlen($outerItem) < 2) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + $map[$key] = []; |
| 120 | + foreach ($items as $item) { |
| 121 | + $map[$key][$int] = $item; |
| 122 | + } |
| 123 | + assertType('array<int, string>', $map[$key]); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * @param array<string, list<string>> $map |
| 129 | + * @param list<list<string>> $nestedItems |
| 130 | + * @param list<string> $outerItems |
| 131 | + */ |
| 132 | +function testNestedInnerForeach(array $map, string $key, int $int, array $nestedItems, array $outerItems): void { |
| 133 | + foreach ($outerItems as $outerItem) { |
| 134 | + if (strlen($outerItem) < 2) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + $map[$key] = []; |
| 138 | + foreach ($nestedItems as $items) { |
| 139 | + foreach ($items as $item) { |
| 140 | + $map[$key][$int] = $item; |
| 141 | + } |
| 142 | + } |
| 143 | + assertType('array<int, string>', $map[$key]); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @param array<string, list<string>> $map |
| 149 | + * @param array<string, list<string>> $nestedItems |
| 150 | + * @param list<string> $outerItems |
| 151 | + */ |
| 152 | +function testNestedInnerForeachNullCoalesce(array $map, string $key, int $int, array $nestedItems, array $outerItems): void { |
| 153 | + foreach ($outerItems as $outerItem) { |
| 154 | + if (strlen($outerItem) < 2) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + $map[$key] = []; |
| 158 | + foreach ($outerItems as $ns) { |
| 159 | + foreach ($nestedItems[$ns] ?? [] as $item) { |
| 160 | + $map[$key][$int] = $item; |
| 161 | + } |
| 162 | + } |
| 163 | + assertType('array<int, string>', $map[$key]); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * @param array<string, list<array{ns: string, name: string}>> $map |
| 169 | + * @param array<string, list<array{ns: string, name: string}>> $grouped |
| 170 | + * @param array<string, list<string>> $extra |
| 171 | + */ |
| 172 | +function testCloseToOriginal(array $map, array $grouped, array $extra, int $int): void { |
| 173 | + foreach ($extra as $merged) { |
| 174 | + if (count($merged) < 2) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + $target = end($merged); |
| 178 | + if (!isset($grouped[$target])) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + $key = $grouped[$target][0]['ns']; |
| 182 | + |
| 183 | + $map[$key] = []; |
| 184 | + foreach ($merged as $ns) { |
| 185 | + foreach ($grouped[$ns] ?? [] as $item) { |
| 186 | + $map[$key][$int] = $item; |
| 187 | + } |
| 188 | + } |
| 189 | + assertType('array<int, array{ns: string, name: string}>', $map[$key]); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +/** |
| 194 | + * @param list<string> $list |
| 195 | + */ |
| 196 | +function testAppend(array $list): void { |
| 197 | + $list[] = 'foo'; |
| 198 | + assertType('non-empty-list<string>', $list); |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * @param list<string> $list |
| 203 | + */ |
| 204 | +function testLiteralZero(array $list): void { |
| 205 | + $list[0] = 'foo'; |
| 206 | + assertType("non-empty-list<string>&hasOffsetValue(0, 'foo')", $list); |
| 207 | +} |
0 commit comments