-
-
Notifications
You must be signed in to change notification settings - Fork 426
[TypeDeclaration] Make configurable on NarrowObjectReturnTypeRector to allow to not make change return on abstract/interface return #7695
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
28 changes: 28 additions & 0 deletions
28
.../Rector/ClassMethod/NarrowObjectReturnTypeRector/DisableReturnAbstractOrInterfaceTest.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\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class DisableReturnAbstractOrInterfaceTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/FixtureSkipReturnAbstractOrInterface'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/disable_return_abstract_or_interface_configured_rule.php'; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...owObjectReturnTypeRector/FixtureSkipReturnAbstractOrInterface/skip_abstract_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,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\FixtureSkipReturnAbstractOrInterface; | ||
|
|
||
| use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\AbstractTalk; | ||
| use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\ConcreteConferenceTalk; | ||
|
|
||
| final class SkipReturnAbstractClass | ||
| { | ||
| public function create(): AbstractTalk | ||
| { | ||
| return new ConcreteConferenceTalk(); | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...ObjectReturnTypeRector/FixtureSkipReturnAbstractOrInterface/skip_return_interface.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\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\FixtureSkipReturnAbstractOrInterface; | ||
|
|
||
| use Illuminate\Container\Container; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| final class SkipReturnInterface | ||
| { | ||
| public function create(): ContainerInterface | ||
| { | ||
| return new Container(); | ||
| } | ||
| } | ||
|
|
||
| ?> |
12 changes: 12 additions & 0 deletions
12
...rowObjectReturnTypeRector/config/disable_return_abstract_or_interface_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,12 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->ruleWithConfiguration(NarrowObjectReturnTypeRector::class, [ | ||
| NarrowObjectReturnTypeRector::IS_ALLOW_ABSTRACT_AND_INTERFACE => false, | ||
| ]); | ||
| }; | ||
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
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
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.
Looks good 👍
I just find this option name confusing. Could you name it
INCLUDE_STANDALONE_CLASS?Uh oh!
There was an error while loading. Please reload this page.
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.
That means the value need to be true, default false, but "include" term means "also", which a bit contradictive imo.
For better word, I think it can be
ALLOW_CONTRACT_RETURNIt is about adding return type which return type is contract.
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.
Include standalone class should be
falseby default.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.
It is nothing todo with standalone class or not, it is about returning abstract or interface as return type, whether class extends another class or not, if the method return interface, this config will skip.
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. I've checked the original report again.
In that case we don't need any configuration. That case should be skipped, as it would create breaking code.
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.
we had discussion about this on original PR with @Orest-Divintari and @ruudk , the use of replace interface/abstract class to concrete class is valid,
But as @calebdw and @WyriHaximus pointed, there are use case which keep interface/abstract as return type make sense, usually on unit test purpose which still use mock object everywhere.
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.
Yes, but how do you make a boundary which interface should be replaced and which not? This rule replace e.g. all
getRule()in phsptan tests with exact return type of the rule. It doesn't add any value, as parent return type is generic on purpose. It's part of contract.Once we start adding such configuration, this rule turns into manual labour I want to avoid. This rule should only replace one object type with more specific one. Not all interface types with exact returns, which sparks last 2 reports.
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'll look into this tomorrow. Closing to avoid further configuration on this rule.
Uh oh!
There was an error while loading. Please reload this page.
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.
Common interface type to be skipped:
Psr\\prefixInterop\\prefixCommon interface type should be allowed to be replaced:
This is what I proposed in old PR 3 weeks ago
and closed.