rector/rules/Php71/IsArrayAndDualCheckToAble.php

69 lines
2.6 KiB
PHP
Raw Normal View History

2019-10-13 07:59:52 +02:00
<?php
declare (strict_types=1);
2019-09-23 18:05:19 +02:00
namespace Rector\Php71;
2018-10-03 22:11:30 +08:00
2018-11-07 21:53:19 +01:00
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
2018-10-03 22:11:30 +08:00
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use Rector\Core\NodeManipulator\BinaryOpManipulator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php71\ValueObject\TwoNodeMatch;
final class IsArrayAndDualCheckToAble
2018-10-03 22:11:30 +08:00
{
/**
* @var \Rector\Core\NodeManipulator\BinaryOpManipulator
*/
private $binaryOpManipulator;
2018-11-07 18:04:38 +01:00
/**
* @var \Rector\NodeNameResolver\NodeNameResolver
2018-11-07 18:04:38 +01:00
*/
private $nodeNameResolver;
public function __construct(\Rector\Core\NodeManipulator\BinaryOpManipulator $binaryOpManipulator, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver)
{
$this->binaryOpManipulator = $binaryOpManipulator;
$this->nodeNameResolver = $nodeNameResolver;
}
public function processBooleanOr(\PhpParser\Node\Expr\BinaryOp\BooleanOr $booleanOr, string $type, string $newMethodName) : ?\PhpParser\Node\Expr\FuncCall
2018-10-03 22:11:30 +08:00
{
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($booleanOr, \PhpParser\Node\Expr\Instanceof_::class, \PhpParser\Node\Expr\FuncCall::class);
if (!$twoNodeMatch instanceof \Rector\Php71\ValueObject\TwoNodeMatch) {
2018-10-03 22:11:30 +08:00
return null;
}
/** @var Instanceof_ $instanceOf */
$instanceOf = $twoNodeMatch->getFirstExpr();
/** @var FuncCall $funcCall */
$funcCall = $twoNodeMatch->getSecondExpr();
$instanceOfClass = $instanceOf->class;
if ($instanceOfClass instanceof \PhpParser\Node\Expr) {
2020-06-21 16:51:14 +02:00
return null;
}
if ((string) $instanceOfClass !== $type) {
2018-10-03 22:11:30 +08:00
return null;
}
if (!$this->nodeNameResolver->isName($funcCall, 'is_array')) {
2018-10-03 22:11:30 +08:00
return null;
}
// both use same var
if (!$funcCall->args[0]->value instanceof \PhpParser\Node\Expr\Variable) {
2018-10-03 22:11:30 +08:00
return null;
}
/** @var Variable $firstVarNode */
$firstVarNode = $funcCall->args[0]->value;
if (!$instanceOf->expr instanceof \PhpParser\Node\Expr\Variable) {
2018-10-03 22:11:30 +08:00
return null;
}
/** @var Variable $secondVarNode */
$secondVarNode = $instanceOf->expr;
2018-10-03 22:11:30 +08:00
// are they same variables
if ($firstVarNode->name !== $secondVarNode->name) {
return null;
}
return new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name($newMethodName), [new \PhpParser\Node\Arg($firstVarNode)]);
2018-10-03 22:11:30 +08:00
}
}