Skip to content

Commit

Permalink
Removing parent node calls (#4137)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 9, 2023
1 parent 8fe22e4 commit af780c8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 79 deletions.
22 changes: 2 additions & 20 deletions build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 371 Rules Overview
# 370 Rules Overview

<br>

Expand All @@ -10,7 +10,7 @@

- [CodingStyle](#codingstyle) (34)

- [DeadCode](#deadcode) (44)
- [DeadCode](#deadcode) (43)

- [DependencyInjection](#dependencyinjection) (1)

Expand Down Expand Up @@ -2678,24 +2678,6 @@ Remove operation with 1 and 0, that have no effect on the value

<br>

### RemoveDelegatingParentCallRector

Removed dead parent call, that does not change anything

- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector`](../rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php)

```diff
class SomeClass
{
- public function prettyPrint(array $stmts): string
- {
- return parent::prettyPrint($stmts);
- }
}
```

<br>

### RemoveDoubleAssignRector

Simplify useless double assigns
Expand Down
Expand Up @@ -140,8 +140,7 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
}

if ($this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
$class = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
return $class instanceof Class_ && $class->extends instanceof FullyQualified;
return $class->extends instanceof FullyQualified;
}

return $this->nodeNameResolver->isName($classMethod, MethodName::INVOKE);
Expand Down
Expand Up @@ -6,13 +6,12 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -21,7 +20,7 @@
/**
* @see \Rector\Tests\DependencyInjection\Rector\ClassMethod\AddMethodParentCallRector\AddMethodParentCallRectorTest
*/
final class AddMethodParentCallRector extends AbstractRector implements ConfigurableRectorInterface
final class AddMethodParentCallRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
{
/**
* @var array<string, string>
Expand Down Expand Up @@ -75,31 +74,29 @@ public function getNodeTypes(): array
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);
if (! $classLike instanceof ClassLike) {
if (! $scope->isInClass()) {
return null;
}

$className = (string) $this->nodeNameResolver->getName($classLike);
$classReflection = $scope->getClassReflection();

foreach ($this->methodByParentTypes as $type => $method) {
// not itself
if ($className === $type) {
if ($classReflection->getName() === $type) {
continue;
}

if ($this->shouldSkipMethod($node, $method)) {
continue;
}

if (! $this->isObjectType($classLike, new ObjectType($type))) {
if (! $classReflection->isSubclassOf($type)) {
continue;
}

$node->stmts[] = $this->createParentStaticCall($method);

return $node;
}

Expand Down
23 changes: 8 additions & 15 deletions rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php
Expand Up @@ -4,12 +4,11 @@

namespace Rector\Php55\Rector\FuncCall;

use PHPStan\Analyser\Scope;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -20,13 +19,8 @@
* @changelog https://3v4l.org/GU9dP
* @see \Rector\Tests\Php55\Rector\FuncCall\GetCalledClassToSelfClassRector\GetCalledClassToSelfClassRectorTest
*/
final class GetCalledClassToSelfClassRector extends AbstractRector implements MinPhpVersionInterface
final class GetCalledClassToSelfClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassAnalyzer $classAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change get_called_class() to self::class on final class', [
Expand Down Expand Up @@ -65,23 +59,22 @@ public function getNodeTypes(): array
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if (! $this->isName($node, 'get_called_class')) {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);

if (! $class instanceof Class_) {
if (! $scope->isInClass()) {
return null;
}

if ($class->isFinal()) {
$classReflection = $scope->getClassReflection();
if ($classReflection->isFinalByKeyword()) {
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF, 'class');
}

if ($this->classAnalyzer->isAnonymousClass($class)) {
if ($classReflection->isAnonymous()) {
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF, 'class');
}

Expand Down
24 changes: 9 additions & 15 deletions rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php
Expand Up @@ -6,10 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -20,13 +19,8 @@
* @changelog https://3v4l.org/dJgXd
* @see \Rector\Tests\Php55\Rector\FuncCall\GetCalledClassToStaticClassRector\GetCalledClassToStaticClassRectorTest
*/
final class GetCalledClassToStaticClassRector extends AbstractRector implements MinPhpVersionInterface
final class GetCalledClassToStaticClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassAnalyzer $classAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change get_called_class() to static::class on non-final class', [
Expand Down Expand Up @@ -65,22 +59,22 @@ public function getNodeTypes(): array
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if (! $this->isName($node, 'get_called_class')) {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC, 'class');
if (! $scope->isInClass()) {
return null;
}

if ($this->classAnalyzer->isAnonymousClass($class)) {
$classReflection = $scope->getClassReflection();
if ($classReflection->isAnonymous()) {
return null;
}

if (! $class->isFinal()) {
if (! $classReflection->isFinal()) {
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC, 'class');
}

Expand Down
Expand Up @@ -10,7 +10,8 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -25,7 +26,7 @@
*
* @see \Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\ClassOnThisVariableObjectRectorTest
*/
final class ClassOnThisVariableObjectRector extends AbstractRector implements MinPhpVersionInterface
final class ClassOnThisVariableObjectRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -62,27 +63,36 @@ public function run()
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class];
return [Class_::class];
}

/**
* @param ClassConstFetch $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$className = $node->isFinal() ? 'self' : 'static';

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return null;
}
$hasChanged = false;
$this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged, $className): ?ClassConstFetch {
if (! $node instanceof ClassConstFetch) {
return null;
}

if ($this->shouldSkip($node)) {
return null;
}

$className = $class->isFinal() ? 'self' : 'static';
$node->class = new Name($className);
$node->class = new Name($className);
$hasChanged = true;
return $node;
});

if ($hasChanged) {
return $node;
}

return $node;
return null;
}

public function provideMinPhpVersion(): int
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v82';
private const CACHE_KEY = 'v83';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit af780c8

Please sign in to comment.