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

SimpleXMLElement + null safe operator results in errors #6020

Closed
PHLAK opened this issue Nov 16, 2021 · 4 comments
Closed

SimpleXMLElement + null safe operator results in errors #6020

PHLAK opened this issue Nov 16, 2021 · 4 comments
Labels
Milestone

Comments

@PHLAK
Copy link

PHLAK commented Nov 16, 2021

Bug report

When using the null safe operator with SimpleXMLElement phpstan reports an error. More specifically, phpstan doesn't seem to understand that the properties of a SimpleXMLElement object can possibly return null.

Using nullsafe property access on non-nullable type SimpleXMLElement. Use -> instead.

Code snippet that reproduces the problem

https://phpstan.org/r/68f49c78-6d49-4e4c-96f0-83562eff662e

Here's some additional code illustrating the way properties on the SimpleXMLElement object work.

>>> $foo = new SimpleXMLElement('<foo>asdf</foo>')
=> SimpleXMLElement {#5830
     +"0": "asdf",
   }
>>> $foo
=> SimpleXMLElement {#5830
     +"0": "asdf",
   }
>>> $foo->bar
=> SimpleXMLElement {#5839}
>>> $foo->bar->baz
=> null
>>> $foo->bar->baz->qux
<warning>PHP Warning:  Attempt to read property "qux" on null in eval()'d code on line 1</warning>
=> null

This is a bit odd because $foo->bar (a non-existent element in the XML) returns an empty SimpleXMLElement. Perhaps I'm missunderstanding the way the SimpleXMLElement object works.

Expected output

No errors

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jan 31, 2022
@phpstan-bot
Copy link
Contributor

@PHLAK After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-5: Expression "$xml->foo?->bar?->baz" on a separate line does not do anything.
-5: Using nullsafe property access on non-nullable type SimpleXMLElement. Use -> instead.
-5: Using nullsafe property access on non-nullable type SimpleXMLElement. Use -> instead.
+5: Expression "$xml->foo?->bar?->baz" on a separate line does not do anything.
Full report
Line Error
5 Expression "$xml->foo?->bar?->baz" on a separate line does not do anything.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#76

@PHLAK
Copy link
Author

PHLAK commented Feb 9, 2022

Awesome, thanks @ondrejmirtes!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants