-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PHPStan strict rules (except for empty.notAllowed) #1241
Conversation
031d197
to
220bdfa
Compare
1f74e35
to
0dc2c63
Compare
…ndNotBoolean for tests
00f04f7
to
db82f1e
Compare
The one last PHPStan error remaining is |
@@ -57,3 +59,6 @@ parameters: | |||
- | |||
identifier: cast.string | |||
path: */tests/* | |||
- | |||
identifier: staticMethod.dynamicCall |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this issue is being reported for assertion method calls in tests.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. I've added some suggestions, but none blocking so I'm pre-approving. Feel free to apply or ignore.
plugins/embed-optimizer/hooks.php
Outdated
@@ -63,7 +63,7 @@ function embed_optimizer_filter_oembed_html( string $html ): string { | |||
if ( 1 === $script_count && ! $has_inline_script && $html_processor->has_bookmark( 'script' ) ) { | |||
add_action( 'wp_footer', 'embed_optimizer_lazy_load_scripts' ); | |||
if ( $html_processor->seek( 'script' ) ) { | |||
if ( $html_processor->get_attribute( 'type' ) ) { | |||
if ( is_string( $html_processor->get_attribute( 'type' ) ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
if ( is_string( $html_processor->get_attribute( 'type' ) ) ) { | |
if ( null !== $html_processor->get_attribute( 'type' ) ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I think the code should stay as-is. Since get_attribute
can return true
in the case of a boolean attribute, we wouldn't want to try passing a true
to set_attrbute()
. I believe PHPStan would complain about that anyway.
@@ -465,7 +465,7 @@ function perflab_get_plugin_settings_url( string $plugin_slug ): ?string { | |||
return null; | |||
} | |||
$href = $p->get_attribute( 'href' ); | |||
if ( $href && is_string( $href ) ) { | |||
if ( is_string( $href ) && '' !== $href ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could go either way here, but for consistency...
if ( is_string( $href ) && '' !== $href ) { | |
if ( null !== $href && '' !== $href ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should stay as is because if <a href>
is encountered, then $href
here would be true
which would be a type error since the function returns a string
or null
.
@@ -67,7 +67,7 @@ public function register_metric( string $metric_slug, array $args ): void { | |||
return; | |||
} | |||
|
|||
if ( did_action( 'perflab_server_timing_send_header' ) && ! doing_action( 'perflab_server_timing_send_header' ) ) { | |||
if ( 0 !== did_action( 'perflab_server_timing_send_header' ) && ! doing_action( 'perflab_server_timing_send_header' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is such a common pattern in WP that I wouldn't even think of needing to specify, but makes sense.
if ( '' === $resource_url ) { | ||
return ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just return the value early in this case.
if ( '' === $resource_url ) { | |
return ''; | |
if ( '' === $resource_url ) { | |
return $resource_url; |
Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, pending the merge conflict resolution
… add/phpstan-strict-rules * 'trunk' of https://github.com/WordPress/performance: (206 commits) fix(webp-uploads): prevent picture element when JPEG output disabled Prepare 0.4.1 release of Optimization Detective Update attribute order in Image Prioritizer test after eliminating excessive seek() calls Account for visitors calling next_token() not just seek() Update attribute order in embed-optimizer test due to not seeking Add test to ensure tag visitation does not exceed seek limit Fix attribute order in tests now that seeking is not happening Add missing bookmark name to warning message Only seek back to the current tag if a tag visitor seeked Migrate commander for v12 Upgrade commander to 12.1.0 Bump yoast/phpunit-polyfills from 1.1.1 to 2.0.1 Bump @wordpress/scripts from 26.19.0 to 28.3.0 Bump @wordpress/env from 9.6.0 to 10.3.0 Migrate husky Update Octokit to use dynamic import Bump husky from 8.0.3 to 9.1.0 Add labels for Dependabot PRs Bump web-vitals from 3.5.0 to 4.2.1 Bump phpstan/extension-installer from 1.3.1 to 1.4.1 ...
… add/phpstan-strict-rules * 'trunk' of https://github.com/WordPress/performance: Fix OD test case for WP 6.7-alpha
if ( | ||
! is_string( $class_name ) | ||
|| | ||
1 !== preg_match( '/(?:^|\s)wp-image-([1-9]\d*)(?:\s|$)/i', $class_name, $matches ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 !== preg_match( '/(?:^|\s)wp-image-([1-9]\d*)(?:\s|$)/i', $class_name, $matches ) | |
1 !== (int) preg_match( '/(?:^|\s)wp-image-([1-9]\d*)(?:\s|$)/i', $class_name, $matches ) |
Do we needs to cast int
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because it can either be an int
or false
, and we're checking to see if it is not identical to the value.
@@ -534,22 +534,26 @@ function webp_uploads_update_image_references( $content ): string { | |||
|
|||
// This content does not have any tag on it, move forward. | |||
// TODO: Eventually this should use the HTML API to parse out the image tags and then update them. | |||
if ( ! preg_match_all( '/<(img)\s[^>]+>/', $content, $img_tags, PREG_SET_ORDER ) ) { | |||
if ( 0 === (int) preg_match_all( '/<(img)\s[^>]+>/', $content, $img_tags, PREG_SET_ORDER ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of (int)
isn't needed here either...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, actually it is. Because it's preg_match_all()
so we don't know the number of image tags being matched. Otherwise, it could be 1 === ...
. So by casting the return value to (int)
, it will convert both 0
and false
to 0
. Alternatively, this could be:
if ( 0 === (int) preg_match_all( '/<(img)\s[^>]+>/', $content, $img_tags, PREG_SET_ORDER ) ) { | |
if ( false === (bool) preg_match_all( '/<(img)\s[^>]+>/', $content, $img_tags, PREG_SET_ORDER ) ) { |
But false
indicates the error state. The integer return value is the normal case, the number of matches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @westonruter, LGTM!
@westonruter Could you please merge as it not allow me. Shows that |
Part of #1219
See #775
Note: This does not fix the
empty.notAllowed
issues since they are more complicated and deserve a separate PR.Just adding
phpstan-strict-rules
results in the following 863 errors in the codebase:This report was generated via:
When ignoring
staticMethod.dynamicCall
,booleanNot.exprNotBoolean
, andif.condNotBoolean
in tests, this goes down to just 144 errors:The following remain to be addressed:
empty.notAllowed
(60)booleanNot.exprNotBoolean
(34)if.condNotBoolean
(16)booleanAnd.leftNotBoolean
(11)cast.useless
(5)booleanOr.rightNotBoolean
(4)(rule disabled)arrayFilter.strict
(4)method.childReturnType
(3)ternary.condNotBoolean
(2)booleanAnd.rightNotBoolean
(2)variable.implicitArray
(1)plus.leftNonNumeric
(1)elseif.condNotBoolean
(1)