Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#50767 closed task (blessed) (fixed)

Coding Standards fixes for WP 5.6

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by:
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch has-unit-tests
Focuses: coding-standards Cc:

Description

Previously:

Attachments (4)

50767-use-self-when-appropriate.patch (1.5 KB) - added by jrf 4 years ago.
CS: use self when appropriate * get_default_primary_column_name() is a protected method, so calling it statically with the class name is bad practice. * Similarly this applies when calling a private constructor.
50767-dont-use-alias-functions.patch (56.7 KB) - added by jrf 4 years ago.
QA: don't use alias functions Using the canonical function name for PHP functions is strongly recommended as aliases may be deprecated or removed without (much) warning. This replaces all uses of the following: * join() with implode() * sizeof() with count() * is_writeable() with is_writable() * doubleval() with a (float) cast In part, this is a follow-up to #47746
50767-remove-assumption-in-code.patch (1.6 KB) - added by jrf 4 years ago.
QA: remove assumption in code Use a more stable condition. If $wpdb->get_row() is successful and the $output parameter has not been set, the output will be an instance of stdClass, so test to confirm that instead of testing against "not null".
50767-use-instanceof.patch (3.1 KB) - added by jrf 4 years ago.
Modernize: use instanceof instead of a comparison with get_class() Includes adjusting external libraries which are no longer maintained externally.

Download all attachments as: .zip

Change History (26)

#1 @SergeyBiryukov
4 years ago

In 48764:

Coding Standards: Fix WPCS issues in wp-admin/includes/plugin.php.

Includes minor code layout fixes for better readability.

Props rnaby for initial patch.
See #50767, #43848.

#2 @SergeyBiryukov
4 years ago

In 48765:

Coding Standards: Use consistent formatting for translator comments in wp-includes/rest-api.php.

See #50767.

#3 @SergeyBiryukov
4 years ago

In 48766:

Coding Standards: Remove a few more extra brackets from some conditions in wp-admin/includes/plugin.php.

Follow-up to [48764].

See #50767.

#4 @SergeyBiryukov
4 years ago

In 48967:

Coding Standards: Use strict comparison in wp-admin/includes/class-wp-ms-themes-list-table.php.

See #50767.

#5 @SergeyBiryukov
4 years ago

In 48968:

Coding Standards: Use strict comparison in wp-admin/includes/class-wp-plugins-list-table.php.

See #50767.

#6 @SergeyBiryukov
4 years ago

In 48970:

Coding Standards: Use strict comparison in wp-admin/includes/class-wp-plugin-install-list-table.php.

See #50767.

#7 @SergeyBiryukov
4 years ago

In 48988:

Tests: Move the data_wp_site_query_meta_query() data provider next to the test it's used in.

See #50767.

#8 @SergeyBiryukov
4 years ago

In 49004:

Coding Standards: Give the $id variable in slashed data tests a more descriptive name.

See #50767, #51344.

#9 @SergeyBiryukov
4 years ago

In 49086:

Coding Standards: Make checks for an empty post in wp-includes/post.php more consistent.

See #50767.

@jrf
4 years ago

CS: use self when appropriate * get_default_primary_column_name() is a protected method, so calling it statically with the class name is bad practice. * Similarly this applies when calling a private constructor.

@jrf
4 years ago

QA: don't use alias functions Using the canonical function name for PHP functions is strongly recommended as aliases may be deprecated or removed without (much) warning. This replaces all uses of the following: * join() with implode() * sizeof() with count() * is_writeable() with is_writable() * doubleval() with a (float) cast In part, this is a follow-up to #47746

@jrf
4 years ago

QA: remove assumption in code Use a more stable condition. If $wpdb->get_row() is successful and the $output parameter has not been set, the output will be an instance of stdClass, so test to confirm that instead of testing against "not null".

@jrf
4 years ago

Modernize: use instanceof instead of a comparison with get_class() Includes adjusting external libraries which are no longer maintained externally.

This ticket was mentioned in PR #621 on WordPress/wordpress-develop by jrfnl.


4 years ago
#10

  • Keywords has-patch has-unit-tests added

### CS: use self when appropriate

  • get_default_primary_column_name() is a protected method, so calling it statically with the class name is bad practice.
  • Similarly this applies when calling a private constructor.

### QA: don't use alias functions

