[Php81] Skip NullToStrictStringFuncCallArgRector for magic __get() property access#7992
Merged
Merged
Conversation
…operty access resolving to ErrorType Fixes rectorphp/rector#9762
a5eadd8 to
c49acec
Compare
Member
Author
|
Looks good 🙏 |
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#9762
Problem
NullToStrictStringFuncCallArgRectorincorrectly wraps a magic property access in a(string)cast even when the value is already guarded by a truthyifcheck:Root Cause
When
__get()has no return type hint, PHPStan resolves any magic property access toErrorType(shown as*ERROR*indumpType). SinceErrorType extends MixedType, the existingshouldSkipType()check treats it as a plainMixedTypeand does not skip. TheisAnErrorType()guard also misses it because it inspects the native type (plainMixedType) and its parent-scope fallback only fires inside closures/functions — at the top levelgetParentScope()returnsnull.Fix
In
shouldSkipValue(), check upfront whether the argument is aPropertyFetchon a class that declares__get(). If so, the type is unresolvable via static analysis and the cast would be wrong.hasMethod('__get')resolves through the full inheritance chain, so it also covers classes that inherit__get()from a parent (e.g. CakePHP'sEntityTrait).Test
Added
skip_magic_get_property_in_if_truthy_check.php.incfixture reproducing the exact scenario from the issue.