Skip to content
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

Merged
merged 20 commits into from
Mar 2, 2023
Merged

Fix PHPStan errors #7477

merged 20 commits into from
Mar 2, 2023

Conversation

thelovekesh
Copy link
Collaborator

@thelovekesh thelovekesh commented Mar 1, 2023

Summary

  • Add treatPhpDocTypesAsCertain: false to PHPStan config so that PHPStan doesn't take doc types as certain.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

- This will stop PHPStan to consider docblock type defination as certain
@CLAassistant
Copy link

CLAassistant commented Mar 1, 2023

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 1, 2023

Plugin builds for 140aeb7 are ready 🛎️!

@thelovekesh
Copy link
Collaborator Author

@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.

…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();
Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Member

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.

@westonruter westonruter added this to the v2.4.1 milestone Mar 1, 2023
@@ -595,7 +595,7 @@ static function ( $validation_error ) {
);

// Remove element if it has illegal CDATA.
if ( ! empty( $cdata ) && $node instanceof DOMElement ) {
if ( ! empty( $cdata ) ) {
Copy link
Member

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 ) ) {
Copy link
Member

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 ) {
Copy link
Member

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 ) ) {
Copy link
Member

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 ) {
Copy link
Member

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();
Copy link
Member

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 &&
Copy link
Member

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 ) ) {
Copy link
Member

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:

Suggested change
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 ) {
Copy link
Member

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;
		}

Comment on lines -829 to +828
if ( ! empty( $error_source_slugs ) && is_array( $error_source_slugs ) ) {
if ( ! empty( $error_source_slugs ) ) {
Copy link
Member

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.

@westonruter
Copy link
Member

It seems there are a few bugs in the latest PHPStan version so I have started a discussion which later converted into an issue

This is related to these failures, correct?

image

Co-authored-by: Weston Ruter <westonruter@google.com>
@thelovekesh
Copy link
Collaborator Author

This is related to these failures, correct?

Yes. Since we are very specific about these cases we require them as it is, what about ignoring PHPStan on these lines?

@thelovekesh
Copy link
Collaborator Author

@westonruter I have made a few changes to how we ignore the PHPStan errors, Previously we were using @phpstan-ignore-next-line comments which are hard to keep track of, and even remove them when things get fixed upstream.

I have added a new phpstan-baseline.php file which will keep track of ignoring required errors with todo. Or we can say a single source to deal with PHPStan errors.

@westonruter
Copy link
Member

The inclusion of phpstan-baseline.php should eliminate the need for ignoreErrors in the phpstan.neon.dist, correct?

amp-wp/phpstan.neon.dist

Lines 33 to 35 in 963082a

ignoreErrors:
# Setting src to false indicates a bundle of dependencies.
- '#^Property _WP_Dependency::\$src \(string\) does not accept false\.$#'

Should that be moved to phpstan-baseline.php as well?

phpstan-baseline.php Outdated Show resolved Hide resolved
thelovekesh and others added 2 commits March 3, 2023 01:13
Co-authored-by: Weston Ruter <westonruter@google.com>
Co-authored-by: Weston Ruter <westonruter@google.com>
@westonruter
Copy link
Member

Alternatively, I found that if I run:

phpstan analyse --generate-baseline

It will generate a phpstan-baseline.neon file. The output can then be put straight into the ignoreErrors section of phpstan.neo.dist.

Is there a specific reason to use a PHP file for this?

@thelovekesh
Copy link
Collaborator Author

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.

@westonruter westonruter merged commit b51c19f into develop Mar 2, 2023
@westonruter westonruter deleted the fix/phpstan branch March 2, 2023 20:27
@thelovekesh
Copy link
Collaborator Author

QA Passed ✅

Up until 4337065, the base branch had no PHPStan issues.

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants