2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php71;
|
2018-10-03 14:11:30 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
|
|
|
use PhpParser\Node\Name;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeManipulator\BinaryOpManipulator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\Php71\ValueObject\TwoNodeMatch;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Comparing\NodeComparator;
|
2019-07-02 16:04:54 +00:00
|
|
|
final class IsArrayAndDualCheckToAble
|
2018-10-03 14:11:30 +00:00
|
|
|
{
|
2018-10-13 13:47:36 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\BinaryOpManipulator
|
2018-10-13 13:47:36 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $binaryOpManipulator;
|
2018-11-07 17:04:38 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2018-11-07 17:04:38 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $nodeNameResolver;
|
2022-08-13 18:43:10 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Comparing\NodeComparator
|
2022-08-13 18:43:10 +00:00
|
|
|
*/
|
|
|
|
private $nodeComparator;
|
|
|
|
public function __construct(BinaryOpManipulator $binaryOpManipulator, NodeNameResolver $nodeNameResolver, NodeComparator $nodeComparator)
|
2018-10-13 13:47:36 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2022-08-13 18:43:10 +00:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2018-10-13 13:47:36 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function processBooleanOr(BooleanOr $booleanOr, string $type, string $newMethodName) : ?FuncCall
|
2018-10-03 14:11:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($booleanOr, Instanceof_::class, FuncCall::class);
|
|
|
|
if (!$twoNodeMatch instanceof TwoNodeMatch) {
|
2018-10-03 14:11:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
/** @var Instanceof_ $instanceofExpr */
|
|
|
|
$instanceofExpr = $twoNodeMatch->getFirstExpr();
|
|
|
|
/** @var FuncCall $funcCallExpr */
|
|
|
|
$funcCallExpr = $twoNodeMatch->getSecondExpr();
|
|
|
|
$instanceOfClass = $instanceofExpr->class;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($instanceOfClass instanceof Expr) {
|
2020-06-21 14:51:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
if ((string) $instanceOfClass !== $type) {
|
2018-10-03 14:11:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
if (!$this->nodeNameResolver->isName($funcCallExpr, 'is_array')) {
|
2018-10-03 14:11:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-02 12:32:12 +00:00
|
|
|
if ($funcCallExpr->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
if (!isset($funcCallExpr->getArgs()[0])) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-08 07:37:37 +00:00
|
|
|
$firstArg = $funcCallExpr->getArgs()[0];
|
|
|
|
$firstExprNode = $firstArg->value;
|
2022-08-13 18:43:10 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($instanceofExpr->expr, $firstExprNode)) {
|
2018-10-03 14:11:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-13 18:43:10 +00:00
|
|
|
// both use same Expr
|
|
|
|
return new FuncCall(new Name($newMethodName), [new Arg($firstExprNode)]);
|
2018-10-03 14:11:30 +00:00
|
|
|
}
|
|
|
|
}
|