Make WordPress Core

Opened 7 years ago

Closed 3 weeks ago

#46846 closed defect (bug) (fixed)

Tight comparisons and use of Yoda conditions are not consistent

Reported by: subrataemfluence's profile subrataemfluence Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: Formatting Keywords: has-patch dev-feedback
Focuses: coding-standards Cc:

Description

In wp-includes/formatting.php, as I have seen comparisons and use of Yoda conditions are not consistent through out the file. I have made some changes and uploading a proposed patch here. Let me know if this helps!

Attachments (2)

46846.diff (6.0 KB) - added by subrataemfluence 7 years ago.
46846.1.diff (2.4 KB) - added by sabernhardt 5 months ago.
more Yoda conditions

Download all attachments as: .zip

Change History (10)

#1 follow-up: @callumbw95
5 months ago

  • Resolution set to worksforme
  • Status changed from new to closed

Hi @subrataemfluence,

I have taken a quick look into this, and compared your patch to whats there today in the file, and I can see that most if not all conditionals are now written as yoda conditions. As of such I am going to close this ticket, but if you have anything further to add on this please reopen it.

#2 in reply to: ↑ 1 ; follow-up: @sabernhardt
5 months ago

  • Keywords 2nd-opinion added

most if not all conditionals are now written as yoda conditions

All the proposed comparisons are now strict, and most of them have switched to Yoda condition order.

  • r45887: strict comparison and Yoda condition in iso8601_to_datetime()
  • r47219: strict comparison and Yoda condition in iso8601_timezone_to_offset(), plus Yoda conditions for antispambot() and sanitize_option()
  • r47808: strict comparisons in convert_smilies(), wp_trim_excerpt(), esc_url(), sanitize_option(), and wp_staticize_emoji()
  • r55642: strict comparison in wp_sprintf_l()
  • r56325: strict comparison in antispambot()

That still leaves two from the first patch. However, similar comparisons exist elsewhere in the same file.

  1. if ( count( $matches ) === 0 ) on line 3432 for translate_smiley() is like
  2. if ( strpos( $email, '@', 1 ) === false ) on line 3558 for is_email() matches

In addition to those, I found

  • if ( trim( $text ) === '' ) on line 449 for wpautop()
  • if ( $force || (int) get_option( 'use_balanceTags' ) === 1 ) on line 2544 for balanceTags()
  • if ( stripos( $text, 'target' ) === false || stripos( $text, '<a ' ) === false || is_serialized( $text ) ) on line 3312 for wp_targeted_link_rel(), which r59120 deprecated

I'll upload a patch editing those 8 lines, in case it is worth reopening the ticket for any of them.

@sabernhardt
5 months ago

more Yoda conditions

#3 @peterwilsoncc
5 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Resolution worksforme deleted
  • Status changed from closed to reopened

@sabernhardt I've reopened the ticket to consider your patch and placed it on the 6.9 milestone. That will ensure the props are assigned correctly.

This ticket was mentioned in PR #9607 on WordPress/wordpress-develop by IshikaBansal95.


3 months ago
#4

Trac ticket:

This ticket was mentioned in Slack in #core by ishika_bansal. View the logs.


3 months ago

#6 @sabernhardt
3 months ago

  • Keywords 2nd-opinion removed

removing the '2nd-opinion' keyword since the ticket was reopened and we have a pull request

#7 in reply to: ↑ 2 @SergeyBiryukov
3 weeks ago

  • Owner set to SergeyBiryukov
  • Status changed from reopened to reviewing

Replying to sabernhardt:

I'll upload a patch editing those 8 lines, in case it is worth reopening the ticket for any of them.

Thanks for the patch!

Looking at the PHP coding standards, Yoda conditions are mostly relevant for variables:

When doing logical comparisons involving variables, always put the variable on the right side and put constants, literals, or function calls on the left side. If neither side is a variable, the order is not important.

So technically, I'm not sure this needs any changes. However, if the goal is to make the order more consistent throughout the file, I have no objections to that :)

#8 @SergeyBiryukov
3 weeks ago

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

In 61053:

Coding Standards: Use Yoda conditions consistently in wp-includes/formatting.php.

While Yoda conditions are mostly relevant for variables and not required if neither side is a variable, this commit aims to make the order more consistent throughout the file.

Follow-up to [1636], [4990], [6974], [10322], [10769], [11048], [42770], [47219], [56325].

Props subrataemfluence, sabernhardt, ishikaatxecurify, callumbw95, peterwilsoncc, SergeyBiryukov.
Fixes #46846.

Note: See TracTickets for help on using tickets.