-
-
Notifications
You must be signed in to change notification settings - Fork 424
feature: remove @return void from magic methods #7678
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
Draft
parijke
wants to merge
1
commit into
rectorphp:main
Choose a base branch
from
parijke:feature/remove-void-from-magic-methods-as-return
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.
Draft
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
16 changes: 16 additions & 0 deletions
16
...dCode/Rector/ClassMethod/RemoveUselessReturnTagRector/Fixture/remove_return_construct.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,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture; | ||
|
|
||
| class RemoveReturnConstruct | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __construct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture; | ||
|
|
||
| class RemoveReturnClone | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __clone() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture; | ||
|
|
||
| class RemoveReturnClone | ||
| { | ||
| 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\RemoveUselessReturnTagRector\Fixture; | ||
|
|
||
| class RemoveReturnConstruct | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __construct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\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\RemoveUselessReturnTagRector\Fixture; | ||
|
|
||
| class RemoveReturnDestruct | ||
| { | ||
| /** | ||
| * @return void | ||
| */ | ||
| function __destruct() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\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'; | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...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,8 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([\Rector\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector::class]); |
103 changes: 103 additions & 0 deletions
103
...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,103 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\DeadCode\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
| use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessVoidReturnFromDocblockOnVoidMagicMethodsRector\RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRectorTest | ||
| */ | ||
| 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 \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 instanceof ClassMethod) { | ||
| 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(); | ||
| if ($returnTagValueNode === null) { | ||
| return null; | ||
| } | ||
|
|
||
| // Remove only if it's exactly @return void | ||
| $returnType = (string) $returnTagValueNode->type; | ||
| if (strtolower($returnType) !== 'void') { | ||
| return null; | ||
| } | ||
|
|
||
| $phpDocInfo->removeByType(get_class($returnTagValueNode)); | ||
| $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
| return $node; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move the fixture under new namespace directory
RemoveUselessVoidReturnFromDocblockVoidMagicMethodsRector