#50767 closed task (blessed) (fixed)
Coding Standards fixes for WP 5.6
Reported by: | SergeyBiryukov | Owned by: | |
---|---|---|---|
Milestone: | 5.6 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch has-unit-tests |
Focuses: | coding-standards | Cc: |
Attachments (4)
Change History (26)
@
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.
@
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
@
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".
@
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()
withimplode()
sizeof()
withcount()
is_writeable()
withis_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:
↓ 15
@
4 years ago
The linked PR combines the four patches I just uploaded so we can see a passing build.
#15
in reply to:
↑ 11
@
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
@
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 )
orif ( 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.
In 48764: