php-src icon indicating copy to clipboard operation
php-src copied to clipboard

`isset` (and `??` operator) could should a notice if the inner expression is non-array

Open mvorisek opened this issue 4 years ago • 18 comments

Description

The following code:

$oneDimensionalArray = [];
var_dump($oneDimensionalArray[0][1]);
var_dump($oneDimensionalArray[0][1] ?? '');

https://3v4l.org/1LMRe

Resulted in this output:

Warning: Undefined array key 0 in /in/1LMRe on line 4

Warning: Trying to access array offset on value of type null in /in/1LMRe on line 4
NULL
string(0) ""

But I expected this output instead:

Warning: Undefined array key 0 in /in/1LMRe on line 4

Warning: Trying to access array offset on value of type null in /in/1LMRe on line 4
NULL
Warning: Undefined array key 0 in /in/1LMRe on line 4

string(0) ""

PHP Version

8 - master

Operating System

any

mvorisek avatar Apr 12 '22 11:04 mvorisek

isset should also not suppress such notice: https://3v4l.org/Nn6Hg

mvorisek avatar Apr 12 '22 12:04 mvorisek

This is not a bug. It's the default behaviour of isset() to do a safe, warning-free access check (and ?? shares isset() semantics).

I.e. isset() does not only check the existence of a key or property, but also whether it can be dereferenced at all via the specific operator.

bwoebi avatar Apr 12 '22 12:04 bwoebi

@iluuu1994 Oh, you retagged that just now. Well, feel free to reopen that as a feature if you consider it worthwhile pursuing. I think the current semantics make perfect sense.

Anyway, in any case, a change here would require a RFC.

bwoebi avatar Apr 12 '22 12:04 bwoebi

Yes, please reopen, retitled and desc updated

mvorisek avatar Apr 12 '22 12:04 mvorisek

@mvorisek your last case is explicitly allowed by isset docs:

 If multiple parameters are supplied then isset() will return true only if all of the parameters are considered set. Evaluation goes from left to right and stops as soon as an unset variable is encountered. 

bwoebi avatar Apr 12 '22 12:04 bwoebi

@bwoebi I think closing this was sensible. We actually had an internal discussions about feature requests in GitHub issues. They're usually not helpful, somebody has to step up and create an RFC + implementation and GitHub issues are rarely the motivation to do so. But what we might do is auto-close feature requests that require RFCs after a certain period of inactivity, analogous to "Needs feedback".

iluuu1994 avatar Apr 12 '22 12:04 iluuu1994

No, please no, what is the point of keeping open issue count low? When an issue is closed, there is much less chance that someone will find it. On the other side, everyone can see how old the issue is. This is how bugs.php.net works/ed, some issues are commented for many years until they are addressed.

mvorisek avatar Apr 12 '22 12:04 mvorisek

When an issue is closed, there is much less chance that someone will find it.

And when we have 4000+ issues like on bugs.php.net, chances are also pretty much 0 that somebody will find it.

iluuu1994 avatar Apr 12 '22 12:04 iluuu1994

@iluuu1994 I agree. I think it's good to have these "Status: Requires RFC" issues open for a while (2 weeks like the Needs Feedback ones) for visibility. However it clutters the issue list when searching for older bugs which need to be fixed etc..

So yes, please automatically close them after a while. It's always possible to filter for Requires RFC in the closed issues (as long as we remove the label from actually implemented RFCs).

bwoebi avatar Apr 12 '22 12:04 bwoebi

Yes. I think it's much more sensible for a feature list to grow endlessly long than it is for a bug list. That's what we're trying to avoid and I think it's in the interest of everyone.

iluuu1994 avatar Apr 12 '22 12:04 iluuu1994

I see your point, but the list of the sensible features is limited. When tracking issue is keep open, any duplicated issue can be linked to it and it can be much easier filtered.

When autoclosing bot is present, I personally spam him too to keep it open :) and at least for me, such comments are really annoying to read, and even much worse, notification for every comment is sent, which leads to blacklisting such bot on GitHub, and important messages by the bot can be missed later.

mvorisek avatar Apr 12 '22 12:04 mvorisek

@mvorisek Who determines whether a Feature is sensible? I for instance consider this Feature request unreasonable, because isset should provide a safe way to verify the accessibility of any expression, regardless of the types of any intermediary values.

bwoebi avatar Apr 12 '22 13:04 bwoebi

here is a better example with parenthesis: https://3v4l.org/MpqMB

I know, they have no effect, but they stress the idea, why the inner expression should not be affected, thus this feature request, only the outer notice by [] operator must be supressed

Who determines whether a Feature is sensible?

and small feature requests not requiring should be keep open?

I propose to keep things as they are and impl. such bot only after this is a real issue. Maybe we will find a better way later or we might be gifted by Github with a feature for it :)

mvorisek avatar Apr 12 '22 13:04 mvorisek

@mvorisek Parenthesis are abstracted away upon AST construction, they have no deeper meaning except providing explicit operator grouping instead of the default precedence.

Wrapping something in a parenthesis does not magically separate values. $a and ($a) are identical for everything the compiler is concerned with. (like ($a)[0] = 1; does a write to $a.)

bwoebi avatar Apr 12 '22 13:04 bwoebi

even $userr['name'] ?? null does not emit a warning, notice the typo in the variable name

mvorisek avatar Apr 12 '22 13:04 mvorisek

PHP could use nullsafe index access #7825 , ?? hides all kinds of potential mistakes. And closing sensible RFC required issues helps you, but not the project itself nor the community.

KapitanOczywisty avatar Apr 12 '22 21:04 KapitanOczywisty

More than a hypothetical "nullsafe array access" $array_that_may_be_null?->[$key_that_must_exist_in_the_array_if_not_null], what seems desirable here is a hypothetical "lenient array access" $array_that_must_not_be_null[?$key_that_may_not_exist_in_the_array], I think?

guilliamxavier avatar May 16 '22 15:05 guilliamxavier

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

github-actions[bot] avatar Aug 15 '22 00:08 github-actions[bot]