-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Remove foreach.nonIterable baseline and fix its issue #13079
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
$this->capis astdClassas the class property docblock claims, then will the(array)cast not create a clone of the data, iterate over the clone, delete properties from the clone, and then leave the original$this->capuntouched?if so, wouldn’t that create a logical defect and nullify this code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your comment would be valid i believe if we were actually unsetting properties of the actual $cap property. We don't though. So the only downside i see with this code is that its actually using a bit of extra memory because of the cloning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha. thanks for pointing that out.
because I want to be careful here and make sure we are careful about changing code just because a linter flags a warning, I am checking this again.
in explicitly-casing to
(array)we will still accept runtime corruption for things not castable into an array, but the PHPStan warning is silenced.additionally, it seems like the change from iterating over an object’s properties to the array cast changes what’s iterated. whereas the existing
foreachonly iterates overpublicproperties, the array cast is more equivalent toget_mangled_object_vars()and returns private and protected properties.the cast to
(array)silences the warning, but doesn’t really address the issue PHPStan raised, which is that we’re attempting to iterate over something that may not be iterable.we could always throw in a check for
is_iterable()around theforeach, which would address the reported warning, but I am also curious why PHPStan isn’t inferring that already?$this->capis typed asstdClass(which is iterable)get_post_type_capabilities()which is typed to return anobjectperhaps the problem is in PHPStan, or I’m still missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting to an array also doesn't address errors at higher PHPStan error levels. There remains an error on the next line at level >5:
This is getting at the underlying problem, that
$capis not anarray-keyat all, but rather astdClasswhich can never be an array key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here was introduced in r37890 (bded47a).
The code used to live on
unregister_post_type()here it looked like this:wordpress-develop/src/wp-includes/post.php
Lines 1258 to 1261 in 78eb45a
The
capwas populated byget_post_type_capabilities():wordpress-develop/src/wp-includes/post.php
Lines 1114 to 1115 in 78eb45a
Even then this was an
object:wordpress-develop/src/wp-includes/post.php
Lines 1347 to 1349 in 78eb45a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm working up an alternative. This is a long-standing bug that PHPStan has caught which looks like it warrants a separate Trac ticket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Core-66008