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

Upgrade PHPStan to 1.11.6 #1325

Merged
merged 6 commits into from
Jul 1, 2024
Merged

Upgrade PHPStan to 1.11.6 #1325

merged 6 commits into from
Jul 1, 2024

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jul 1, 2024

This upgrades PHPStan to 1.11.6 which introduced two new issues.

 ------ -------------------------------------------------------------------------------------------------------------------- 
  Line   plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php                           
 ------ -------------------------------------------------------------------------------------------------------------------- 
  44     Offset 'background_image' might not exist on array{0?: string, 1?: string, background_image?: string, 2?: string}.  
 ------ -------------------------------------------------------------------------------------------------------------------- 

This is a case where PHPStan is not aware that at this point, the array keys are actually not nullable at this point. This is a bug in PHPStan which I've filed as phpstan/phpstan#11262. The issue seems to be that if the return value of preg_match() is casted to a bool or int then the array shape doesn't come through. So it's now checking to see if the return value is exactly 1 instead. Additionally, another check for '' !== $matches['background_image'] will be able to be removed once phpstan/phpstan#11222 is implemented, since we'll know that the array shape here has a non-empty-string.

 ------ ----------------------------------------------------------------- 
  Line   plugins/webp-uploads/hooks.php                                   
 ------ ----------------------------------------------------------------- 
  547    Variable $class_name in empty() always exists and is not falsy.  
 ------ -----------------------------------------------------------------

There was addressing the second if statement in the following code:

// Find the ID of each image by the class.
if ( ! preg_match( '/wp-image-([\d]+)/i', $img, $class_name ) ) {
continue;
}
if ( empty( $class_name ) ) {
continue;
}

It is a useless condition because since preg_match() is known to have succeeded, the $class_name will never be empty.

This fixes the issue by removing that condition, but also makes the code more robust by leveraging the HTML Tag Processor. It also adds missing test assertions for webp_uploads_update_image_references().

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Jul 1, 2024
@westonruter westonruter added this to the performance-lab n.e.x.t milestone Jul 1, 2024
Copy link

github-actions bot commented Jul 1, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

// Find the ID of each image by the class.
// TODO: It would be preferable to use the $processor->class_list() method but there seems to be some typing issues with PHPStan.
$class_name = $processor->get_attribute( 'class' );
if ( ! is_string( $class_name ) || ! preg_match( '/(?:^|\s)wp-image-([1-9]\d*)(?:\s|$)/i', $class_name, $matches ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more robust than before since it constraints looking for the class name inside the class attribute. Previously the string could be anywhere in the img tag.

continue;
}

// Make sure we use the last item on the list of matches.
$attachment_id = (int) $class_name[1];

if ( ! $attachment_id ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never be zero because the regex is now [1-9]\d*.

@@ -533,29 +533,28 @@ 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, switch to using the wp_content_img_tag filter? #1259

@adamsilverstein
Copy link
Member

Looks good! some test failing?

@westonruter
Copy link
Member Author

@adamsilverstein I re-ran the failed jobs. They're passing now. Seems like a wp-env hiccup.

@adamsilverstein adamsilverstein self-requested a review July 1, 2024 18:35
Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spiffy!

@westonruter westonruter merged commit 1ed2fbd into trunk Jul 1, 2024
17 checks passed
@westonruter westonruter deleted the upgrade/phpstan-1.11.6 branch July 1, 2024 18:40
@@ -39,9 +39,9 @@ public function __invoke( OD_HTML_Tag_Walker $walker ): bool {
if (
is_string( $style )
&&
0 < (int) preg_match( '/background(-image)?\s*:[^;]*?url\(\s*[\'"]?\s*(?<background_image>.+?)\s*[\'"]?\s*\)/', $style, $matches )
1 === preg_match( '/background(?:-image)?\s*:[^;]*?url\(\s*[\'"]?\s*(?<background_image>.+?)\s*[\'"]?\s*\)/', $style, $matches ) // Not compatible with strict rules. See <https://github.com/phpstan/phpstan/issues/11262>.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah. I should have removed the comment as it is now irrelevant after 75cd653 per phpstan/phpstan#11262 (comment). I'll do so as part of another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement of an existing feature
2 participants