mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 16:12:22 +00:00
[EarlyReturn] Handle mirror comments before elseif on RemoveAlwaysElseRector (#234)
Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
This commit is contained in:
parent
6311c8b5c9
commit
24187781db
|
@ -35,7 +35,8 @@ class ElseIf_
|
|||
bar();
|
||||
} elseif ($cond3) {
|
||||
baz();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\EarlyReturn\Tests\Rector\If_\RemoveAlwaysElseRector\Fixture;
|
||||
|
||||
class KeepTheElseIfElseifHasNoStmt
|
||||
{
|
||||
public function convert($data)
|
||||
{
|
||||
// a comment 1
|
||||
if (is_array($data)) {
|
||||
return $res;
|
||||
}
|
||||
// a comment 2
|
||||
elseif ($data instanceof EncodedString) {
|
||||
}
|
||||
// a comment 3
|
||||
elseif (is_string($data)) {
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\EarlyReturn\Tests\Rector\If_\RemoveAlwaysElseRector\Fixture;
|
||||
|
||||
class KeepTheElseIfElseifHasNoStmt
|
||||
{
|
||||
public function convert($data)
|
||||
{
|
||||
// a comment 1
|
||||
if (is_array($data)) {
|
||||
return $res;
|
||||
}
|
||||
// a comment 2
|
||||
if ($data instanceof EncodedString) {
|
||||
}
|
||||
// a comment 3
|
||||
elseif (is_string($data)) {
|
||||
}
|
||||
else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\EarlyReturn\Tests\Rector\If_\RemoveAlwaysElseRector\Fixture;
|
||||
|
||||
class LostCommentBeforeElseif2
|
||||
{
|
||||
public function convert($data)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
return $res;
|
||||
} elseif ($data instanceof EncodedString) {
|
||||
}
|
||||
// this comment should be kept
|
||||
elseif (is_string($data)) {
|
||||
return $data;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\EarlyReturn\Tests\Rector\If_\RemoveAlwaysElseRector\Fixture;
|
||||
|
||||
class LostCommentBeforeElseif2
|
||||
{
|
||||
public function convert($data)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
return $res;
|
||||
}
|
||||
if ($data instanceof EncodedString) {
|
||||
}
|
||||
// this comment should be kept
|
||||
elseif (is_string($data)) {
|
||||
return $data;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -46,7 +46,8 @@ class ProcessEmptyReturnLast
|
|||
if ($value - 1) {
|
||||
$value = 55;
|
||||
return 10;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Rector\EarlyReturn\Rector\If_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Exit_;
|
||||
use PhpParser\Node\Stmt\Continue_;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\ElseIf_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
|
@ -77,10 +78,12 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
if ($node->elseifs !== []) {
|
||||
$originalNode = clone $node;
|
||||
$if = new If_($node->cond);
|
||||
$if->stmts = $node->stmts;
|
||||
|
||||
$this->addNodeBeforeNode($if, $node);
|
||||
$this->mirrorComments($if, $node);
|
||||
|
||||
/** @var ElseIf_ $firstElseIf */
|
||||
$firstElseIf = array_shift($node->elseifs);
|
||||
|
@ -88,6 +91,16 @@ CODE_SAMPLE
|
|||
$node->stmts = $firstElseIf->stmts;
|
||||
$this->mirrorComments($node, $firstElseIf);
|
||||
|
||||
$statements = $this->getStatementsElseIfs($node);
|
||||
if ($statements !== []) {
|
||||
$this->addNodesAfterNode($statements, $node);
|
||||
}
|
||||
|
||||
if ($originalNode->else instanceof Else_) {
|
||||
$node->else = null;
|
||||
$this->addNodeAfterNode($originalNode->else, $node);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
|
@ -100,9 +113,30 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
private function doesLastStatementBreakFlow(If_ $if): bool
|
||||
/**
|
||||
* @return ElseIf_[]
|
||||
*/
|
||||
private function getStatementsElseIfs(If_ $if): array
|
||||
{
|
||||
$lastStmt = end($if->stmts);
|
||||
$statements = [];
|
||||
foreach ($if->elseifs as $key => $elseif) {
|
||||
if ($this->doesLastStatementBreakFlow($elseif) && $elseif->stmts !== []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$statements[] = $elseif;
|
||||
unset($if->elseifs[$key]);
|
||||
}
|
||||
|
||||
return $statements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param If_|ElseIf_ $node
|
||||
*/
|
||||
private function doesLastStatementBreakFlow(Node $node): bool
|
||||
{
|
||||
$lastStmt = end($node->stmts);
|
||||
|
||||
return ! ($lastStmt instanceof Return_
|
||||
|| $lastStmt instanceof Throw_
|
||||
|
|
Loading…
Reference in New Issue
Block a user