From 41be8026f1764437989bffc77b546a8a88230efa Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 30 Aug 2022 08:48:28 +0200 Subject: [PATCH] Regression tests --- ...nexistentOffsetInArrayDimFetchRuleTest.php | 10 ++++++ tests/PHPStan/Rules/Arrays/data/bug-3872.php | 19 ++++++++++++ tests/PHPStan/Rules/Arrays/data/bug-6783.php | 31 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-3872.php create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-6783.php diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index 34214e8c68..f0a15bde0b 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -508,4 +508,14 @@ public function testSpecifyExistentOffsetWhenEnteringForeach(): void $this->analyse([__DIR__ . '/data/specify-existent-offset-when-entering-foreach.php'], []); } + public function testBug3872(): void + { + $this->analyse([__DIR__ . '/data/bug-3872.php'], []); + } + + public function testBug6783(): void + { + $this->analyse([__DIR__ . '/data/bug-6783.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-3872.php b/tests/PHPStan/Rules/Arrays/data/bug-3872.php new file mode 100644 index 0000000000..944cba7757 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-3872.php @@ -0,0 +1,19 @@ + '', 'operator' => '']; + if ($item['value']) { + $item['value'] = strtotime($item['value']); + if ($item['operator'] === 'eq') { + echo 'test'; + } + } + } + } +} diff --git a/tests/PHPStan/Rules/Arrays/data/bug-6783.php b/tests/PHPStan/Rules/Arrays/data/bug-6783.php new file mode 100644 index 0000000000..beda63bd95 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-6783.php @@ -0,0 +1,31 @@ + + */ +function foo(): array +{ + // something from elsewhere (not in the scope of PHPStan) + return [ + // removing or keeping those lines does/should not change the reporting + 'foo' => [ + 'bar' => true, + ] + ]; +} + +function bar() { + $data = foo(); + $data = $data['foo'] ?? []; // <<< removing this line suppress the error + $data += [ + 'default' => true, + ]; + foreach (['formatted'] as $field) { + $data[$field] = empty($data[$field]) ? false : true; + } + + $bar = $data['bar']; +} +