Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable property access on SimpleXMLElement #243

Open
ghispi opened this issue Apr 22, 2024 · 3 comments
Open

Variable property access on SimpleXMLElement #243

ghispi opened this issue Apr 22, 2024 · 3 comments

Comments

@ghispi
Copy link

ghispi commented Apr 22, 2024

Bug report

I'm not sure if this is bug or intended behavior but since SimpleXMLElement has special treatment as an Universal object crates, this should be handled as well, as there is no other way to access nodes having hyphen or other prohibited character.

As per https://www.php.net/manual/en/simplexml.examples-basic.php, example 3

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Code snippet that reproduces the problem

https://phpstan.org/r/93b6f5e2-cddb-4172-b42b-5017d0911fc4

Expected output

No error

Did PHPStan help you today? Did it make you happy in any way?

No response

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan Apr 22, 2024
@VincentLanglet
Copy link
Contributor

Hi @ondrejmirtes,

I'm interested in solving this issue.
Before implementing the solution, I'd like to know what do you prefer

Option 1:
All VariableRules should ignore constant string so the following code should be valid

$date = new \DateTime();
$date->{'format'}('Y');

Option 2:
The only non-reported constant string should be for SimpleXMLElement

$xml = new \ SimpleXMLElement();
$xml->{'foo-bar'};

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Dec 28, 2024

After some investigations, I think it will be option 2 because the rule previously handled the SimpleXMLElement case.

It's confirmed by the test which passed SimpleXMLElement

It's confirmed by the doc https://phpstan.org/config-reference#universal-object-crates which said SImpleXMLElement is included by default

Then, it was removed in this PHPStan commit phpstan/phpstan-src@56970a6
and now it has its own ExtendedPropertyReflection in https://github.com/phpstan/phpstan-src/blob/70572a17d592b6a7c148d9b2188408127378a4c2/src/Reflection/Php/SimpleXMLElementProperty.php#L17

That's why SimpleXMLElement is not handled anymore

@VincentLanglet
Copy link
Contributor

I opened a PR to provide a behavior similar between SimpleXMLElement and Stdclass
#255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants