phpstan / phpstan-strict-rules

Extra strict and opinionated rules for PHPStan

Repository from Github https://github.comphpstan/phpstan-strict-rulesRepository from Github https://github.comphpstan/phpstan-strict-rules

Variable property access on SimpleXMLElement

ghispi opened this issue · comments

commented

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

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'};

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

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

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.