From 0178e9cadd1f34d64931d83b9a09716759eeb378 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 31 Jul 2020 00:40:12 +0200 Subject: [PATCH] [PHP 5.6] Fix foreach unset variable default --- ...DefaultValueForUndefinedVariableRector.php | 16 ++++++---- ...ultValueForUndefinedVariableRectorTest.php | 14 -------- .../Php74Test.php | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/Php74Test.php diff --git a/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php b/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php index c6725efbae3..4754cac96ba 100644 --- a/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php +++ b/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php @@ -32,6 +32,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; /** * @see https://github.com/vimeo/psalm/blob/29b70442b11e3e66113935a2ee22e165a70c74a4/docs/fixing_code.md#possiblyundefinedvariable * @see https://3v4l.org/MZFel + * * @see \Rector\Php56\Tests\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\AddDefaultValueForUndefinedVariableRectorTest */ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector @@ -116,13 +117,18 @@ PHP private function collectUndefinedVariableScope(Node $node): array { $undefinedVariables = []; + $this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (&$undefinedVariables): ?int { // entering new scope - break! if ($node instanceof FunctionLike && ! $node instanceof ArrowFunction) { return NodeTraverser::STOP_TRAVERSAL; } - $this->collectDefinedVariablesFromForeach($node); + if ($node instanceof Foreach_) { + // handled above + $this->collectDefinedVariablesFromForeach($node); + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } if (! $node instanceof Variable) { return null; @@ -150,13 +156,9 @@ PHP return array_unique($undefinedVariables); } - private function collectDefinedVariablesFromForeach(Node $node): void + private function collectDefinedVariablesFromForeach(Foreach_ $foreach): void { - if (! $node instanceof Foreach_) { - return; - } - - $this->traverseNodesWithCallable($node->stmts, function (Node $node): void { + $this->traverseNodesWithCallable((array) $foreach->stmts, function (Node $node): void { if ($node instanceof Assign || $node instanceof AssignRef) { if (! $node->var instanceof Variable) { return; diff --git a/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/AddDefaultValueForUndefinedVariableRectorTest.php b/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/AddDefaultValueForUndefinedVariableRectorTest.php index ce9f1cb7174..0bca2b50af9 100644 --- a/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/AddDefaultValueForUndefinedVariableRectorTest.php +++ b/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/AddDefaultValueForUndefinedVariableRectorTest.php @@ -24,20 +24,6 @@ final class AddDefaultValueForUndefinedVariableRectorTest extends AbstractRector return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); } - /** - * @dataProvider provideDataPhp74() - * @requires PHP >= 7.4 - */ - public function testPhp74(SmartFileInfo $fileInfo): void - { - $this->doTestFileInfo($fileInfo); - } - - public function provideDataPhp74(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp74'); - } - protected function getRectorClass(): string { return AddDefaultValueForUndefinedVariableRector::class; diff --git a/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/Php74Test.php b/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/Php74Test.php new file mode 100644 index 00000000000..24a334794e6 --- /dev/null +++ b/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/Php74Test.php @@ -0,0 +1,32 @@ += 7.4 + */ + public function test(SmartFileInfo $fileInfo): void + { + $this->doTestFileInfo($fileInfo); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp74'); + } + + protected function getRectorClass(): string + { + return AddDefaultValueForUndefinedVariableRector::class; + } +}