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\DeadCode\Rector\StaticCall;
|
2018-12-25 14:14:32 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-05-25 14:40:36 +00:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
|
|
|
use Rector\NodeAnalyzer\ClassAnalyzer;
|
|
|
|
use Rector\NodeManipulator\ClassMethodManipulator;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\RemoveParentCallWithoutParentRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2023-05-25 14:40:36 +00:00
|
|
|
final class RemoveParentCallWithoutParentRector extends AbstractRector
|
2018-12-25 14:14:32 +00:00
|
|
|
{
|
2018-12-25 15:01:25 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\ClassMethodManipulator
|
2018-12-25 15:01:25 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $classMethodManipulator;
|
2021-04-23 19:32:59 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ClassAnalyzer
|
2021-04-23 19:32:59 +00:00
|
|
|
*/
|
|
|
|
private $classAnalyzer;
|
2021-12-17 00:05:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-12-17 00:05:48 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2023-05-25 14:40:36 +00:00
|
|
|
public function __construct(ClassMethodManipulator $classMethodManipulator, ClassAnalyzer $classAnalyzer, ReflectionProvider $reflectionProvider)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->classMethodManipulator = $classMethodManipulator;
|
2021-04-23 19:32:59 +00:00
|
|
|
$this->classAnalyzer = $classAnalyzer;
|
2021-12-17 00:05:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2018-12-25 15:01:25 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-25 14:14:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unused parent call with no parent class', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-25 14:14:32 +00:00
|
|
|
class OrphanClass
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-25 14:14:32 +00:00
|
|
|
class OrphanClass
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-25 14:14:32 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-25 14:14:32 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-25 14:14:32 +00:00
|
|
|
{
|
2023-05-25 14:40:36 +00:00
|
|
|
return [Class_::class];
|
2018-12-25 14:14:32 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-25 14:40:36 +00:00
|
|
|
* @param Class_ $node
|
2018-12-25 14:14:32 +00:00
|
|
|
*/
|
2023-05-25 14:40:36 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-25 14:14:32 +00:00
|
|
|
{
|
2023-05-25 14:40:36 +00:00
|
|
|
if ($this->shouldSkipClass($node)) {
|
2019-12-04 21:06:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
$hasChanged = \false;
|
2023-05-25 14:40:36 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
2023-06-05 13:42:47 +00:00
|
|
|
if ($classMethod->stmts === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($classMethod->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Expression) {
|
|
|
|
continue;
|
2023-05-25 14:40:36 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
if ($stmt->expr instanceof StaticCall && $this->isParentStaticCall($stmt->expr)) {
|
|
|
|
if ($this->doesCalledMethodExistInParent($stmt->expr, $node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($classMethod->stmts[$key]);
|
|
|
|
$hasChanged = \true;
|
2023-05-25 14:40:36 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
if ($stmt->expr instanceof Assign) {
|
|
|
|
$assign = $stmt->expr;
|
|
|
|
if ($assign->expr instanceof StaticCall && $this->isParentStaticCall($assign->expr)) {
|
|
|
|
$staticCall = $assign->expr;
|
|
|
|
// is valid call
|
|
|
|
if ($this->doesCalledMethodExistInParent($staticCall, $node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$assign->expr = $this->nodeFactory->createNull();
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
2023-05-25 14:40:36 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
}
|
2018-12-25 15:01:25 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2019-01-25 00:49:26 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
return null;
|
2023-05-25 14:40:36 +00:00
|
|
|
}
|
|
|
|
private function isParentStaticCall(Expr $expr) : bool
|
|
|
|
{
|
|
|
|
if (!$expr instanceof StaticCall) {
|
|
|
|
return \false;
|
2018-12-25 14:14:32 +00:00
|
|
|
}
|
2023-05-25 14:40:36 +00:00
|
|
|
return $this->isName($expr->class, ObjectReference::PARENT);
|
2018-12-25 14:14:32 +00:00
|
|
|
}
|
2023-05-25 14:40:36 +00:00
|
|
|
private function shouldSkipClass(Class_ $class) : bool
|
2021-12-31 12:16:48 +00:00
|
|
|
{
|
2023-06-05 13:42:47 +00:00
|
|
|
// skip cases when parent class reflection is not found
|
2023-05-25 14:40:36 +00:00
|
|
|
if ($class->extends instanceof FullyQualified && !$this->reflectionProvider->hasClass($class->extends->toString())) {
|
2021-12-31 12:16:48 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2023-05-25 14:40:36 +00:00
|
|
|
// currently the classMethodManipulator isn't able to find usages of anonymous classes
|
|
|
|
return $this->classAnalyzer->isAnonymousClass($class);
|
|
|
|
}
|
|
|
|
private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $class) : bool
|
|
|
|
{
|
|
|
|
if (!$class->extends instanceof Name) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$calledMethodName = $this->getName($staticCall->name);
|
|
|
|
if (!\is_string($calledMethodName)) {
|
|
|
|
return \false;
|
2021-12-31 12:16:48 +00:00
|
|
|
}
|
2023-05-25 14:40:36 +00:00
|
|
|
return $this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $calledMethodName);
|
2021-12-31 12:16:48 +00:00
|
|
|
}
|
2018-12-25 14:14:32 +00:00
|
|
|
}
|