rector/rules/DeadCode/SideEffect/SideEffectNodeDetector.php

127 lines
4.4 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
2022-06-06 16:43:29 +00:00
namespace RectorPrefix20220606\Rector\DeadCode\SideEffect;
use RectorPrefix20220606\Nette\Utils\Strings;
2022-06-06 16:43:29 +00:00
use RectorPrefix20220606\PhpParser\Node;
use RectorPrefix20220606\PhpParser\Node\Expr;
use RectorPrefix20220606\PhpParser\Node\Expr\ArrayDimFetch;
use RectorPrefix20220606\PhpParser\Node\Expr\Assign;
use RectorPrefix20220606\PhpParser\Node\Expr\BinaryOp\Concat;
use RectorPrefix20220606\PhpParser\Node\Expr\FuncCall;
use RectorPrefix20220606\PhpParser\Node\Expr\MethodCall;
use RectorPrefix20220606\PhpParser\Node\Expr\New_;
use RectorPrefix20220606\PhpParser\Node\Expr\NullsafeMethodCall;
use RectorPrefix20220606\PhpParser\Node\Expr\PropertyFetch;
use RectorPrefix20220606\PhpParser\Node\Expr\StaticCall;
use RectorPrefix20220606\PhpParser\Node\Expr\Variable;
use RectorPrefix20220606\PhpParser\Node\Name;
use RectorPrefix20220606\PhpParser\Node\Name\FullyQualified;
use RectorPrefix20220606\PhpParser\Node\Scalar\Encapsed;
use RectorPrefix20220606\PHPStan\Type\ConstantType;
use RectorPrefix20220606\PHPStan\Type\ObjectType;
use RectorPrefix20220606\Rector\NodeTypeResolver\NodeTypeResolver;
final class SideEffectNodeDetector
{
/**
* @var array<class-string<Expr>>
*/
2022-06-06 16:43:29 +00:00
private const SIDE_EFFECT_NODE_TYPES = [Encapsed::class, New_::class, Concat::class, PropertyFetch::class];
/**
* @var array<class-string<Expr>>
*/
2022-06-06 16:43:29 +00:00
private const CALL_EXPR_SIDE_EFFECT_NODE_TYPES = [MethodCall::class, New_::class, NullsafeMethodCall::class, StaticCall::class];
/**
* @readonly
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @readonly
* @var \Rector\DeadCode\SideEffect\PureFunctionDetector
*/
private $pureFunctionDetector;
2022-06-06 16:43:29 +00:00
public function __construct(NodeTypeResolver $nodeTypeResolver, PureFunctionDetector $pureFunctionDetector)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->pureFunctionDetector = $pureFunctionDetector;
}
2022-06-06 16:43:29 +00:00
public function detect(Expr $expr) : bool
{
2022-06-06 16:43:29 +00:00
if ($expr instanceof Assign) {
return \true;
}
$exprStaticType = $this->nodeTypeResolver->getType($expr);
2022-06-06 16:43:29 +00:00
if ($exprStaticType instanceof ConstantType) {
return \false;
}
foreach (self::SIDE_EFFECT_NODE_TYPES as $sideEffectNodeType) {
if (\is_a($expr, $sideEffectNodeType, \true)) {
return \false;
}
}
2022-06-06 16:43:29 +00:00
if ($expr instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($expr);
}
2022-06-06 16:43:29 +00:00
if ($expr instanceof Variable || $expr instanceof ArrayDimFetch) {
$variable = $this->resolveVariable($expr);
// variables don't have side effects
2022-06-06 16:43:29 +00:00
return !$variable instanceof Variable;
}
return \true;
}
2022-06-06 16:43:29 +00:00
public function detectCallExpr(Node $node) : bool
{
2022-06-06 16:43:29 +00:00
if (!$node instanceof Expr) {
return \false;
}
2022-06-06 16:43:29 +00:00
if ($node instanceof StaticCall && $this->isClassCallerThrowable($node)) {
return \false;
}
2022-06-06 16:43:29 +00:00
if ($node instanceof New_ && $this->isPhpParser($node)) {
return \false;
}
$exprClass = \get_class($node);
if (\in_array($exprClass, self::CALL_EXPR_SIDE_EFFECT_NODE_TYPES, \true)) {
return \true;
}
2022-06-06 16:43:29 +00:00
if ($node instanceof FuncCall) {
return !$this->pureFunctionDetector->detect($node);
}
return \false;
}
2022-06-06 16:43:29 +00:00
private function isPhpParser(New_ $new) : bool
{
2022-06-06 16:43:29 +00:00
if (!$new->class instanceof FullyQualified) {
return \false;
}
$className = $new->class->toString();
2022-06-06 16:43:29 +00:00
$namespace = Strings::before($className, '\\', 1);
return $namespace === 'PhpParser';
}
2022-06-06 16:43:29 +00:00
private function isClassCallerThrowable(StaticCall $staticCall) : bool
{
$class = $staticCall->class;
2022-06-06 16:43:29 +00:00
if (!$class instanceof Name) {
return \false;
}
2022-06-06 16:43:29 +00:00
$throwableType = new ObjectType('Throwable');
$type = new ObjectType($class->toString());
return $throwableType->isSuperTypeOf($type)->yes();
}
/**
* @param \PhpParser\Node\Expr\ArrayDimFetch|\PhpParser\Node\Expr\Variable $expr
*/
2022-06-06 16:43:29 +00:00
private function resolveVariable($expr) : ?Variable
{
2022-06-06 16:43:29 +00:00
while ($expr instanceof ArrayDimFetch) {
$expr = $expr->var;
}
2022-06-06 16:43:29 +00:00
if (!$expr instanceof Variable) {
return null;
}
return $expr;
}
}