2020-02-09 12:31:31 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\NodeManipulator;
|
2020-02-09 12:31:31 +01:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
|
|
|
|
use PhpParser\Node\Expr\BitwiseNot;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\Cast;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
|
|
|
use PhpParser\Node\Expr\Clone_;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Expr\Empty_;
|
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
|
|
|
use PhpParser\Node\Expr\Isset_;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\UnaryMinus;
|
|
|
|
use PhpParser\Node\Expr\UnaryPlus;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-02-09 12:31:31 +01:00
|
|
|
final class LivingCodeManipulator
|
|
|
|
{
|
2021-08-17 10:45:55 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-17 10:45:55 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(NodeTypeResolver $nodeTypeResolver)
|
2020-11-16 17:50:38 +00:00
|
|
|
{
|
2021-08-17 10:45:55 +00:00
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
2020-11-16 17:50:38 +00:00
|
|
|
}
|
2020-02-09 12:31:31 +01:00
|
|
|
/**
|
2020-11-01 16:39:04 +01:00
|
|
|
* @return Expr[]|mixed[]
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node|int|string|null $expr
|
2020-02-09 12:31:31 +01:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function keepLivingCodeFromExpr($expr) : array
|
2020-02-09 12:31:31 +01:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof Expr) {
|
2020-07-27 08:56:25 +02:00
|
|
|
return [];
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Closure || $expr instanceof Scalar || $expr instanceof ConstFetch) {
|
2020-02-09 12:31:31 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if ($this->isNestedExpr($expr)) {
|
|
|
|
return $this->keepLivingCodeFromExpr($expr->expr);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Variable) {
|
2020-02-09 12:31:31 +01:00
|
|
|
return $this->keepLivingCodeFromExpr($expr->name);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof PropertyFetch) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($this->keepLivingCodeFromExpr($expr->var), $this->keepLivingCodeFromExpr($expr->name));
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof ArrayDimFetch) {
|
2021-10-07 19:40:12 +00:00
|
|
|
$type = $this->nodeTypeResolver->getType($expr->var);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof ObjectType) {
|
|
|
|
$objectType = new ObjectType('ArrayAccess');
|
2021-08-17 10:45:55 +00:00
|
|
|
if ($objectType->isSuperTypeOf($type)->yes()) {
|
|
|
|
return [$expr];
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($this->keepLivingCodeFromExpr($expr->var), $this->keepLivingCodeFromExpr($expr->dim));
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof ClassConstFetch || $expr instanceof StaticPropertyFetch) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($this->keepLivingCodeFromExpr($expr->class), $this->keepLivingCodeFromExpr($expr->name));
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
|
|
|
if ($this->isBinaryOpWithoutChange($expr)) {
|
2020-07-27 08:56:25 +02:00
|
|
|
/** @var BinaryOp $binaryOp */
|
|
|
|
$binaryOp = $expr;
|
|
|
|
return $this->processBinary($binaryOp);
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Instanceof_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($this->keepLivingCodeFromExpr($expr->expr), $this->keepLivingCodeFromExpr($expr->class));
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Isset_) {
|
2020-07-27 08:56:25 +02:00
|
|
|
return $this->processIsset($expr);
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
|
|
|
return [$expr];
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isNestedExpr(Expr $expr) : bool
|
2020-04-26 02:57:47 +02:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return $expr instanceof Cast || $expr instanceof Empty_ || $expr instanceof UnaryMinus || $expr instanceof UnaryPlus || $expr instanceof BitwiseNot || $expr instanceof BooleanNot || $expr instanceof Clone_;
|
2020-04-26 02:57:47 +02:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isBinaryOpWithoutChange(Expr $expr) : bool
|
2020-02-09 12:31:31 +01:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof BinaryOp) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-07-27 08:56:25 +02:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return !($expr instanceof LogicalAnd || $expr instanceof BooleanAnd || $expr instanceof LogicalOr || $expr instanceof BooleanOr || $expr instanceof Coalesce);
|
2020-07-27 08:56:25 +02:00
|
|
|
}
|
2020-08-11 12:59:04 +02:00
|
|
|
/**
|
2020-08-29 11:03:40 +02:00
|
|
|
* @return Expr[]
|
2020-08-11 12:59:04 +02:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinary(BinaryOp $binaryOp) : array
|
2020-07-27 08:56:25 +02:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_merge($this->keepLivingCodeFromExpr($binaryOp->left), $this->keepLivingCodeFromExpr($binaryOp->right));
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|
2020-08-11 12:59:04 +02:00
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processIsset(Isset_ $isset) : array
|
2020-08-05 22:45:36 +02:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
$livingExprs = [];
|
|
|
|
foreach ($isset->vars as $expr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$livingExprs = \array_merge($livingExprs, $this->keepLivingCodeFromExpr($expr));
|
2020-11-16 17:50:38 +00:00
|
|
|
}
|
|
|
|
return $livingExprs;
|
2020-08-05 22:45:36 +02:00
|
|
|
}
|
2020-02-09 12:31:31 +01:00
|
|
|
}
|