mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #3521 from rectorphp/fix-comment
fix nested comment in nested foreach to if
This commit is contained in:
commit
e1dd00ed9f
@ -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_()];
|
||||
|
||||
|
@ -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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user