Skip to content

Update PHPStan level to 3 #1200

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

Merged
merged 11 commits into from
May 6, 2024
Merged

Update PHPStan level to 3 #1200

merged 11 commits into from
May 6, 2024

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 4, 2024

See #775.

This fixes the following PHPStan issues:

[ERROR] Found 42 errors
------ ----------------------------------------------------------------------------------------------------------------------------------------- 
  Line   plugins/dominant-color-images/hooks.php                                                                                                  
 ------ ----------------------------------------------------------------------------------------------------------------------------------------- 
  66     Offset 'dominant_color' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.  
  68     Offset 'dominant_color' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.  
  130    Offset 'dominant_color' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.  
  133    Offset 'dominant_color' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.  
  135    Offset 'dominant_color' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.  
 ------ ----------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------- 
  Line   plugins/webp-uploads/hooks.php          
 ------ ---------------------------------------- 
  301    Cannot assign offset string to string.  
 ------ ---------------------------------------- 

 ------ ----------------------------------------------------- 
  Line   tests/includes/admin/server-timing-tests.php         
 ------ ----------------------------------------------------- 
  72     Offset 'perflab-server…' does not exist on array{}.  
  76     Offset 'perflab-server…' does not exist on array{}.  
 ------ ----------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/image-edit-tests.php                                                                                         
 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 
  45     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  81     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  91     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  181    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  182    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  220    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  324    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/load-tests.php                                                                                               
 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 
  56     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  57     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  88     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  89     Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  119    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  120    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  150    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  151    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  329    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  342    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  363    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  369    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  390    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  394    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  484    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  484    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  530    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  530    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  589    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
  782    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  783    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  783    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}.        
  809    Offset 'sources' does not exist on array{width: int, height: int, file: string, sizes: array, image_meta: array, filesize: int}|false.  
 ------ ---------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------ 
  Line   tests/plugins/webp-uploads/rest-api-tests.php         
 ------ ------------------------------------------------------ 
  37     Cannot assign offset 'id' to WP_REST_Request<array>.  
  37     WP_REST_Request<array> does not accept int|WP_Error.  
  83     Cannot assign offset 'id' to WP_REST_Request<array>.  
  83     WP_REST_Request<array> does not accept int|WP_Error.  
 ------ ------------------------------------------------------   

Please review individual commits for changes.

@westonruter westonruter added the [Type] Bug An existing feature is broken label May 4, 2024
@westonruter westonruter added this to the performance-lab 3.1.0 milestone May 4, 2024
Copy link

github-actions bot commented May 4, 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: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: thelovekesh <thelovekesh@git.wordpress.org>

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

Comment on lines 24 to 26
if ( ! is_array( $metadata ) ) {
$metadata = array();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Checks like this and the use of mixed as a possible param type are needed because plugins can add filters that stick any type in for $metadata.

@westonruter westonruter force-pushed the update/phpstan-level-3 branch from 8aaed27 to b9356ad Compare May 4, 2024 00:31
Comment on lines -283 to -285
* @param string $output_format The image editor default output format mapping.
* @param string $filename Path to the image.
* @param string $mime_type The source image mime type.
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that webp_uploads_filter_image_editor_output_format() is added as a filter for image_editor_output_format. This filter is defined as taking these params:

		 * @param string[] $output_format {
		 *     An array of mime type mappings. Maps a source mime type to a new
		 *     destination mime type. Default empty array.
		 *
		 *     @type string ...$0 The new mime type.
		 * }
		 * @param string $filename  Path to the image.
		 * @param string $mime_type The source image mime type.

So the use of string here for $output_format is incorrect. It should have been array<string, string> in the plugin, and in core string[] is not as specific as it could be (since it implies numeric keys).

Additionally, I tried using just string PHP types for $filename and $mimetype, but when running unit tests I got a failure. This is because the function that applies the filter is \WP_Image_Editor::get_output_format() and this function takes null as default values for $filename and $mime_type, which is what is passed when using a factory to create an image during testing. So core should actually be typing the filter:

		 * @param string|null $filename  Path to the image.
		 * @param string|null $mime_type The source image mime type.

These are accounted for with the new function signature for webp_uploads_filter_image_editor_output_format().

Comment on lines -36 to -37
$request = new WP_REST_Request();
$request['id'] = $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.

PHPStan was complaining here:

  37     Cannot assign offset 'id' to WP_REST_Request<array>.  
  37     WP_REST_Request<array> does not accept int|WP_Error.

Apparently WP_REST_Request is not typed sufficiently in core for PHPStan to accept setting an array offset. So opting for the set_param() method is an easy fix for this.

Base automatically changed from update/phpstan-level-2 to trunk May 6, 2024 05:45
*
* @since n.e.x.t
*
* @param int $attachment_id Attachment post ID. Defaults to global $post.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param int $attachment_id Attachment post ID. Defaults to global $post.
* @param int $attachment_id Attachment post ID. Defaults 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

True, but wp_get_attachment_metadata defaults to $post which is what this function will do when zero is passed. I copied this phpdoc from core.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, instead of adding a helper wrapper function with the type definition, maybe it would be better to add a stub function definition for PHPStan to read?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, a stub is better for consistent PHPDoc comments.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've implemented the stub in 3e8fed7 which improved a lot.

Comment on lines -553 to -572

/**
* Metadata potentially amended by webp_uploads_create_sources_property().
*
* Note the sources key is not normally present in the response for wp_get_attachment_metadata(). The sources
* key here, however, is being injected via the 'wp_generate_attachment_metadata' filter via the
* webp_uploads_create_sources_property() function.
*
* @see webp_uploads_create_sources_property()
*
* @var array{
* width: int,
* height: int,
* file: non-falsy-string,
* sizes: array,
* image_meta: array,
* filesize: int,
* sources?: array<string, array{ file: string, filesize: int }>
* } $metadata
*/
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 all eliminated because of the introduction of the function stub which includes the additional typing for the array keys added by filters in the webp-uploads and dominant-color-images plugins.

@westonruter westonruter merged commit 6721e23 into trunk May 6, 2024
@westonruter westonruter deleted the update/phpstan-level-3 branch May 6, 2024 19:47
@westonruter westonruter added the skip changelog PRs that should not be mentioned in changelogs label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip changelog PRs that should not be mentioned in changelogs [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants