[wp-trac] [WordPress Trac] #61860: PHPCS: Empty FOREACH statement detected

WordPress Trac noreply at wordpress.org
Mon Aug 12 22:26:38 UTC 2024


#61860: PHPCS: Empty FOREACH statement detected
-------------------------------+------------------------------
 Reporter:  dhruvang21         |       Owner:  (none)
     Type:  enhancement        |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Formatting         |     Version:  4.2.3
 Severity:  normal             |  Resolution:
 Keywords:  changes-requested  |     Focuses:  docs
-------------------------------+------------------------------

Comment (by jrf):

 Replying to [comment:6 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
 [https://github.com/squizlabs/PHP_CodeSniffer/pull/2827 fixed for cases
 where it was considered useful, but not for `foreach`], though there is a
 [https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/482 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:

 {{{#!php
 $needle  = array_key_first( $replace_pairs );
 $replace = $replace_pairs[$needle];
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61860#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list