From 26352d0e392407724c1088dd25b734cf632c9e10 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Tue, 20 Oct 2020 10:59:09 -0400 Subject: [PATCH] Fix #3625 - getIterator call is used inside loop --- .../Statements/Block/ForeachAnalyzer.php | 6 ++++++ tests/UnusedVariableTest.php | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php index 8a4af61cf..b8a0449d0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php @@ -787,12 +787,18 @@ class ForeachAnalyzer $statements_analyzer->addSuppressedIssues(['PossiblyUndefinedMethod']); } + $was_inside_call = $context->inside_call; + + $context->inside_call = true; + \Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer::analyze( $statements_analyzer, $fake_method_call, $context ); + $context->inside_call = $was_inside_call; + if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) { $statements_analyzer->removeSuppressedIssues(['PossiblyInvalidMethodCall']); } diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 02c4c3750..4266dbb2d 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -2203,6 +2203,27 @@ class UnusedVariableTest extends TestCase return fn() => $e->getMessage(); }' ], + 'useImmutableGetIteratorInForeach' => [ + ' + */ + public function getIterator() { + yield from [1, 2, 3]; + } + } + + $a = new A(); + + foreach ($a as $v) { + echo $v; + }' + ], ]; }