WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 3 months ago

#48965 closed enhancement (fixed)

Prefer strict PHP comparisons over loose PHP comparisons

Reported by: pikamander2 Owned by: SergeyBiryukov
Milestone: 5.4 Priority: normal
Severity: minor Version:
Component: General Keywords:
Focuses: coding-standards Cc:

Description

My understanding is that it's usually better to use strict comparisons (===, !==) rather than loose comparisons (==, !=) because they're more explicit and therefore less prone to silly edge case bugs.

Although it wouldn't affect the logic of the code in some cases, it could still be helpful for knowing that no implicit type conversion is happening and therefore making certain things faster to debug. Plus, the process of converting loose comparisons might reveal some dormant bugs in the code that would have been difficult to find otherwise.

So, here are some examples of what I'm proposing:

Change this:

if ( $url && $url != network_home_url( , 'http' ) ) :

to this:

if ( $url && $url !== network_home_url( , 'http' ) ) :

And this:

if ( $signup->domain . $signup->path == ) {

To this:

if ( $signup->domain . $signup->path === ) {

And this:

if ( SITECOOKIEPATH != COOKIEPATH ) {

to this:

if ( SITECOOKIEPATH !== COOKIEPATH ) {

I'd be willing to do a lot of the conversions myself, but first I wanted to see if that's something that we're interested in.

Attachments (7)

wp-activate.php.patch (902 bytes) - added by pikamander2 4 months ago.
wp-comments-post.php.patch (472 bytes) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions
wp-cron.php.patch (839 bytes) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions
wp-links-opml.php.patch (588 bytes) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions
wp-login.php.patch (2.2 KB) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions
wp-mail.php.patch (1.1 KB) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions
wp-signup.php.patch (706 bytes) - added by pikamander2 4 months ago.
Use strict comparisons and yoda conditions

Download all attachments as: .zip

Change History (14)

#1 @pikamander2
4 months ago

Here are some past cases where converting loose comparisons (and using yoda conditions) seems to have been accepted:

Just wanted to check if that's still desired and what all I should watch out for while doing so.

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

#2 @SergeyBiryukov
4 months ago

  • Focuses coding-standards added

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

@pikamander2
4 months ago

Use strict comparisons and yoda conditions

#3 @pikamander2
4 months ago

@SergeyBiryukov - Here are some strict comparison and yoda condition updates to the WordPress PHP files in the root directory.

#4 @pikamander2
3 months ago

@SergeyBiryukov @garrett-eclipse - Any updates on this? I can go through more files if these changes look good.

#5 @SergeyBiryukov
3 months ago

  • Milestone changed from Awaiting Review to 5.4
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#6 @SergeyBiryukov
3 months ago

Just noting that wp-comments-post.php is already handled in [47028] / #49105.

#7 @SergeyBiryukov
3 months ago

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

In 47054:

Coding Standards: Use strict comparison and Yoda conditions in the root directory files.

Props pikamander2.
Fixes #48965.

Note: See TracTickets for help on using tickets.