From 58e578e6a7d1a820c598da3f8be0980eddbe8f88 Mon Sep 17 00:00:00 2001 From: orklah Date: Thu, 2 Sep 2021 19:57:42 +0200 Subject: [PATCH 1/2] fix Generator giving templates to Iterator --- .../Type/TemplateStandinTypeReplacer.php | 3 ++- tests/GeneratorTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index 477685d39e5..96fc77fee0c 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -12,6 +12,7 @@ use function array_merge; use function array_values; use function count; +use function is_a; use function reset; use function strpos; use function substr; @@ -356,7 +357,7 @@ private static function findMatchingAtomicTypesForTemplate( $input_key = substr($input_key, 0, $bracket_pos); } - if ($input_key === $key) { + if ($input_key === $key || is_a($input_key, $key, true)) { $matching_atomic_types[$atomic_input_type->getId()] = $atomic_input_type; continue; } diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 9c2c30eb5e2..8feb6988bd4 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -270,6 +270,22 @@ function f() : Generator { [], ['UndefinedClass'] ], + 'fillTemplatesForIteratorFromGenerator' => [ + ' + */ + function generator(): Generator + { + yield "test"; + } + + $iterator = new NoRewindIterator(generator()); + ', + 'assertions' => [ + '$iterator' => 'NoRewindIterator', + ] + ], ]; } From 854858a3bc57ec95bdcd1b9086d015e522aa9649 Mon Sep 17 00:00:00 2001 From: orklah Date: Thu, 2 Sep 2021 20:55:50 +0200 Subject: [PATCH 2/2] this should have less impact --- src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index 96fc77fee0c..e0068e0ffc3 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -357,7 +357,7 @@ private static function findMatchingAtomicTypesForTemplate( $input_key = substr($input_key, 0, $bracket_pos); } - if ($input_key === $key || is_a($input_key, $key, true)) { + if ($input_key === $key) { $matching_atomic_types[$atomic_input_type->getId()] = $atomic_input_type; continue; } @@ -484,6 +484,12 @@ private static function findMatchingAtomicTypesForTemplate( $atomic_input_type->as ) ); + continue; + } + + if (is_a($input_key, $key, true)) { + $matching_atomic_types[$atomic_input_type->getId()] = $atomic_input_type; + continue; } }