-
-
Notifications
You must be signed in to change notification settings - Fork 424
[dead-code] Add RemoveParentDelegatingConstructorRector #7769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...r/ClassMethod/RemoveParentDelegatingConstructorRector/Fixture/skip_different_args.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\Node\Scalar\String_; | ||
|
|
||
| final class SomeDifferentArgs extends String_ | ||
| { | ||
| public function __construct($value, $attributes) | ||
| { | ||
| parent::__construct($value, []); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
.../ClassMethod/RemoveParentDelegatingConstructorRector/Fixture/skip_different_count.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\Node\Scalar\String_; | ||
|
|
||
| final class SkipDifferentCount extends String_ | ||
| { | ||
| public function __construct($value) | ||
| { | ||
| parent::__construct($value, []); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...r/ClassMethod/RemoveParentDelegatingConstructorRector/Fixture/skip_different_type.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\NodeTraverser; | ||
| use Rector\Comments\NodeVisitor\CommentRemovingNodeVisitor; | ||
|
|
||
| final class SkipDifferentType extends NodeTraverser | ||
| { | ||
| public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor) | ||
| { | ||
| parent::__construct($commentRemovingNodeVisitor); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...sMethod/RemoveParentDelegatingConstructorRector/Fixture/skip_first_class_callable.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\Node\Scalar\String_; | ||
|
|
||
| final class SkipFirstClassCallable extends String_ | ||
| { | ||
| public function __construct($value, $attributes) | ||
| { | ||
| parent::__construct(...); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...ode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector/Fixture/some_class.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\Node\Scalar\String_; | ||
|
|
||
| final class SomeClass extends String_ | ||
| { | ||
| public function __construct($value, $attributes) | ||
| { | ||
| parent::__construct($value, $attributes); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture; | ||
|
|
||
| use PhpParser\Node\Scalar\String_; | ||
|
|
||
| final class SomeClass extends String_ | ||
| { | ||
| } | ||
|
|
||
| ?> |
28 changes: 28 additions & 0 deletions
28
...d/RemoveParentDelegatingConstructorRector/RemoveParentDelegatingConstructorRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class RemoveParentDelegatingConstructorRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...ode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([RemoveParentDelegatingConstructorRector::class]); |
238 changes: 238 additions & 0 deletions
238
rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\DeadCode\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Stmt; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use PhpParser\NodeVisitor; | ||
| use PHPStan\Reflection\ClassReflection; | ||
| use PHPStan\Reflection\ExtendedMethodReflection; | ||
| use Rector\Enum\ObjectReference; | ||
| use Rector\PHPStan\ScopeFetcher; | ||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||
| use Rector\ValueObject\MethodName; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\RemoveParentDelegatingConstructorRectorTest | ||
| */ | ||
| final class RemoveParentDelegatingConstructorRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly StaticTypeMapper $staticTypeMapper, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Remove constructor that only delegates call to parent class with same values', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class Node | ||
| { | ||
| public function __construct(array $attributes) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| class SomeParent extends Node | ||
| { | ||
| public function __construct(array $attributes) | ||
| { | ||
| parent::__construct($attributes); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class Node | ||
| { | ||
| public function __construct(array $attributes) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| class SomeParent extends Node | ||
| { | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethod $node | ||
| */ | ||
| public function refactor(Node $node): ?int | ||
| { | ||
| if (! $this->isName($node, MethodName::CONSTRUCT)) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->stmts === null || count($node->stmts) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $parentMethodReflection = $this->matchParentConstructorReflection($node); | ||
| if (! $parentMethodReflection instanceof ExtendedMethodReflection) { | ||
| return null; | ||
| } | ||
|
|
||
| $soleStmt = $node->stmts[0]; | ||
| $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); | ||
| if ($parentCallArgs === null) { | ||
| return null; | ||
| } | ||
|
|
||
| // match count and order | ||
| if (! $this->isParameterAndArgCountAndOrderIdentical($node)) { | ||
| return null; | ||
| } | ||
|
|
||
| // match parameter types and parent constructor types | ||
| if (! $this->areConstructorAndParentParameterTypesMatching($node, $parentMethodReflection)) { | ||
| return null; | ||
| } | ||
|
|
||
| return NodeVisitor::REMOVE_NODE; | ||
| } | ||
|
|
||
| private function matchParentConstructorReflection(ClassMethod $classMethod): ?ExtendedMethodReflection | ||
| { | ||
| $scope = ScopeFetcher::fetch($classMethod); | ||
|
|
||
| $classReflection = $scope->getClassReflection(); | ||
| if (! $classReflection instanceof ClassReflection) { | ||
| return null; | ||
| } | ||
|
|
||
| $parentClassReflection = $classReflection->getParentClass(); | ||
| if (! $parentClassReflection instanceof ClassReflection) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $parentClassReflection->hasConstructor()) { | ||
| return null; | ||
| } | ||
|
|
||
| return $parentClassReflection->getConstructor(); | ||
| } | ||
|
|
||
| /** | ||
| * Looking for parent::__construct() | ||
| * | ||
| * @return Arg[]|null | ||
| */ | ||
| private function matchParentConstructorCallArgs(Stmt $stmt): ?array | ||
| { | ||
| if (! $stmt instanceof Expression) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $stmt->expr instanceof StaticCall) { | ||
| return null; | ||
| } | ||
|
|
||
| $staticCall = $stmt->expr; | ||
| if ($staticCall->isFirstClassCallable()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $this->isName($staticCall->class, ObjectReference::PARENT)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $this->isName($staticCall->name, MethodName::CONSTRUCT)) { | ||
| return null; | ||
| } | ||
|
|
||
| return $staticCall->getArgs(); | ||
| } | ||
|
|
||
| private function isParameterAndArgCountAndOrderIdentical(ClassMethod $classMethod): bool | ||
| { | ||
| $soleStmt = $classMethod->stmts[0]; | ||
|
|
||
| $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); | ||
| if ($parentCallArgs === null) { | ||
| return false; | ||
| } | ||
|
|
||
| $constructorParams = $classMethod->getParams(); | ||
| if (count($constructorParams) !== count($parentCallArgs)) { | ||
| return false; | ||
| } | ||
|
|
||
| // match passed names in the same order | ||
| $paramNames = []; | ||
| foreach ($constructorParams as $constructorParam) { | ||
| $paramNames[] = $this->getName($constructorParam->var); | ||
| } | ||
|
|
||
| $argNames = []; | ||
| foreach ($parentCallArgs as $parentCallArg) { | ||
| $argValue = $parentCallArg->value; | ||
| if (! $argValue instanceof Variable) { | ||
| return false; | ||
| } | ||
|
|
||
| $argNames[] = $this->getName($argValue); | ||
| } | ||
|
|
||
| return $paramNames === $argNames; | ||
| } | ||
|
|
||
| private function areConstructorAndParentParameterTypesMatching( | ||
| ClassMethod $classMethod, | ||
| ExtendedMethodReflection $extendedMethodReflection | ||
| ): bool { | ||
| foreach ($classMethod->getParams() as $position => $param) { | ||
| $parameterType = $param->type; | ||
|
|
||
| // no type override | ||
| if ($parameterType === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $parametersSelector = $extendedMethodReflection->getOnlyVariant(); | ||
|
|
||
| foreach ($parametersSelector->getParameters() as $index => $parameterReflection) { | ||
| if ($index !== $position) { | ||
| continue; | ||
| } | ||
|
|
||
| $parentParameterType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( | ||
| $parameterReflection->getType(), | ||
| TypeKind::PARAM | ||
| ); | ||
|
|
||
| if (! $this->nodeComparator->areNodesEqual($parameterType, $parentParameterType)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.