Fix phpstan/phpstan#7088: Accessing SimpleXMLElement nodes using braces gives mixed type#5406
Merged
VincentLanglet merged 2 commits intophpstan:2.1.xfrom Apr 7, 2026
Conversation
…turns mixed
- When accessing SimpleXMLElement properties via $prop->{$x} with a non-constant
string name, the type was incorrectly resolved as mixed instead of SimpleXMLElement|null
- Added fallback in PropertyFetchHandler::resolveType() to query the var type's
property reflection for non-constant string property names
- New regression test in tests/PHPStan/Analyser/nsrt/bug-7088.php
9e33578 to
721d265
Compare
Contributor
|
While using a hardcoded constant I think it's good enough but we might prefer ask validation on ondrej. WDYT ? |
VincentLanglet
approved these changes
Apr 6, 2026
staabm
reviewed
Apr 6, 2026
Address review feedback: the magic string '__phpstan_dynamic_property'
is now a private constant with a docblock explaining its purpose.
It serves as a representative property name for dynamic property access
resolution ($obj->{$expr}), allowing PropertiesClassReflectionExtensions
that accept any property name to return the correct type.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
approved these changes
Apr 7, 2026
phpstan-bot
added a commit
to phpstan-bot/phpstan-src
that referenced
this pull request
Apr 7, 2026
…es gives mixed type (phpstan#5406) Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Accessing
SimpleXMLElementproperties using dynamic names ($prop->{$x}) was incorrectly resolved asmixedinstead ofSimpleXMLElement|null. Static property names ($prop->foo) and constant string expressions ($prop->{'foo-bar'}) already worked correctly.Changes
src/Analyser/ExprHandler/PropertyFetchHandler.phpfor non-constant string property names: when the name is a string type but not a constant string, query the var type's property reflection to determine the typetests/PHPStan/Analyser/nsrt/bug-7088.phpRoot cause
In
PropertyFetchHandler::resolveType(), when the property name expression is not anIdentifierand not a constant string, the code immediately returnedMixedType. For classes likeSimpleXMLElementthat have aPropertiesClassReflectionExtensionaccepting any property name, we can still determine the correct type by querying the property reflection with a representative property name.Test
Added
tests/PHPStan/Analyser/nsrt/bug-7088.phpthat verifies all three property access patterns onSimpleXMLElementresolve to(SimpleXMLElement|null):$prop->foo(static name)$prop->{'foo-bar'}(constant string expression)$prop->{$x}(dynamic variable name)Fixes phpstan/phpstan#7088