From c17a331f2c1e06bb1614b44645649fd3f27b4dd1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 31 Jan 2022 17:08:34 +0100 Subject: [PATCH] Regression tests Closes https://github.com/phpstan/phpstan/issues/2420 Closes https://github.com/phpstan/phpstan/issues/2718 Closes https://github.com/phpstan/phpstan/issues/3126 Closes https://github.com/phpstan/phpstan/issues/4586 Closes https://github.com/phpstan/phpstan/issues/4887 --- .../Analyser/NodeScopeResolverTest.php | 5 +++ tests/PHPStan/Analyser/data/bug-2420.php | 33 +++++++++++++++ tests/PHPStan/Analyser/data/bug-2718.php | 41 +++++++++++++++++++ tests/PHPStan/Analyser/data/bug-3126.php | 37 +++++++++++++++++ tests/PHPStan/Analyser/data/bug-4586.php | 13 ++++++ tests/PHPStan/Analyser/data/bug-4887.php | 18 ++++++++ 6 files changed, 147 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-2420.php create mode 100644 tests/PHPStan/Analyser/data/bug-2718.php create mode 100644 tests/PHPStan/Analyser/data/bug-3126.php create mode 100644 tests/PHPStan/Analyser/data/bug-4586.php create mode 100644 tests/PHPStan/Analyser/data/bug-4887.php diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 36ac460ea4..37b28051e8 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -665,6 +665,11 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/template-null-bound.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4592.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4903.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2420.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2718.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3126.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4586.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4887.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-2420.php b/tests/PHPStan/Analyser/data/bug-2420.php new file mode 100644 index 0000000000..8b3d5b4809 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-2420.php @@ -0,0 +1,33 @@ + false, + 1 => false, + ]; + + public function sayHello(int $key): void + { + $config = self::CONFIG[$key] ?? true; + assertType('bool', $config); + } +} + +class HelloWorld2 +{ + const CONFIG = [ + 0 => ['foo' => false], + 1 => ['foo' => false], + ]; + + public function sayHello(int $key): void + { + $config = self::CONFIG[$key]['foo'] ?? true; + assertType('bool', $config); + } +} diff --git a/tests/PHPStan/Analyser/data/bug-2718.php b/tests/PHPStan/Analyser/data/bug-2718.php new file mode 100644 index 0000000000..cbd4640939 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-2718.php @@ -0,0 +1,41 @@ + 'a', 'user_id' => 'id1'], + ['group_id' => 'a', 'user_id' => 'id2'], + ['group_id' => 'a', 'user_id' => 'id3'], + ['group_id' => 'b', 'user_id' => 'id4'], + ['group_id' => 'b', 'user_id' => 'id5'], + ['group_id' => 'b', 'user_id' => 'id6'], + ]; + }; + + $orders = $fun(); + + $result = []; + foreach ($orders as $order) { + assertType('bool', isset($result[$order['group_id']]['users'])); + if (isset($result[$order['group_id']]['users'])) { + $result[$order['group_id']]['users'][] = $order['user_id']; + continue; + } + + $result[$order['group_id']] = [ + 'users' => [ + $order['user_id'], + ], + ]; + } + } + +} diff --git a/tests/PHPStan/Analyser/data/bug-3126.php b/tests/PHPStan/Analyser/data/bug-3126.php new file mode 100644 index 0000000000..9f675a4239 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-3126.php @@ -0,0 +1,37 @@ + $input + */ + function failure(array $input): void { + $results = []; + + foreach ($input as $keyOne => $layerOne) { + assertType('bool', isset($results[$keyOne]['name'])); + if(isset($results[$keyOne]['name']) === false) { + $results[$keyOne]['name'] = $layerOne; + } + } + } + + /** + * @param array $input + */ + function no_failure(array $input): void { + $results = []; + + foreach ($input as $keyOne => $layerOne) { + if(isset($results[$keyOne]) === false) { + $results[$keyOne] = $layerOne; + } + } + } + +} diff --git a/tests/PHPStan/Analyser/data/bug-4586.php b/tests/PHPStan/Analyser/data/bug-4586.php new file mode 100644 index 0000000000..82291ec0df --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4586.php @@ -0,0 +1,13 @@ + $_REQUEST, + '$_COOKIE' => $_COOKIE, + '$_SERVER' => $_SERVER, + '$GLOBALS' => $GLOBALS, + '$SESSION' => isset($_SESSION) ? $_SESSION : NULL]; + +foreach ($foo as $data) +{ + assertType('bool', is_array($data)); +}