-
Notifications
You must be signed in to change notification settings - Fork 384
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
Fix PHPStan errors #7477
Fix PHPStan errors #7477
Conversation
- This will stop PHPStan to consider docblock type defination as certain
Plugin builds for 140aeb7 are ready 🛎️!
|
@westonruter It seems there are a few bugs in the latest PHPStan version so I have started a discussion which later converted into an issue phpstan/phpstan#8980. |
…ansientCaching::process()`
…::extract_by_filename_or_filesystem()`
…t_instance()` - On AMP Support page it will create a new instance of `WP_Site_Health` anyway. So better to remove dependency on `get_instance()` method.
…rl` in `AMP_Validated_URL_Post_Type::print_admin_notice()`
…AMP_Validation_Error_Taxonomy::handle_validation_error_update()`
$site_health = method_exists( 'WP_Site_Health', 'get_instance' ) ? WP_Site_Health::get_instance() : new WP_Site_Health(); | ||
$loopback_status = $site_health->can_perform_loopback(); | ||
$loopback_status = ( new WP_Site_Health() )->can_perform_loopback(); |
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.
Even though this is wrongly flagged by PHPStan as an error but I think it's in better shape now. On the support page, there will be no instance of WP_Site_Health
and it will be freshly instantiated anyway.
So IMO it's better to instantiate and read the required data since we will not be using this class instance anywhere else.
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.
Yes, this makes sense. And WP_Site_Health::get_instance()
is the singleton static getter, so it'll instantiate it if it doesn't already exist, and only one copy will ever only ever get constructed.
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.
Nevermind, my comment is saying the opposite.
The WP_Site_Health::get_instance()
method was introduced in WordPress 5.4 via WordPress/wordpress-develop@637b6f5. So that's why we have the method_exists
check.
The only potential problem here is that the constructor for the class has side effects. But as you say, this class wouldn't have been instantiated on the support page anyway.
@@ -595,7 +595,7 @@ static function ( $validation_error ) { | |||
); | |||
|
|||
// Remove element if it has illegal CDATA. | |||
if ( ! empty( $cdata ) && $node instanceof DOMElement ) { | |||
if ( ! empty( $cdata ) ) { |
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.
Makes sense since we already know it is a DOMElement
.
@@ -828,7 +828,7 @@ private function get_required_script_components( DOMElement $node, $tag_spec, $a | |||
} | |||
|
|||
// Check if element needs amp-bind component. | |||
if ( $node instanceof DOMElement && ! in_array( 'amp-bind', $this->script_components, true ) ) { | |||
if ( ! in_array( 'amp-bind', $this->script_components, true ) ) { |
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.
Good
@@ -2334,7 +2334,7 @@ private function has_parent( DOMElement $node, $parent_spec_name ) { | |||
|
|||
// Ensure attributes match; if not move up to the next node. | |||
foreach ( $parsed_spec_name['attributes'] as $attr_name => $attr_value ) { | |||
if ( $node instanceof DOMElement && strtolower( $node->getAttribute( $attr_name ) ) !== $attr_value ) { | |||
if ( strtolower( $node->getAttribute( $attr_name ) ) !== $attr_value ) { |
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.
Good
if ( 'delete' === $action && self::delete_empty_term( $term_id ) ) { | ||
if ( self::delete_empty_term( $term_id ) ) { |
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.
Good since the function bails above already if 'delete' !== $action
if ( 'edit.php' === $pagenow && 'delete' === $action && 1 === $updated_count ) { | ||
if ( 'edit.php' === $pagenow && 1 === $updated_count ) { |
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.
Good since the function bails above already if 'delete' !== $action
$site_health = method_exists( 'WP_Site_Health', 'get_instance' ) ? WP_Site_Health::get_instance() : new WP_Site_Health(); | ||
$loopback_status = $site_health->can_perform_loopback(); | ||
$loopback_status = ( new WP_Site_Health() )->can_perform_loopback(); |
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.
Yes, this makes sense. And WP_Site_Health::get_instance()
is the singleton static getter, so it'll instantiate it if it doesn't already exist, and only one copy will ever only ever get constructed.
@@ -551,7 +551,6 @@ function amp_is_available() { | |||
} | |||
} elseif ( ! ( | |||
$queried_object instanceof WP_Post && | |||
$wp_query instanceof WP_Query && |
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.
Redundant indeed since above:
if ( ! $wp_query instanceof WP_Query || ! did_action( 'wp' ) ) {
return;
@@ -230,7 +230,7 @@ public static function extract_by_filename_or_filesystem( $dimensions ) { | |||
$image_size = ( ! empty( $image_size ) && is_array( $image_size ) ) ? $image_size : []; | |||
} | |||
|
|||
if ( ( empty( $image_size ) || ! is_array( $image_size ) ) && file_exists( $image_file ) ) { | |||
if ( ( empty( $image_size ) ) && file_exists( $image_file ) ) { |
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.
Redundant indeed since we check if it is an array above. Nevertheless, this can be further cleaned up a tiny bit more to remove the extra parentheses:
if ( ( empty( $image_size ) ) && file_exists( $image_file ) ) { | |
if ( empty( $image_size ) && file_exists( $image_file ) ) { |
if ( $post instanceof WP_Post && 'post' === get_current_screen()->base && self::POST_TYPE_SLUG === get_current_screen()->post_type ) { | ||
if ( $post instanceof WP_Post && 'post' === get_current_screen()->base ) { |
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.
Indeed redundant since above:
if ( ! get_current_screen() || self::POST_TYPE_SLUG !== get_current_screen()->post_type ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
if ( ! empty( $error_source_slugs ) && is_array( $error_source_slugs ) ) { | ||
if ( ! empty( $error_source_slugs ) ) { |
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 PHP<8.0, array_values()
can return null
if it is not passed an array. Nevertheless, we have ensured above that the $error_sources
input is an array.
Co-authored-by: Weston Ruter <[email protected]>
Yes. Since we are very specific about these cases we require them as it is, what about ignoring PHPStan on these lines? |
…n phpstan baseline instead
…tan-ignore-next-line` comments
@westonruter I have made a few changes to how we ignore the PHPStan errors, Previously we were using I have added a new |
The inclusion of Lines 33 to 35 in 963082a
Should that be moved to |
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Alternatively, I found that if I run:
It will generate a Is there a specific reason to use a PHP file for this? |
Yes, but I am not sure if we should use the main config file for this as changes can be there as PHPStan introduces new rules. I choose a PHP file for better readability and comments due to better syntax highlighting. |
QA Passed ✅ Up until 4337065, the base branch had no PHPStan issues. |
Summary
treatPhpDocTypesAsCertain: false
to PHPStan config so that PHPStan doesn't take doc types as certain.Checklist