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

Update PHPStan level to 1 #1198

Merged
merged 7 commits into from
May 6, 2024
Merged

Update PHPStan level to 1 #1198

merged 7 commits into from
May 6, 2024

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 3, 2024

Summary

See #775.

This fixes the following issues when running npm run phpstan:

[ERROR] Found 109 errors
 ------ --------------------------------------------------------------------- 
  Line   plugins/dominant-color-images/hooks.php                              
 ------ --------------------------------------------------------------------- 
  145    Variable $data in empty() always exists and is always falsy.         
  149    Variable $extra_class in empty() always exists and is always falsy.  
 ------ --------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------- 
  Line   plugins/webp-uploads/helper.php                                  
 ------ ----------------------------------------------------------------- 
  160    Function remove_filter invoked with 4 parameters, 2-3 required.  
 ------ ----------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------- 
  Line   plugins/webp-uploads/hooks.php                                        
 ------ ---------------------------------------------------------------------- 
  556    Offset 'sources' on array{width: int, height: int, file:              
         non-falsy-string, sizes: array, image_meta: array, filesize: int} in  
         isset() does not exist.                                               
 ------ ---------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------- 
  Line   plugins/webp-uploads/image-edit.php                              
 ------ ----------------------------------------------------------------- 
  199    Function remove_filter invoked with 4 parameters, 2-3 required.  
  211    Function remove_filter invoked with 4 parameters, 2-3 required.  
 ------ ----------------------------------------------------------------- 

 ------ -------------------------------------------------------------------- 
  Line   tests/includes/admin/load-tests.php                                 
 ------ -------------------------------------------------------------------- 
  23     Offset 'options-general.php' on array{} in isset() does not exist.  
  36     Offset 'options-general.php' on array{} in isset() does not exist.  
 ------ -------------------------------------------------------------------- 

 ------ ---------------------------------------------------------- 
  Line   tests/includes/admin/server-timing-tests.php              
 ------ ---------------------------------------------------------- 
  26     Offset 'tools.php' on array{} in isset() does not exist.  
  39     Offset 'tools.php' on array{} in isset() does not exist.  
 ------ ---------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/auto-sizes/auto-sizes-test.php                         
 ------ --------------------------------------------------------------------- 
  22     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/dominant-color-images/dominant-color-test.php          
 ------ --------------------------------------------------------------------- 
  147    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  195    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  235    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/speculation-rules/speculation-rules-helper-test.php    
 ------ --------------------------------------------------------------------- 
  18     Constant WP_CONTENT_URL not found.                                   
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  25     Constant WP_CONTENT_URL not found.                                   
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------ 
  Line   tests/plugins/webp-uploads/data/class-wp-image-doesnt-support-webp.php  
 ------ ------------------------------------------------------------------------ 
  10     Constant WPINC not found.                                               
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols     
  11     Constant WPINC not found.                                               
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols     
 ------ ------------------------------------------------------------------------ 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/helper-tests.php                          
 ------ --------------------------------------------------------------------- 
  33     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  41     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  48     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  55     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  65     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  88     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  118    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  144    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  192    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  216    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  233    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  248    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  271    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  305    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  340    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  585    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  586    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/image-edit-tests.php                      
 ------ --------------------------------------------------------------------- 
  25     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  71     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  125    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  158    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  210    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  265    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  313    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  342    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  355    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  369    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  386    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  408    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  428    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  460    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  480    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  509    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/load-tests.php                            
 ------ --------------------------------------------------------------------- 
  46     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  78     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  109    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  140    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  172    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  198    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  225    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  244    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  274    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  301    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  317    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  352    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  381    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  404    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  474    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  498    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  563    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  564    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  577    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  602    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  626    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  627    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  644    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  672    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  701    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  720    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  745    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  763    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  803    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  830    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  862    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  915    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  936    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  954    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  963    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/plugins/webp-uploads/rest-api-tests.php                        
 ------ --------------------------------------------------------------------- 
  32     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  79     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   tests/utils/TestCase/DominantColorTestCase.php                       
 ------ --------------------------------------------------------------------- 
  16     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  21     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  26     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  31     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  37     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  42     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  47     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  52     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  58     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  63     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  68     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  73     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  79     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  84     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  89     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  94     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  99     Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  114    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  117    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  130    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
  133    Constant TESTS_PLUGIN_DIR not found.                                 
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ --------------------------------------------------------------------- 

Relevant technical choices

Please review individual commits.

@westonruter westonruter added the [Type] Bug An existing feature is broken label May 3, 2024
@westonruter westonruter added this to the performance-lab 3.1.0 milestone May 3, 2024
@westonruter westonruter force-pushed the update/phpstan-level-1 branch from cde5aab to de2bb64 Compare May 3, 2024 23:12
@@ -142,11 +142,11 @@ function dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $
$extra_class = $image_meta['has_transparency'] ? 'has-transparency' : 'not-transparent';
}

if ( ! empty( $data ) ) {
Copy link
Member Author

@westonruter westonruter May 3, 2024

Choose a reason for hiding this comment

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

PHPStan was complaining here:

Variable $data in empty() always exists and is always falsy.

For some reason switching to a simple boolean check fixed the issue. In any case, using empty() is discouraged per #1091 (comment)

Same goes for the change to $extra_class below.

Comment on lines -34 to -38

return $hook_suffix;
}

// @phpstan-ignore-next-line
Copy link
Member Author

Choose a reason for hiding this comment

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

As noted in #1188 (comment) this returning of the $hook_suffix was only being used in tests. This PR eliminates the need for that return value by instead computing the $hook_suffix in the test, and then seeing if the expected action is added.

@westonruter westonruter marked this pull request as ready for review May 3, 2024 23:17
Copy link

github-actions bot commented May 3, 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>

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

@@ -5,6 +5,9 @@
* @package performance-lab
*/

define( 'TESTS_PLUGIN_DIR', './' );
define( 'WPINC', 'wp-includes' );
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@szepeviktor szepeviktor Aug 27, 2024

Choose a reason for hiding this comment

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

I know there is no core function to get the value of WPINC but I do not want to encourage the users of my package to use WPINC.

@westonruter westonruter force-pushed the update/phpstan-level-1 branch from de2bb64 to 09d742d Compare May 3, 2024 23:23
@@ -88,11 +88,19 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
*
* @since 1.1.0
*
* @param array|null|WP_Error $image Image data {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} or null or WP_Error.
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 width, height, and mime-type are actually not used here. The only keys used are file and either path or filesize.

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

Without this type definition, the isset( $metadata['sources'][ $target_mime ]['file'] ) check below fails PHPStan because wp_get_attachment_metadata() doesn't normally return a sources key.

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 revisited this when bumping PHPStan to level 3. See #1200.

@westonruter westonruter merged commit 0c41032 into trunk May 6, 2024
25 of 26 checks passed
@westonruter westonruter deleted the update/phpstan-level-1 branch May 6, 2024 05:34
@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