From ef0c88f958cf39a652dd1d0858e61590fd1135bc Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 18 Jan 2020 18:09:50 +0100 Subject: [PATCH] Fix UnwrapFutureCompatibleIfFunctionExistsRector for no else [closes #2691] --- CHANGELOG.md | 2 +- ...FutureCompatibleIfFunctionExistsRector.php | 24 ++++++++++++------- .../Node/Manipulator/IfManipulator.php | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3012c4bca455..8a1e636cccdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,7 +115,7 @@ PRs and issues are linked, so you can find more about it. Thanks to [ChangelogLi - [#2565] [DeadCode] Add RemoveUnusedClassesRector - [#2593] [DoctrineGedmoToKnpLabs] Add SoftDeletableBehaviorRector -- [#2569] [Polyfill] Add UnwrapFutureCompatibleIfRector +- [#2569] [Polyfill] Add UnwrapFutureCompatibleIfFunctionExistsRector - [#2570] [SOLID] Add ChangeNestedIfsToEarlyReturnRector & ChangeIfElseValueAssignToEarlyReturnRector - [#2568] [Symfony 5] Add param types diff --git a/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php b/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php index bedce0af56d6..74bfb3afd402 100644 --- a/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php +++ b/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php @@ -5,6 +5,7 @@ namespace Rector\Polyfill\Rector\If_; use PhpParser\Node; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\If_; use Rector\PhpParser\Node\Manipulator\IfManipulator; use Rector\Polyfill\FeatureSupport\FunctionSupportResolver; @@ -13,7 +14,7 @@ use Rector\RectorDefinition\RectorDefinition; /** - * @see \Rector\Polyfill\Tests\Rector\If_\UnwrapFutureCompatibleIfRector\UnwrapFutureCompatibleIfRectorTest + * @see \Rector\Polyfill\Tests\Rector\If_\UnwrapFutureCompatibleIfFunctionExistsRector\UnwrapFutureCompatibleIfFunctionExistsRectorTest */ final class UnwrapFutureCompatibleIfFunctionExistsRector extends AbstractRector { @@ -80,12 +81,12 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $match = $this->ifManipulator->isIfElseWithFunctionCondition($node, 'function_exists'); + $match = $this->ifManipulator->isIfOrIfElseWithFunctionCondition($node, 'function_exists'); if ($match === false) { return null; } - /** @var Node\Expr\FuncCall $funcCall */ + /** @var FuncCall $funcCall */ $funcCall = $node->cond; $functionToExistName = $this->getValue($funcCall->args[0]->value); @@ -97,7 +98,18 @@ public function refactor(Node $node): ?Node return null; } - foreach ($node->stmts as $key => $ifStmt) { + $this->unwrapStmts($node->stmts, $node); + $this->removeNode($node); + + return null; + } + + /** + * @param Node\Stmt[] $stmts + */ + private function unwrapStmts(array $stmts, Node $node): void + { + foreach ($stmts as $key => $ifStmt) { if ($key === 0) { // move comment from if to first element to keep it $ifStmt->setAttribute('comments', $node->getComments()); @@ -105,9 +117,5 @@ public function refactor(Node $node): ?Node $this->addNodeAfterNode($ifStmt, $node); } - - $this->removeNode($node); - - return null; } } diff --git a/src/PhpParser/Node/Manipulator/IfManipulator.php b/src/PhpParser/Node/Manipulator/IfManipulator.php index c37653bfe110..b4f76c6eeae2 100644 --- a/src/PhpParser/Node/Manipulator/IfManipulator.php +++ b/src/PhpParser/Node/Manipulator/IfManipulator.php @@ -248,9 +248,9 @@ public function isIfAndElseWithSameVariableAssignAsLastStmts(If_ $if, Expr $desi * } else { * } */ - public function isIfElseWithFunctionCondition(If_ $if, string $functionName): bool + public function isIfOrIfElseWithFunctionCondition(If_ $if, string $functionName): bool { - if (! $this->isIfWithElse($if)) { + if ((bool) $if->elseifs) { return false; }