Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -97,17 +98,24 @@ 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());
}

$this->addNodeAfterNode($ifStmt, $node);
}

$this->removeNode($node);

return null;
}
}
4 changes: 2 additions & 2 deletions src/PhpParser/Node/Manipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down