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

PHP 7.3: constants are allowed on the left-hand-side of the instanceof operator #1652

Open
1 task
jrfnl opened this issue Sep 21, 2023 · 3 comments
Open
1 task

Comments

@jrfnl
Copy link
Member

jrfnl commented Sep 21, 2023

Is your feature request related to a problem?

Instanceof Operator accepts Literals

instanceof now allows literals as the first operand, in which case the result is always false.

(Note: that last part is not necessarily true anymore now object instances can be assigned to constants, though more testing to see what is and isn't allowed and since when would be needed to be sure)

Refs:

Describe the solution you'd like

A new sniff to detect code like the below:

$var = FALSE instanceof stdClass; // Allowed since PHP 7.3.
  • I intend to create a pull request to implement this feature.
@Lewiscowles1986
Copy link

https://3v4l.org/OUOD5 seems to be as minimally reproducible an example as I could get of the behaviour.

Interestingly (to me) https://3v4l.org/CvTrH (not doing anything with the variable) would seem to be the thing that could cause most confusion for coders (note the 5.x versions not erroring without an access)

@jrfnl
Copy link
Member Author

jrfnl commented Oct 4, 2023

More fun with this:

(note the 5.x versions not erroring without an access)

Same goes for with an access.
This, to me, is actually not surprising as prior to PHP 5.2, there were some issues with the type system causing keywords (like false, self etc) to be interpreted as class names. I suspect it may have something to do with that.

@Lewiscowles1986
Copy link

Lewiscowles1986 commented Oct 4, 2023

Hmm, I riffed on your example as empty output in that UI bothers me. I'm surprised to see it "working" (realistically just not erroring) in some 5.x versions. Thanks for the explain on why that is.

https://3v4l.org/rpl5o#veol

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

No branches or pull requests

2 participants