Merge pull request #3521 from rectorphp/fix-comment

fix nested comment in nested foreach to if
This commit is contained in:
kodiakhq[bot] 2020-06-15 10:03:23 +00:00 committed by GitHub
commit e1dd00ed9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View File

@ -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_()];

View File

@ -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';