[wp-trac] [WordPress Trac] #2784: Make all foreach() loops cast to array
WordPress Trac
noreply at wordpress.org
Sat Feb 10 12:39:13 UTC 2024
#2784: Make all foreach() loops cast to array
-------------------------------------------------+-------------------------
Reporter: markjaquith | Owner:
| jacobsantos
Type: defect (bug) | Status: closed
Priority: normal | Milestone: 2.7
Component: Optimization | Version: 2.1
Severity: normal | Resolution: fixed
Keywords: has-patch needs-testing dev- | Focuses:
feedback |
-------------------------------------------------+-------------------------
Comment (by jacobsantos):
Replying to [comment:27 sandrakwf04cw]:
> Could you explain how casting the input of foreach() loops to an array
helps mitigate potential errors?
Yes. Using a type that is not an array or array-like throws a warning. By
casting you essentially noop when it isn't an array or array-like. It is
generally better to get that warning and fix it. Given WordPress's dynamic
nature it is near impossible to ensure that the data provided has been
vented or could be vented without checking for array or array like
everywhere. Or you could attempt to cast to an array which results in an
empty array for non array like values. It will also convert an object to
an array, albeit not always correctly.
The solution provided is 16 years old and there are better ways or at
least more modern ways of determining whether something is the correct
shape. The solution should be fine as it still works to remove warnings.
I am no longer part of WordPress community so I will leave it to you
whether you wish to research a solution and advocate for it.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/2784#comment:28>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list