2018-10-03 22:11:30 +08:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php;
|
|
|
|
|
2018-11-07 21:53:19 +01:00
|
|
|
use PhpParser\Node\Arg;
|
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;
|
2019-02-27 22:54:39 +01:00
|
|
|
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
|
2018-11-04 02:17:02 +01:00
|
|
|
use Rector\PhpParser\Node\Resolver\NameResolver;
|
2018-10-03 22:11:30 +08:00
|
|
|
|
|
|
|
final class DualCheckToAble
|
|
|
|
{
|
2018-10-13 21:47:36 +08:00
|
|
|
/**
|
2018-10-15 16:46:15 +08:00
|
|
|
* @var NameResolver
|
2018-10-13 21:47:36 +08:00
|
|
|
*/
|
2018-10-15 16:46:15 +08:00
|
|
|
private $nameResolver;
|
2018-10-13 21:47:36 +08:00
|
|
|
|
2018-11-07 18:04:38 +01:00
|
|
|
/**
|
2019-02-27 22:54:39 +01:00
|
|
|
* @var BinaryOpManipulator
|
2018-11-07 18:04:38 +01:00
|
|
|
*/
|
2019-02-27 22:54:39 +01:00
|
|
|
private $binaryOpManipulator;
|
2018-11-07 18:04:38 +01:00
|
|
|
|
2019-02-27 22:54:39 +01:00
|
|
|
public function __construct(NameResolver $nameResolver, BinaryOpManipulator $binaryOpManipulator)
|
2018-10-13 21:47:36 +08:00
|
|
|
{
|
2018-10-15 16:46:15 +08:00
|
|
|
$this->nameResolver = $nameResolver;
|
2019-02-27 22:54:39 +01:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2018-10-13 21:47:36 +08:00
|
|
|
}
|
|
|
|
|
2019-02-22 18:25:31 +01:00
|
|
|
public function processBooleanOr(BooleanOr $booleanOr, string $type, string $newMethodName): ?FuncCall
|
2018-10-03 22:11:30 +08:00
|
|
|
{
|
2019-02-27 22:54:39 +01:00
|
|
|
$matchedNodes = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
|
2019-02-22 18:25:31 +01:00
|
|
|
$booleanOr,
|
2018-11-09 23:27:07 +01:00
|
|
|
Instanceof_::class,
|
|
|
|
FuncCall::class
|
2018-11-07 18:04:38 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($matchedNodes === null) {
|
2018-10-03 22:11:30 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:22:27 +01:00
|
|
|
/** @var Instanceof_ $instanceOfNode */
|
|
|
|
/** @var FuncCall $funcCallNode */
|
2018-11-07 18:04:38 +01:00
|
|
|
[$instanceOfNode, $funcCallNode] = $matchedNodes;
|
2018-10-03 22:11:30 +08:00
|
|
|
|
|
|
|
if ((string) $instanceOfNode->class !== $type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var FuncCall $funcCallNode */
|
2018-10-15 16:46:15 +08:00
|
|
|
if ($this->nameResolver->resolve($funcCallNode) !== 'is_array') {
|
2018-10-03 22:11:30 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both use same var
|
|
|
|
if (! $funcCallNode->args[0]->value instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Variable $firstVarNode */
|
|
|
|
$firstVarNode = $funcCallNode->args[0]->value;
|
|
|
|
|
|
|
|
if (! $instanceOfNode->expr instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Variable $secondVarNode */
|
|
|
|
$secondVarNode = $instanceOfNode->expr;
|
|
|
|
|
|
|
|
// are they same variables
|
|
|
|
if ($firstVarNode->name !== $secondVarNode->name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:53:19 +01:00
|
|
|
return new FuncCall(new Name($newMethodName), [new Arg($firstVarNode)]);
|
2018-10-03 22:11:30 +08:00
|
|
|
}
|
|
|
|
}
|