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()`
| if ( class_exists( 'WP_Site_Health' ) ) { | ||
| $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.
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.
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.
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.
|
|
||
| // Remove element if it has illegal CDATA. | ||
| if ( ! empty( $cdata ) && $node instanceof DOMElement ) { | ||
| if ( ! empty( $cdata ) ) { |
There was a problem hiding this comment.
Makes sense since we already know it is a DOMElement.
|
|
||
| // 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 ) ) { |
| // 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 ) { |
| $updated_count = 0; | ||
| foreach ( $term_ids as $term_id ) { | ||
| if ( 'delete' === $action && self::delete_empty_term( $term_id ) ) { | ||
| if ( self::delete_empty_term( $term_id ) ) { |
There was a problem hiding this comment.
Good since the function bails above already if 'delete' !== $action
|
|
||
| $term_ids_count = count( $term_ids ); | ||
| if ( 'edit.php' === $pagenow && 'delete' === $action && 1 === $updated_count ) { | ||
| if ( 'edit.php' === $pagenow && 1 === $updated_count ) { |
There was a problem hiding this comment.
Good since the function bails above already if 'delete' !== $action
| if ( class_exists( 'WP_Site_Health' ) ) { | ||
| $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.
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.
| } | ||
| } elseif ( ! ( | ||
| $queried_object instanceof WP_Post && | ||
| $wp_query instanceof WP_Query && |
There was a problem hiding this comment.
Redundant indeed since above:
if ( ! $wp_query instanceof WP_Query || ! did_action( 'wp' ) ) {
return;| } | ||
|
|
||
| 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.
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 ) ) { |
| // Add notice for PHP fatal error during validation request. | ||
| $post = get_post(); | ||
| 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.
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.
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 <westonruter@google.com>
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 <westonruter@google.com>
Co-authored-by: Weston Ruter <westonruter@google.com>
|
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: falseto PHPStan config so that PHPStan doesn't take doc types as certain.Checklist