From 98ab8c91998536b1e27302004997493dfec04e17 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 20 Jun 2022 09:41:16 +0200 Subject: [PATCH] Regression test Closes https://github.com/phpstan/phpstan/issues/4117 --- .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-4117.php | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-4117.php diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 5094afa198..2ad1ac4f6c 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -915,6 +915,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/constant-array-intersect.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7153.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/in-array-non-empty.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4117.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-4117.php b/tests/PHPStan/Analyser/data/bug-4117.php new file mode 100644 index 0000000000..4e5c2f90bf --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4117.php @@ -0,0 +1,54 @@ + + */ +class GenericList implements IteratorAggregate +{ + /** @var array */ + protected $items = []; + + /** + * @return ArrayIterator + */ + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->items); + } + + /** + * @return ?T + */ + public function broken(int $key) + { + $item = $this->items[$key] ?? null; + if ($item) { + assertType("T of mixed~0|0.0|''|'0'|array{}|false|null (class Bug4117Types\GenericList, argument)", $item); + } else { + assertType("(array{}&T (class Bug4117Types\GenericList, argument))|(0.0&T (class Bug4117Types\GenericList, argument))|(0&T (class Bug4117Types\GenericList, argument))|(''&T (class Bug4117Types\GenericList, argument))|('0'&T (class Bug4117Types\GenericList, argument))|(T (class Bug4117Types\GenericList, argument)&false)|null", $item); + } + + assertType("T of mixed~0|0.0|''|'0'|array{}|false|null (class Bug4117Types\GenericList, argument)|null", $item); + + return $item; + } + + /** + * @return ?T + */ + public function works(int $key) + { + $item = $this->items[$key] ?? null; + assertType('T (class Bug4117Types\GenericList, argument)|null', $item); + + return $item; + } +}