Refactoring order creates incompatible return types#1869
Merged
TomasVotruba merged 1 commit intorectorphp:masterfrom Sep 15, 2019
scheb:return-type-refactoring-order
Merged
Refactoring order creates incompatible return types#1869TomasVotruba merged 1 commit intorectorphp:masterfrom scheb:return-type-refactoring-order
TomasVotruba merged 1 commit intorectorphp:masterfrom
scheb:return-type-refactoring-order
Conversation
Member
|
There's been major refactoring of types from string to PHPStan value object types. |
Contributor
Author
|
Added a new test case, which is technically the same as the |
Member
|
Thanks 👍 I'll look at it locally |
TomasVotruba
added a commit
that referenced
this pull request
Feb 25, 2022
rectorphp/rector-src@4dee58b [Php81] Fix property with attribute inlined on ReadOnlyPropertyRector (#1869)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This test case shows how the order in which files are parsed & refactored can lead to incompatible method signatures on the return type. The rector does
populateChildren, but the implementation implicity relies on the superclass being processed before its subclasses to ensure return type compatibility. This in combination with the subtype logic leads to this result.(Note: Actually I have to say I don't really understand why this subtype logic is there, since return type variance is only supported from PHP 7.4 on. Earlier versions support type variance for the "iterable" type which is there as the test case, but I have the impression that's the one single case when PHP <7.4 supports return type variance).
I'd guess an approach similar to #1842 would be the solution. Doing it the other way round: instead of refactoring children after refactoring the node itself, the parents should be refactored before the node is refactored. This would ensure the return type decision for the parent class was made by the rector and we can make a correct decision for the node itself.
Bonus objective would be to support refactoring with return type variance when PHP7.4 is set as the language level.
I'll have a look at a potential fix as soon as I find time to have a deeper look.