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.0: cannot use isset() on the result of an expression #1301

Open
jrfnl opened this issue Sep 17, 2021 · 4 comments
Open

PHP < 7.0: cannot use isset() on the result of an expression #1301

jrfnl opened this issue Sep 17, 2021 · 4 comments

Comments

@jrfnl
Copy link
Member

jrfnl commented Sep 17, 2021

Given the following code samples:

if (!isset(MY_CONSTANT[substr($data, 0, 2)])) {}

$marker = substr($data, 0, 2);
if (!isset(self::MY_CONSTANT[$marker])) {}

Linting this on PHP < 7.0 results in a Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) error.

Would be nice if we could detect this.

Patterns which do work cross-version:

$marker = substr($data, 0, 2);
if (!isset($array_var[$marker])) {
if (!isset(self::$array_var[$marker])) {
if (!isset($this->array_var[$marker])) {

I suspect this may be related to the Uniform Variable Syntax RFC, but haven't dug in to verify.

@villfa
Copy link

villfa commented Dec 1, 2021

Here the RFC you're looking for: https://wiki.php.net/rfc/empty_isset_exprs

@jrfnl
Copy link
Member Author

jrfnl commented Dec 1, 2021

@villfa Unfortunately that's not the right RFC.

The RFC you referenced applies only to empty(). It was approved and merged into PHP 5.5 and is covered by the PHPCompatibility.LanguageConstructs.NewEmptyNonVariable sniff.

The change above is about isset() and the use of constants containing an array (as supported in PHP 5.6 and higher when declared using the const keyword).

@villfa
Copy link

villfa commented Dec 1, 2021

My bad. I just notice the result of the vote excluded isset().

@jrfnl
Copy link
Member Author

jrfnl commented Nov 3, 2022

Just as a note for the record: running isset()on plain constants/class constants is fine, it's only array access on those constants which is problematic.

The ability to declare a const as an array was only added in PHP 5.6, the ability to do the same using define() was added in PHP 7.0, which is probably why the ability to use isset() on array access on constants was added in PHP 7.0.

Source: WordPress/wordpress-develop#3555 (comment)

Just ran into this one again. I still can't really find a relevant RFC, but I now think this is directly related to the addition of the ability to declare constant arrays in PHP. Doesn't look like either of those changes were handled via an RFC though, so finding the exact specs of the change will be sketchy.

Even so, I think a sniff which would detect isset() being used on array access on (class/global) constants should probably catch the most pertinent issues.

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