Make WordPress Core

Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#61860 closed enhancement (fixed)

PHPCS: Empty FOREACH statement detected

Reported by: dhruvang21's profile dhruvang21 Owned by: sergeybiryukov's profile 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)

error-resolve.patch (564 bytes) - added by dhruvang21 4 months ago.

Download all attachments as: .zip

Change History (17)

#1 @TobiasBg
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

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.

#2 @jrf
4 months ago

@TobiasBg Better yet, use the correct syntax in that case...
foreach ( $replace_pairs as $needle => $replace );

#3 @TobiasBg
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 @mi5t4n
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 @TobiasBg
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: @jrf
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 @SergeyBiryukov
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 @jrf
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 @TobiasBg
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!

Last edited 4 months ago by TobiasBg (previous) (diff)

#11 @dhruvang21
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.

#12 @mi5t4n
4 months ago

@TobiasBg Glad to help.

#13 @SergeyBiryukov
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

#14 @SergeyBiryukov
4 months ago

#58357 was marked as a duplicate.

#15 @SergeyBiryukov
4 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 58889:

Coding Standards: Replace an empty foreach loop in wp_replace_in_html_tags().

This aims to clarify the intention of the code and improve readability.

Follow-up to [33359].

Props jrf, TobiasBg, mi5t4n, dhruvang21, mayura8991, nadimcse, Presskopp, SergeyBiryukov.
Fixes #61860.

@SergeyBiryukov commented on PR #7183:


4 months ago
#16

Thanks for the PR! Merged in r58889.

Note: See TracTickets for help on using tickets.