[Php80] Skip promotion when property type is wider than constructor param on ClassPropertyAssignToConstructorPromotionRector#8170
Open
ximki-vinki wants to merge 2 commits into
Conversation
…aram on ClassPropertyAssignToConstructorPromotionRector (#9809)
Member
|
This is not the right solution, as default |
…opertyAssignToConstructorPromotionRector(#9809)
Author
Change solution |
|
|
||
| final class WiderPropertyTypeWithoutNullAssign | ||
| { | ||
| public function __construct(private(set) ?object $entity) |
Member
There was a problem hiding this comment.
Suggested change
| public function __construct(private(set) ?object $entity) | |
| public function __construct(private(set) object $entity) |
There is no null override and constructor always set an object, so ? should be removed.
|
|
||
| private function shouldSkipPropertyAssignedNull(Class_ $class, string $propertyName): bool | ||
| { | ||
| return (bool) $this->betterNodeFinder->findFirst( |
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.
Fixes rectorphp/rector#9809
Summary
ClassPropertyAssignToConstructorPromotionRectorno longer promotes when the property type is strictly wider than the constructor parameter type (e.g.?objectproperty vsobjectparam). Promotion would copy the wider property type onto the constructor and loosen the constructor contract.Narrowing cases (e.g.
objectproperty vs?objectparam) still promote as before.Repro from the issue: https://getrector.com/demo/980360e4-321c-4a02-ac29-7062848188cf
Changes
shouldSkipWiderPropertyType()withshouldUsePropertyTypeForPromotedParam+isSubtypeguardskip_wider_property_type.php.inc— no change expected (issue repro)narrower_property_type.php.inc— promotion with narrowing still worksTest plan
vendor/bin/phpunit rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/ClassPropertyAssignToConstructorPromotionRectorTest.phpcomposer complete-check