From d1e5990531a0894f9cb246b517190beb4095af6d Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Mon, 15 Jun 2020 11:54:06 +0200 Subject: [PATCH] fix nested comment in nested foreach to if --- .../ChangeNestedForeachIfsToEarlyContinueRector.php | 11 +++++++---- .../Fixture/comment_inside_if_statement.php.inc | 12 ++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php b/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php index ca2ee51157d..d4d36b3d82e 100644 --- a/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php +++ b/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php @@ -14,6 +14,7 @@ use Rector\Core\PhpParser\Node\Manipulator\IfManipulator; use Rector\Core\Rector\AbstractRector; use Rector\Core\RectorDefinition\CodeSample; use Rector\Core\RectorDefinition\RectorDefinition; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\SOLID\NodeTransformer\ConditionInverter; /** @@ -102,15 +103,13 @@ PHP return null; } - $this->processNestedIfsWithNonBreaking($node, $nestedIfsWithOnlyNonReturn); - - return $node; + return $this->processNestedIfsWithNonBreaking($node, $nestedIfsWithOnlyNonReturn); } /** * @param If_[] $nestedIfsWithOnlyReturn */ - private function processNestedIfsWithNonBreaking(Foreach_ $foreach, array $nestedIfsWithOnlyReturn): void + private function processNestedIfsWithNonBreaking(Foreach_ $foreach, array $nestedIfsWithOnlyReturn): Foreach_ { // add nested if openly after this $nestedIfsWithOnlyReturnCount = count($nestedIfsWithOnlyReturn); @@ -130,6 +129,8 @@ PHP $this->addInvertedIfStmtWithContinue($nestedIfWithOnlyReturn, $foreach); } } + + return $foreach; } private function addInvertedIfStmtWithContinue(If_ $nestedIfWithOnlyReturn, Foreach_ $foreach): void @@ -147,6 +148,8 @@ PHP return; } + $nestedIfWithOnlyReturn->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $nestedIfWithOnlyReturn->cond = $invertedCondition; $nestedIfWithOnlyReturn->stmts = [new Continue_()]; diff --git a/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture/comment_inside_if_statement.php.inc b/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture/comment_inside_if_statement.php.inc index b592b702e62..d3d5113e27a 100644 --- a/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture/comment_inside_if_statement.php.inc +++ b/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture/comment_inside_if_statement.php.inc @@ -4,14 +4,14 @@ namespace Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContin class CommentInsideIfStatement { - public function run() + public function run($values) { $items = []; foreach ($values as $value) { - if ($value === 5) //why am I doing this? + if ($value === 5) // why am I doing this? { - if ($value2 === 10) { + if ($value === 10) { $items[] = 'maybe'; } } @@ -27,16 +27,16 @@ namespace Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContin class CommentInsideIfStatement { - public function run() + public function run($values) { $items = []; foreach ($values as $value) { if ($value !== 5) { - //why am I doing this? continue; } - if ($value2 !== 10) { + // why am I doing this? + if ($value !== 10) { continue; } $items[] = 'maybe';