Using the canonical function name for PHP functions is strongly recommended as aliases may be deprecated or removed without (much) warning.

This replaces all uses of the following:

  • join() with implode()
  • sizeof() with count()
  • is_writeable() with is_writable()
  • doubleval() with a (float) cast

In part, this is a follow-up to 47746

### QA: remove assumption in code

Use a more stable condition.
If $wpdb->get_row() is successful and the $output parameter has not been set, the output will be an instance of stdClass, so test to confirm that instead of testing against "not null".

### Modernize: use instanceof instead of a comparison with get_class()

Includes adjusting external libraries which are no longer maintained externally.

Trac ticket: https://core.trac.wordpress.org/ticket/50767

#11 follow-up: @jrf
4 years ago

The linked PR combines the four patches I just uploaded so we can see a passing build.

#12 @SergeyBiryukov
4 years ago

In 49192:

Coding Standards: Use self when appropriate.

  • WP_List_Table::get_default_primary_column_name() is a protected method, so calling it statically with the class name is bad practice.
  • Similarly, this applies when calling a private constructor in WP_Screen::get().

Props jrf.
See #50767.

#13 @SergeyBiryukov
4 years ago

In 49193:

Coding Standards: Replace alias PHP functions with the canonical names.

Using the canonical function name for PHP functions is strongly recommended, as aliases may be deprecated or removed without (much) warning.

This replaces all uses of the following:

  • join() with implode()
  • sizeof() with count()
  • is_writeable() with is_writable()
  • doubleval() with a (float) cast

In part, this is a follow-up to #47746.

Props jrf.
See #50767.

#14 @SergeyBiryukov
4 years ago

In 49194:

Code Modernization: Use instanceof instead of a comparison with get_class().

Includes adjusting external libraries which are no longer maintained externally.

Props jrf.
See #50767.

#15 in reply to: ↑ 11 @SergeyBiryukov
4 years ago

Replying to jrf:

The linked PR combines the four patches I just uploaded so we can see a passing build.

Thanks for the patches!

Re: 50767-remove-assumption-in-code.patch, looking at other instances of $wpdb->get_row() in core, the checks are not very consistent:

  • is_object()
  • ! empty()
  • Just a truthy check, e.g. if ( $result )

Should we bring some consistency to the other instances too? Since $wpdb->get_row() returns an object by default, unless requested otherwise, I think a simple if ( $signup ) or if ( is_object( $signup ) ) check would be enough here.

#16 @jrf
4 years ago

Should we bring some consistency to the other instances too?

That would be a good idea. The current patch was very focused on some problems identified by a static analysis tool.

Since $wpdb->get_row() returns an object by default, unless requested otherwise, I think a simple if ( $signup ) or if ( is_object( $signup ) ) check would be enough here.

Both would still be an unsafe comparison, though the second one less so than the first.

The patch was about unsafe assumptions and doing an if ( $signup ) is still an unsafe assumption as get_row() can also return an array depending on the parameters passed and in that case, the comparison will still pass, but the code in the body of the condition will fail.

The safe comparison is instanceof stdClass, as that is actually testing for what the rest of the code is expecting.

That also prevents problems in the future if get_row(), for instance, would start returning a WP_Error object on failure instead of null, though it doesn't allow for get_row() to return a row object of another type, like WPDB_Row in the future.

#17 @SergeyBiryukov
4 years ago

In 49206:

Coding Standards: Use more specific checks for $wpdb->get_row() results.

If $wpdb->get_row() is successful and the $output parameter has not been set, the output will be an instance of stdClass, so test to confirm that instead of testing against "not null".

This affects:

  • wpmu_validate_user_signup()
  • wpmu_validate_blog_signup()

Props jrf.
See #50767.

#18 @SergeyBiryukov
4 years ago

In 49265:

Coding Standards: Rename a variable in wp-login.php for consistency with other instances.

See #50767.

#19 @SergeyBiryukov
4 years ago

In 49546:

Coding Standards: Use consistent formatting for error messages in WP_Image_Editor_Imagick::write_image() and ::strip_meta().

See #50767.

#20 @SergeyBiryukov
4 years ago

In 49564:

Coding Standards: Pass an empty string instead of null as the $replacement parameter to _deprecated_file().

Follow-up to [48327].

See #50767.

#21 @SergeyBiryukov
4 years ago

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

Follow-up: #51799

Note: See TracTickets for help on using tickets.