-
-
Notifications
You must be signed in to change notification settings - Fork 426
[Deadcode]: add handling for void magic methods #7781
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
Open
parijke
wants to merge
1
commit into
rectorphp:main
Choose a base branch
from
parijke:feature/remove-void-from-magic-methods-as-return-docblock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions
35
...UselessVoidReturnFromDocblockOnVoidMagicMethodsRector/Fixture/remove_return_clone.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,35 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnClone | ||
| { | ||
| /** | ||
| * Other comments | ||
| * | ||
| * @return void | ||
| */ | ||
| function __clone() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnClone | ||
| { | ||
| /** | ||
| * Other comments | ||
| */ | ||
| function __clone() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> |
30 changes: 30 additions & 0 deletions
30
...essVoidReturnFromDocblockOnVoidMagicMethodsRector/Fixture/remove_return_construct.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,30 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnConstruct | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __construct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnConstruct | ||
| { | ||
| function __construct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> |
30 changes: 30 additions & 0 deletions
30
...lessVoidReturnFromDocblockOnVoidMagicMethodsRector/Fixture/remove_return_destruct.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,30 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnDestruct | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __destruct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\Fixture; | ||
|
|
||
| class RemoveReturnDestruct | ||
| { | ||
| function __destruct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> |
28 changes: 28 additions & 0 deletions
28
...nVoidMagicMethodsRector/RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest.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\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest 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
...od/RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector/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\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector::class]); |
112 changes: 112 additions & 0 deletions
112
...DeadCode/Rector/ClassMethod/RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector.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,112 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\DeadCode\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; | ||
| use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
| use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
| use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest | ||
| */ | ||
| final class RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly DocBlockUpdater $docBlockUpdater, | ||
| private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Remove useless @return void docblock from magic methods __construct, __destruct, and __clone', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| public function __construct() {} | ||
|
|
||
| /** | ||
| * @return void | ||
| */ | ||
| public function __destruct() {} | ||
|
|
||
| /** | ||
| * @return void | ||
| */ | ||
| public function __clone() {} | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function __construct() {} | ||
|
|
||
| public function __destruct() {} | ||
|
|
||
| public function __clone() {} | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethod $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($node->returnType instanceof Node) { | ||
| return null; | ||
| } | ||
|
|
||
| $magicMethodNames = ['__construct', '__destruct', '__clone']; | ||
|
|
||
| $methodName = $this->getName($node); | ||
|
|
||
| if (! in_array($methodName, $magicMethodNames, true)) { | ||
| return null; | ||
| } | ||
|
|
||
| $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
|
|
||
| $returnTagValueNode = $phpDocInfo->getReturnTagValue(); | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (! $returnTagValueNode instanceof ReturnTagValueNode) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($returnTagValueNode->description !== '') { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $returnTagValueNode->type instanceof IdentifierTypeNode || $returnTagValueNode->type->__toString() !== 'void') { | ||
| return null; | ||
| } | ||
|
|
||
| $phpDocInfo->removeByType(ReturnTagValueNode::class); | ||
|
|
||
| $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
| return $node; | ||
| } | ||
| } | ||
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.