mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 20:39:43 +01:00
Fix UnwrapFutureCompatibleIfFunctionExistsRector for no else [c… (#2706)
Fix UnwrapFutureCompatibleIfFunctionExistsRector for no else [closes #2691]
This commit is contained in:
commit
4ca86631cd
@ -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
|
||||
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
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\CodeSample;
|
||||
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 @@ PHP
|
||||
*/
|
||||
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 @@ PHP
|
||||
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 @@ PHP
|
||||
|
||||
$this->addNodeAfterNode($ifStmt, $node);
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -248,9 +248,9 @@ final class IfManipulator
|
||||
* } 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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user