2018-10-03 22:11:30 +08:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php;
|
|
|
|
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
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;
|
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-10-15 16:46:15 +08:00
|
|
|
public function __construct(NameResolver $nameResolver)
|
2018-10-13 21:47:36 +08:00
|
|
|
{
|
2018-10-15 16:46:15 +08:00
|
|
|
$this->nameResolver = $nameResolver;
|
2018-10-13 21:47:36 +08:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:11:30 +08:00
|
|
|
public function processBooleanOr(BooleanOr $booleanOrNode, string $type, string $newMethodName): ?FuncCall
|
|
|
|
{
|
|
|
|
$split = $this->splitToInstanceOfAndFuncCall($booleanOrNode);
|
|
|
|
if ($split === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
[$instanceOfNode, $funcCallNode] = $split;
|
|
|
|
|
|
|
|
/** @var Instanceof_ $instanceOfNode */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$funcCallNode = new FuncCall(new Name($newMethodName));
|
|
|
|
$funcCallNode->args[0] = new Arg($firstVarNode);
|
|
|
|
|
|
|
|
return $funcCallNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Instanceof_[]|FuncCall[]
|
|
|
|
*/
|
|
|
|
private function splitToInstanceOfAndFuncCall(BooleanOr $booleanOrNode): ?array
|
|
|
|
{
|
|
|
|
if ($booleanOrNode->left instanceof Instanceof_ && $booleanOrNode->right instanceof FuncCall) {
|
|
|
|
return [$booleanOrNode->left, $booleanOrNode->right];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($booleanOrNode->right instanceof Instanceof_ && $booleanOrNode->left instanceof FuncCall) {
|
|
|
|
return [$booleanOrNode->right, $booleanOrNode->left];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|