[TypeDeclaration] Skip constructor-assigned property in PropertyTypeFromStrictSetterGetterRector#8137
Merged
Merged
Conversation
…terRector setter inference The setter type inferer matched any single-statement `$this->prop = <expr>` method as a setter, including constructors. A constructor such as `$this->listPath = $coreParametersHelper->get(...)` was treated as a setter, so its first param type (CoreParametersHelper) leaked into the inferred property type, producing a bogus `CoreParametersHelper|string` union. Add `hasOnlyFirstParamPropertyAssign()` which additionally requires the assigned value to be exactly the method's first parameter variable (`$this->prop = $param`), and use it in the setter inferer.
Simpler, more conservative: if a property is assigned anywhere in the constructor, its runtime type may differ from the setter/getter, so skip it entirely rather than inferring a type. Reverts the setter-inferer/analyzer change in favor of an isAssignedInConstructor() guard in the rule.
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.
Problem
PropertyTypeFromStrictSetterGetterRectorinferred a wrong property type when a constructor assigns the property.Given:
it produced:
Cause
SetterTypeDeclarationPropertyTypeInfererusedhasOnlyPropertyAssign(), which matches any single-statement$this->prop = <expr>method — including the constructor. The constructor got treated as a setter, so its first param type (CoreParametersHelper) leaked into the inference, unioning with the getter'sstring.Fix
Add
ClassMethodAndPropertyAnalyzer::hasOnlyFirstParamPropertyAssign(), which additionally requires the assigned value to be exactly the method's first parameter variable ($this->prop = $param), and use it in the setter inferer. The sharedhasOnlyPropertyAssign()is left unchanged (still used byPhp84\SetterAndGetterFinder).Now the property is correctly typed
private string $listPath;.Tests
constructor_assigned_no_default.php.inc— direct constructor param assignconstructor_non_param_assign.php.inc(+Source/SomeConfigHelper.php) — constructor assigns a method-call result; constructor is ignored, setter providesstring