diff --git a/tests/PHPStan/Analyser/nsrt/bug-10025.php b/tests/PHPStan/Analyser/nsrt/bug-10025.php new file mode 100644 index 0000000000..4d172f2aac --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-10025.php @@ -0,0 +1,36 @@ + $foos + * @param list $bars + */ +function x(array $foos, array $bars): void +{ + $arr = []; + foreach ($foos as $foo) { + $arr[$foo->groupId]['foo'][] = $foo; + } + foreach ($bars as $bar) { + $arr[$bar->groupId]['bar'][] = $bar; + } + + assertType('array, bar?: non-empty-list}>', $arr); + foreach ($arr as $groupId => $group) { + if (isset($group['foo'])) { + } + if (isset($group['bar'])) { + } + } + + assertType('array, bar?: non-empty-list}>', $arr); +} + diff --git a/tests/PHPStan/Analyser/nsrt/bug-10640.php b/tests/PHPStan/Analyser/nsrt/bug-10640.php new file mode 100644 index 0000000000..12452341c6 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-10640.php @@ -0,0 +1,31 @@ +', $changes); + +foreach (toRem() as $del) { + $changes[$add['id']]['del'][] = doSomething($del); +} +assertType('array', $changes); + +foreach ($changes as $changeSet) { + if (isset($changeSet['del'])) { + doDel($changeSet['del']); + } + if (isset($changeSet['add'])) { + doAdd($changeSet['add']); + } +} + +function doSomething($s) {} +function toAdd($s) {} +function toRem($s) {} +function doDel($s) {} +function doAdd($s) {} diff --git a/tests/PHPStan/Analyser/nsrt/bug-12078.php b/tests/PHPStan/Analyser/nsrt/bug-12078.php new file mode 100644 index 0000000000..c1d22c1ab1 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-12078.php @@ -0,0 +1,67 @@ + + */ +function returnsData6M(): array +{ + return ["A" => 'data A', "B" => 'Data B']; +} + +/** + * @return array + */ +function returnsData3M(): array +{ + return ["A" => 'data A', "C" => 'Data C']; +} + +function main() +{ + $arrDataByKey = []; + + $arrData6M = returnsData6M(); + if ([] === $arrData6M) { + echo "No data for 6M\n"; + } else { + foreach ($arrData6M as $key => $data) { + $arrDataByKey[$key]['6M'][] = $data; + } + } + + $arrData3M = returnsData3M(); + if ([] === $arrData3M) { + echo "No data for 3M\n"; + } else { + foreach ($arrData3M as $key => $data) { + $arrDataByKey[$key]['3M'][] = $data; + } + } + /* + So $arrDataByKey looks like + [ + 'A'=>[ + '6M'=>['data A'], + '3M'=>['data A'] + ], + 'B'=>[ + '6M'=>['data B'] + ], + 'C'=>[ + '3M'=>['data C'] + ] + ] + */ + + assertType("array, '3M'?: non-empty-list}>", $arrDataByKey); + foreach ($arrDataByKey as $key => $arrDataByKeyForKey) { + assertType("non-empty-array{'6M'?: non-empty-list, '3M'?: non-empty-list}", $arrDataByKeyForKey); + echo [] === ($arrDataByKeyForKey['6M'] ?? []) ? 'No 6M data for key ' . $key . "\n" : 'We got 6M data for key ' . $key . "\n"; + echo [] === ($arrDataByKeyForKey['3M'] ?? []) ? 'No 3M data for key ' . $key . "\n" : 'We got 3M data for key ' . $key . "\n"; + assertType("non-empty-array{'6M'?: non-empty-list, '3M'?: non-empty-list}", $arrDataByKeyForKey); + } +} diff --git a/tests/PHPStan/Analyser/nsrt/bug-6173.php b/tests/PHPStan/Analyser/nsrt/bug-6173.php new file mode 100644 index 0000000000..41ced55a63 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-6173.php @@ -0,0 +1,32 @@ +', $res); + foreach ($res as $id => $r) { + assertType('non-empty-array{foo?: int, bar?: int}', $r); + return isset($r['foo']); + } + + return false; + } +} diff --git a/tests/PHPStan/Rules/Variables/IssetRuleTest.php b/tests/PHPStan/Rules/Variables/IssetRuleTest.php index da52dc925f..40b837c134 100644 --- a/tests/PHPStan/Rules/Variables/IssetRuleTest.php +++ b/tests/PHPStan/Rules/Variables/IssetRuleTest.php @@ -504,4 +504,11 @@ public function testPr4374(): void ]); } + public function testBug10640(): void + { + $this->treatPhpDocTypesAsCertain = true; + + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10640.php'], []); + } + }