-
-
Notifications
You must be signed in to change notification settings - Fork 431
[type-declaration-docblocks] Add AddReturnDocblockForCommonObjectDenominatorRector #7249
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
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
28 changes: 28 additions & 0 deletions
28
...orCommonObjectDenominatorRector/AddReturnDocblockForCommonObjectDenominatorRectorTest.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\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddReturnDocblockForCommonObjectDenominatorRectorTest 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'; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...ethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/return_of_objects.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,42 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension; | ||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension; | ||
|
|
||
| final class ReturnOfObjects | ||
| { | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| new FirstExtension(), | ||
| new SecondExtension() | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension; | ||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension; | ||
|
|
||
| final class ReturnOfObjects | ||
| { | ||
| /** | ||
| * @return \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\ExtensionInterface[] | ||
| */ | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| new FirstExtension(), | ||
| new SecondExtension() | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...urnDocblockForCommonObjectDenominatorRector/Fixture/skip_mix_of_object_and_scalar.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension; | ||
|
|
||
| final class ReturnOfObjects | ||
| { | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| new FirstExtension(), | ||
| 555 | ||
| ]; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...turnDocblockForCommonObjectDenominatorRector/Fixture/skip_objects_without_parents.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,17 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstOrphan; | ||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondOrphan; | ||
|
|
||
| final class SkipObjectsWithoutParents | ||
| { | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| new FirstOrphan(), | ||
| new SecondOrphan() | ||
| ]; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...cblockForCommonObjectDenominatorRector/Fixture/skip_objects_without_shared_parent.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,17 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\AnotherExtensionWithDifferentInterface; | ||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension; | ||
|
|
||
| final class SkipObjectsWithoutSharedParent | ||
| { | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| new FirstExtension(), | ||
| new AnotherExtensionWithDifferentInterface(), | ||
| ]; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...lassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Fixture/skip_scalars.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,15 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture; | ||
|
|
||
| final class SkipScalars | ||
| { | ||
| public function getExtensions(): array | ||
| { | ||
| return [ | ||
| 'two', | ||
| 'five' | ||
| ]; | ||
| } | ||
| } | ||
|
|
9 changes: 9 additions & 0 deletions
9
...ocblockForCommonObjectDenominatorRector/Source/AnotherExtensionWithDifferentInterface.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\DifferentInterface; | ||
|
|
||
| final class AnotherExtensionWithDifferentInterface implements DifferentInterface | ||
| { | ||
| } |
8 changes: 8 additions & 0 deletions
8
.../AddReturnDocblockForCommonObjectDenominatorRector/Source/Contract/DifferentInterface.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract; | ||
|
|
||
| interface DifferentInterface | ||
| { | ||
|
|
||
| } |
8 changes: 8 additions & 0 deletions
8
.../AddReturnDocblockForCommonObjectDenominatorRector/Source/Contract/ExtensionInterface.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract; | ||
|
|
||
| interface ExtensionInterface | ||
| { | ||
|
|
||
| } |
9 changes: 9 additions & 0 deletions
9
...r/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Source/FirstExtension.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\ExtensionInterface; | ||
|
|
||
| final class FirstExtension implements ExtensionInterface | ||
| { | ||
| } |
7 changes: 7 additions & 0 deletions
7
...ctor/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Source/FirstOrphan.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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source; | ||
|
|
||
| final class FirstOrphan | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Source/SecondExtension.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 | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source; | ||
|
|
||
| use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\ExtensionInterface; | ||
|
|
||
| final class SecondExtension implements ExtensionInterface | ||
| { | ||
| } |
7 changes: 7 additions & 0 deletions
7
...tor/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/Source/SecondOrphan.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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source; | ||
|
|
||
| final class SecondOrphan | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector/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\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([AddReturnDocblockForCommonObjectDenominatorRector::class]); |
28 changes: 28 additions & 0 deletions
28
...ssMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/function_return.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,28 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture; | ||
|
|
||
| function functionReturn() | ||
| { | ||
| return [ | ||
| 'key' => 'value', | ||
| ]; | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture; | ||
|
|
||
| /** | ||
| * @return array<string, string> | ||
| */ | ||
| function functionReturn() | ||
| { | ||
| return [ | ||
| 'key' => 'value', | ||
| ]; | ||
| } | ||
|
|
||
| ?> |
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
30 changes: 30 additions & 0 deletions
30
rules/TypeDeclarationDocblocks/NodeFinder/ReturnNodeFinder.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,30 @@ | ||||||
| <?php | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| namespace Rector\TypeDeclarationDocblocks\NodeFinder; | ||||||
|
|
||||||
| use PhpParser\Node\Stmt\ClassMethod; | ||||||
| use PhpParser\Node\Stmt\Function_; | ||||||
| use PhpParser\Node\Stmt\Return_; | ||||||
| use Rector\PhpParser\Node\BetterNodeFinder; | ||||||
| use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; | ||||||
|
|
||||||
| final readonly class ReturnNodeFinder | ||||||
| { | ||||||
| public function __construct( | ||||||
| private BetterNodeFinder $betterNodeFinder, | ||||||
| private ReturnAnalyzer $returnAnalyzer, | ||||||
| ) { | ||||||
| } | ||||||
|
|
||||||
| public function findOnlyReturnWithExpr(ClassMethod|Function_ $functionLike): ?Return_ | ||||||
| { | ||||||
| $returnsScoped = $this->betterNodeFinder->findReturnsScoped($functionLike); | ||||||
| if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($functionLike, $returnsScoped)) { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| return $returnsScoped[0]; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs
Suggested change
multiple returns should be skipped on this case for this rule purpose.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
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.
@samsonasik I think this method already covers it.
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.
no,
$returnsScopedcan be many returns, see PR: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.
hasOnlyReturnWithExpris verify that All returns has Expr.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.
I see.