mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-30 06:33:31 +00:00
[EarlyReturn] Skip same return expr on ChangeOrIfReturnToEarlyReturnRector (#797)
* [EarlyReturn] Skip same return expr on ChangeOrIfReturnToEarlyReturnRector * fixture update * phpstan * rollback handling tweak on NarrowUnionTypeDocRector * tweak
This commit is contained in:
parent
654d4a2a2b
commit
4d60c23f7c
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\Fixture;
|
||||||
|
|
||||||
|
class SkipSameReturn
|
||||||
|
{
|
||||||
|
public function run($a, $b, $c)
|
||||||
|
{
|
||||||
|
if ($a && $b || $c) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -90,14 +90,11 @@ CODE_SAMPLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $phpDocInfo->hasChanged()) {
|
if ($phpDocInfo->hasChanged()) {
|
||||||
return null;
|
$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);
|
||||||
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);
|
|
||||||
|
|
||||||
// @see https://github.com/rectorphp/rector-src/pull/795
|
|
||||||
// avoid duplicated ifs and returns when combined with ChangeOrIfReturnToEarlyReturnRector, ChangeAndIfToEarlyReturnRector, and AddArrayReturnDocTypeRector
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,16 +91,18 @@ CODE_SAMPLE
|
||||||
}
|
}
|
||||||
|
|
||||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||||
if ($nextNode === null) {
|
if ($this->shouldSkip($nextNode)) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($nextNode instanceof Return_ && $nextNode->expr === null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Return_ $return */
|
/** @var Return_ $return */
|
||||||
$return = $node->stmts[0];
|
$return = $node->stmts[0];
|
||||||
|
|
||||||
|
// avoid repetitive ifs combined with other rules
|
||||||
|
if ($nextNode instanceof Return_ && $this->nodeComparator->areNodesEqual($nextNode->expr, $return->expr)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$ifs = $this->createMultipleIfs($node->cond, $return, []);
|
$ifs = $this->createMultipleIfs($node->cond, $return, []);
|
||||||
|
|
||||||
foreach ($ifs as $key => $if) {
|
foreach ($ifs as $key => $if) {
|
||||||
|
@ -115,6 +117,15 @@ CODE_SAMPLE
|
||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function shouldSkip(?Node $nextNode): bool
|
||||||
|
{
|
||||||
|
if ($nextNode === null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $nextNode instanceof Return_ && $nextNode->expr === null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param If_[] $ifs
|
* @param If_[] $ifs
|
||||||
* @return If_[]
|
* @return If_[]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user