#61860 closed enhancement (fixed)
PHPCS: Empty FOREACH statement detected
Reported by: | dhruvang21 | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | 4.2.3 |
Component: | Formatting | Keywords: | has-patch |
Focuses: | docs | Cc: |
Description
Empty foreach statement is present in the wp_replace_in_html_tags function on line 757 in the file formatting.php of wp-includes folder
Attachments (1)
Change History (17)
#1
@
4 months ago
- Focuses docs added; coding-standards removed
- Keywords changes-requested added; has-patch removed
- Type changed from defect (bug) to enhancement
- Version set to 4.2.3
#2
@
4 months ago
@TobiasBg Better yet, use the correct syntax in that case...
foreach ( $replace_pairs as $needle => $replace );
#3
@
4 months ago
Thanks, @jrf!
Indeed, it appears that this syntax was originally used, but changed (semi-automatically) in https://core.trac.wordpress.org/changeset/42343/trunk/src/wp-includes/formatting.php some years ago.
#4
@
4 months ago
Can't we do this, if phpcs is throwing error.
<?php $needle = current( array_keys( $replace_pairs ) ); $replace = current( array_values( $replace_pairs ) );
#5
@
4 months ago
From a functionality point of view, this would probably work, but given that the foreach
loop is an a code section that aims to optimize the function run, that speed gain would likely be impacted.
Most likely, phpcs won't actually "complain" about jrf's suggestion (and if so, a // phpcs:ignore
comment would be appropriate, I think) but only about the current syntax with the empty {}
part.
#6
follow-up:
↓ 8
@
4 months ago
Most likely, phpcs won't actually "complain" about jrf's suggestion (and if so, a phpcs:ignore comment would be appropriate, I think) but only about the current syntax with the empty {} part.
I vaguely remember fixing the sniff which would have (unnecessarily) added the braces all those years ago since, so I don't expect PHPCS to complain, but can check and confirm later.
#7
@
4 months ago
@jrf @TobiasBg Since this code only runs when $replace_pairs
has a single element, and we have a polyfill for array_key_first() that also uses a foreach loop, how about this instead?
$needle = array_key_first( $replace_pairs ); $replace = reset( $replace_pairs );
Might clarify any future confusion.
#8
in reply to:
↑ 6
@
4 months ago
Replying to jrf:
Most likely, phpcs won't actually "complain" about jrf's suggestion (and if so, a phpcs:ignore comment would be appropriate, I think) but only about the current syntax with the empty {} part.
I vaguely remember fixing the sniff which would have (unnecessarily) added the braces all those years ago since, so I don't expect PHPCS to complain, but can check and confirm later.
Looks like this was fixed for cases where it was considered useful, but not for `foreach`, though there is a PR lingering which @rodrigosprimo was working on splitting up, which would address this.
However, as @SergeyBiryukov pointed out:
Since this code only runs when $replace_pairs has a single element...
I'd like to suggest a variant of @SergeyBiryukov patch which would avoid the internal array pointer reset:
$needle = array_key_first( $replace_pairs );
$replace = $replace_pairs[$needle];
This ticket was mentioned in PR #7183 on WordPress/wordpress-develop by @mi5t4n.
4 months ago
#9
- Keywords has-patch added
Trac ticket:
#10
@
4 months ago
I like @jrf's suggestion as it's concise and easily readable. The previously/currently used foreach
approach might be efficient, but its intention is overlooked easily. To improve readability, changing this makes sense. ~@dhruvang21 or @mi5t4n, would you maybe like to prepare this as an updated patch?~ Nevermind, I missed that @mi5t4n has already updated the PR. Thanks a lot!
#11
@
4 months ago
@TobiasBg i was just testing the above mentioned patch and it is working fine and saw that the PR is already added.
#13
@
4 months ago
- Keywords changes-requested removed
- Milestone changed from Awaiting Review to 6.7
- Owner set to SergeyBiryukov
- Status changed from new to accepted
@SergeyBiryukov commented on PR #7183:
4 months ago
#16
Thanks for the PR! Merged in r58889.
Hi @dhruvang21!
Thanks for the ticket! However, while the
foreach
body is empty, these lines can not be removed as they do have a purpose:They make the
$needle
and$replace
variables available for the lines following this loop.So, instead of removing the lines, maybe a clarifying comment would be more reasonable.