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

Improve static analysis in Performance Lab plugin files #1188

Merged
merged 8 commits into from
May 3, 2024

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Apr 29, 2024

This fixes a bunch of static analysis issues identified by PHPStan, as well as a few from PhpStorm. This should go a long way to kickstart #775.

With PHPStan level 6, the following issues are resolved with this PR when running composer phpstan load.php includes/*:

PHPStan Output
 ------ ------------------------------------------------------------------------------------------------------------ 
  Line   includes/admin/load.php                                                                                     
 ------ ------------------------------------------------------------------------------------------------------------ 
  18     Function perflab_add_features_page() has no return type specified.                                          
  44     Function perflab_load_features_page() has no return type specified.                                         
  61     Function perflab_render_settings_page() has no return type specified.                                       
  79     Function perflab_admin_pointer() has no return type specified.                                              
  121    Function perflab_render_pointer() has no return type specified.                                             
  121    Function perflab_render_pointer() has parameter $args with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                   
  207    Function perflab_dismiss_wp_pointer_wrapper() has no return type specified.                                 
  222    Function perflab_enqueue_features_page_scripts() has no return type specified.                              
  234    Function perflab_install_activate_plugin_callback() has no return type specified.                           
  276    Function perflab_print_features_page_style() has no return type specified.                                  
  300    Function perflab_plugin_admin_notices() has no return type specified.                                       
 ------ ------------------------------------------------------------------------------------------------------------ 

 ------ --------------------------------------------------------------------- 
  Line   includes/admin/plugins.php                                           
 ------ --------------------------------------------------------------------- 
  84     Function perflab_render_plugins_ui() has no return type specified.   
  286    Function perflab_render_plugin_card() has no return type specified.  
 ------ --------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------------------------------------------- 
  Line   includes/admin/server-timing.php                                                                     
 ------ ----------------------------------------------------------------------------------------------------- 
  22     Function perflab_add_server_timing_page() has no return type specified.                              
  45     Function perflab_load_server_timing_page() has no return type specified.                             
  152    Function perflab_render_server_timing_page() has no return type specified.                           
  176    Function perflab_render_server_timing_page_hooks_field() has no return type specified.               
  208    Function perflab_render_server_timing_page_output_buffering_section() has no return type specified.  
 ------ ----------------------------------------------------------------------------------------------------- 

 ------ -------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   includes/server-timing/class-perflab-server-timing-metric.php                                                                               
 ------ -------------------------------------------------------------------------------------------------------------------------------------------- 
  72     Method Perflab_Server_Timing_Metric::set_value() has no return type specified.                                                              
  73     Call to function is_numeric() with float|int will always evaluate to true.                                                                  
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your phpstan.neon.  
  94     Call to function is_float() with float will always evaluate to true.                                                                        
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your phpstan.neon.  
  94     Result of && is always false.                                                                                                               
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your phpstan.neon.  
  120    Method Perflab_Server_Timing_Metric::measure_before() has no return type specified.                                                         
  132    Method Perflab_Server_Timing_Metric::measure_after() has no return type specified.                                                          
 ------ -------------------------------------------------------------------------------------------------------------------------------------------- 

 ------ -------------------------------------------------------------------------------------------------------------------------- 
  Line   includes/server-timing/class-perflab-server-timing.php                                                                    
 ------ -------------------------------------------------------------------------------------------------------------------------- 
  22     Property Perflab_Server_Timing::$registered_metrics type has no value type specified in iterable type array.              
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                 
  30     Property Perflab_Server_Timing::$registered_metrics_data type has no value type specified in iterable type array.         
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                 
  51     Method Perflab_Server_Timing::register_metric() has no return type specified.                                             
  51     Method Perflab_Server_Timing::register_metric() has parameter $args with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                 
  130    Method Perflab_Server_Timing::send_header() has no return type specified.                                                 
 ------ -------------------------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------------- 
  Line   includes/server-timing/defaults.php                                                                      
 ------ --------------------------------------------------------------------------------------------------------- 
  25     Function perflab_register_default_server_timing_before_template_metrics() has no return type specified.  
  138    Function perflab_register_default_server_timing_template_metrics() has no return type specified.         
  228    Function perflab_register_additional_server_timing_metrics_from_setting() has no return type specified.  
 ------ --------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------------------------------- 
  Line   includes/server-timing/load.php                                                                                            
 ------ --------------------------------------------------------------------------------------------------------------------------- 
  30     Function perflab_server_timing() has no return type specified.                                                             
  72     Function perflab_server_timing_register_metric() has no return type specified.                                             
  72     Function perflab_server_timing_register_metric() has parameter $args with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                  
  141    Function perflab_register_server_timing_setting() has no return type specified.                                            
  162    Function perflab_sanitize_server_timing_setting() return type has no value type specified in iterable type array.          
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                  
 ------ --------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------- 
  Line   includes/server-timing/object-cache.copy.php                                         
 ------ ------------------------------------------------------------------------------------- 
  50     Function perflab_load_server_timing_api_from_dropin() has no return type specified.  
 ------ ------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------------ 
  Line   includes/site-health/audit-autoloaded-options/helper.php                                                                
 ------ ------------------------------------------------------------------------------------------------------------------------ 
  20     Function perflab_aao_autoloaded_options_test() return type has no value type specified in iterable type array.          
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                               
  127    Function perflab_aao_query_autoloaded_options() return type has no value type specified in iterable type array.         
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                               
  259    Function perflab_aao_get_autoload_values_to_autoload() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                               
 ------ ------------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  Line   includes/site-health/audit-autoloaded-options/hooks.php                                                                       
 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  21     Function perflab_aao_add_autoloaded_options_test() has parameter $tests with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  21     Function perflab_aao_add_autoloaded_options_test() return type has no value type specified in iterable type array.            
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  35     Function perflab_aao_register_admin_actions() has no return type specified.                                                   
  45     Function perflab_aao_handle_update_autoload() has no return type specified.                                                   
  99     Function perflab_aao_admin_notices() has no return type specified.                                                            
 ------ ------------------------------------------------------------------------------------------------------------------------------ 

 ------ ----------------------------------------------------------------------------------------------------------------- 
  Line   includes/site-health/audit-enqueued-assets/helper.php                                                            
 ------ ----------------------------------------------------------------------------------------------------------------- 
  20     Function perflab_aea_enqueued_js_assets_test() return type has no value type specified in iterable type array.   
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                        
  114    Function perflab_aea_enqueued_css_assets_test() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                        
 ------ ----------------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------------------------------- 
  Line   includes/site-health/audit-enqueued-assets/hooks.php                                                                       
 ------ --------------------------------------------------------------------------------------------------------------------------- 
  22     Function perflab_aea_audit_enqueued_scripts() has no return type specified.                                                
  67     Function perflab_aea_audit_enqueued_styles() has no return type specified.                                                 
  114    Function perflab_aea_add_enqueued_assets_test() has parameter $tests with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                  
  114    Function perflab_aea_add_enqueued_assets_test() return type has no value type specified in iterable type array.            
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                  
  134    Function perflab_aea_clean_aea_audit_action() has no return type specified.                                                
  148    Function perflab_aea_invalidate_cache_transients() has no return type specified.                                           
 ------ --------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   includes/site-health/avif-support/helper.php                                                                       
 ------ ------------------------------------------------------------------------------------------------------------------- 
  20     Function avif_uploads_check_avif_supported_test() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                          
 ------ ------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  Line   includes/site-health/avif-support/hooks.php                                                                                   
 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  21     Function avif_uploads_add_is_avif_supported_test() has parameter $tests with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  21     Function avif_uploads_add_is_avif_supported_test() return type has no value type specified in iterable type array.            
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
 ------ ------------------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   includes/site-health/webp-support/helper.php                                                                       
 ------ ------------------------------------------------------------------------------------------------------------------- 
  20     Function webp_uploads_check_webp_supported_test() return type has no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                          
 ------ ------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  Line   includes/site-health/webp-support/hooks.php                                                                                   
 ------ ------------------------------------------------------------------------------------------------------------------------------ 
  21     Function webp_uploads_add_is_webp_supported_test() has parameter $tests with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  21     Function webp_uploads_add_is_webp_supported_test() return type has no value type specified in iterable type array.            
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
 ------ ------------------------------------------------------------------------------------------------------------------------------ 

 ------ -------------------------------------------------------------------------------------------------------- 
  Line   load.php                                                                                                
 ------ -------------------------------------------------------------------------------------------------------- 
  51     Function perflab_get_generator_content() has no return type specified.                                  
  74     Function perflab_render_generator() has no return type specified.                                       
  147    Function perflab_maybe_set_object_cache_dropin() has no return type specified.                          
  258    Function perflab_maybe_remove_object_cache_dropin() has no return type specified.                       
  301    Function perflab_no_access_redirect_module_to_performance_feature_page() has no return type specified.  
  322    Function perflab_cleanup_option() has no return type specified.                                         
 ------ -------------------------------------------------------------------------------------------------------- 

The strict PHPStan config I'm using (in phpstan.neon override):

includes:
	- phar://phpstan.phar/conf/bleedingEdge.neon
parameters:
	level: 6
	paths:
		- load.php
		- plugins/
		- includes/
		- tests/
	treatPhpDocTypesAsCertain: false
	checkGenericClassInNonGenericObjectType: false
	scanDirectories:
		- vendor/wp-phpunit/wp-phpunit/
	scanFiles:
		- bin/phpstan/functions.stub
	dynamicConstantNames:
		- PERFLAB_OBJECT_CACHE_DROPIN_VERSION
	bootstrapFiles:
		- load.php
		- bin/phpstan/constants.php
		- plugins/webp-uploads/load.php

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only labels Apr 29, 2024
@westonruter westonruter added this to the performance-lab 3.1.0 milestone Apr 29, 2024
@westonruter westonruter requested a review from thelovekesh April 29, 2024 23:01
@@ -32,6 +34,8 @@ function perflab_add_features_page() {

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.

Because PHPStan identifies correctly that this function is used as an action, and not a filter. So it shouldn't return anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

The returned value is used in testing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally the tests could be updated to not rely on this, but I think that is out of scope for this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -35,14 +37,16 @@ function perflab_add_server_timing_page() {

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.

Because PHPStan identifies correctly that this function is used as an action, and not a filter. So it shouldn't return anything.

@@ -48,6 +50,8 @@ function perflab_server_timing() {

return $server_timing;
}

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

Because PHPStan identifies correctly that this function is used as an action, and not a filter. So it shouldn't return anything.

@westonruter westonruter marked this pull request as ready for review April 29, 2024 23:07
Copy link

github-actions bot commented Apr 29, 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>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@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.

@westonruter westonruter force-pushed the improve/perflab-static-analysis branch from 39d14a9 to 468c49f Compare April 29, 2024 23:15
Base automatically changed from add/feature-dependencies to trunk May 3, 2024 04:10
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @westonruter for the PR. overall look good to me.

I still get on one error.

Ignored error pattern #^Function str_starts_with not found\.$# in paths: /Users/mukesh/performance/plugins/optimization-detective/optimization.php, /Users/mukesh/performance/plugins/speculation-rules/class-plsr-url-pattern-prefixer.php was not matched in reported errors.                                                                                                                                                                                                          
Ignored error pattern #^Function str_contains not found\.$# in paths: /Users/mukesh/performance/plugins/auto-sizes/hooks.php, /Users/mukesh/performance/plugins/dominant-color-images/hooks.php was not matched in reported errors.          

@thelovekesh
Copy link
Member

thelovekesh commented May 3, 2024

It's because PHPStan targets the 7.2 and above functions are PHP 8.0+ compatible and WP adds polyfills for the above functions. Either we can ignore them in baseline or create stubs for them.

We already have ignored them for a few files:

ignoreErrors:
# False positive as WordPress ships with a polyfill.
-
message: "#^Function str_starts_with not found\\.$#"
paths:
- load.php
- plugins/optimization-detective/optimization.php
- plugins/speculation-rules/class-plsr-url-pattern-prefixer.php
-
message: "#^Function str_contains not found\\.$#"
paths:
- plugins/auto-sizes/hooks.php
- plugins/dominant-color-images/hooks.php

but I think it would be better if we can add stubs since more files can use it and PHPStan can read types from stubs.

@westonruter
Copy link
Member Author

@thelovekesh so we add a new stubs file in bin/phpstan/?

@westonruter
Copy link
Member Author

That doesn't seem to work. Seems we have to just ignore the errors for now.

@westonruter
Copy link
Member Author

@mukeshpanchal27 @thelovekesh I've updated the ignoreErrors to remote the path since PhpStorm doesn't work with that yet per WI-63891. I've also added reportUnmatchedIgnoredErrors: false to avoid errors when checking a subset of the codebase.

$ composer phpstan load.php includes/*
> phpstan analyse --memory-limit=2048M 'load.php' 'includes/admin' 'includes/server-timing' 'includes/site-health'
Note: Using configuration file /home/westonruter/repos/performance/phpstan.neon.
 18/18 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

[OK] No errors

This is the stricter config I'm testing with:

includes:
	- phar://phpstan.phar/conf/bleedingEdge.neon
parameters:
	level: 6
	paths:
		- load.php
		- plugins/
		- includes/
		- tests/
	treatPhpDocTypesAsCertain: false
	checkGenericClassInNonGenericObjectType: false
	reportUnmatchedIgnoredErrors: false
	scanDirectories:
		- vendor/wp-phpunit/wp-phpunit/
	ignoreErrors:
		# False positive as WordPress ships with a polyfill.
		# Note that paths here don't work in PhpStorm due to <https://youtrack.jetbrains.com/issue/WI-63891>.
		- "#^Function str_starts_with not found\\.$#"
		- "#^Function str_contains not found\\.$#"

	dynamicConstantNames:
		- PERFLAB_OBJECT_CACHE_DROPIN_VERSION

	bootstrapFiles:
		- load.php
		- bin/phpstan/constants.php
		- plugins/webp-uploads/load.php

@westonruter
Copy link
Member Author

I'd like to get this and #1195 merged sooner than later merge conflicts will just keep popping up, and they improve the quality of the codebase to build upon.

@thelovekesh
Copy link
Member

so we add a new stubs file in bin/phpstan/?

Ideally, we should keep tools configs in a root level tools directory instead of bin which is mostly for executables. I can raise a PR to move them out of bin later.

That doesn't seem to work. Seems we have to just ignore the errors for now.

@westonruter Are you scanning a stubs file for symbol declaration? I am doing it like this and it works for me.

tools/phpstan/stubs.php
<?php

if ( ! function_exists( 'str_starts_with' ) ) {
	/**
	 * Returns true if the string starts with the needle, false otherwise.
	 *
	 * @param string $haystack The string to search in.
	 * @param string $needle The string to search for.
	 *
	 * @return bool
	 */
	function str_starts_with( $haystack, $needle ) { }
}

if ( ! function_exists( 'str_contains' ) ) {
	/**
	 * Returns true if the string starts with the needle, false otherwise.
	 *
	 * @param string $haystack The string to search in.
	 * @param string $needle The string to search for.
	 *
	 * @return bool
	 */
	function str_contains( $haystack, $needle ) { }
}
PHPStan config
diff --git a/phpstan.neon.dist b/phpstan.neon.dist
index 31ee0d46..2790ce58 100644
--- a/phpstan.neon.dist
+++ b/phpstan.neon.dist
@@ -11,12 +11,10 @@ parameters:
                - load.php
                - bin/phpstan/constants.php
                - plugins/webp-uploads/load.php
+       scanFiles:
+               - tools/phpstan/stubs.php
        scanDirectories:
                - vendor/wp-phpunit/wp-phpunit/
        dynamicConstantNames:
                - PERFLAB_OBJECT_CACHE_DROPIN_VERSION
        reportUnmatchedIgnoredErrors: false
-       ignoreErrors:
-               # False positive as WordPress ships with a polyfill.
-               - "#^Function str_starts_with not found\\.$#"
-               - "#^Function str_contains not found\\.$#"

@westonruter
Copy link
Member Author

Are you scanning a stubs file for symbol declaration? I am doing it like this and it works for me.

Oh, I was using stubFiles not scanFiles.

@thelovekesh
Copy link
Member

Oh, I was using stubFiles not scanFiles.

I think that's for reading accurate PHPDoc for symbol declarations. Ref: https://phpstan.org/user-guide/stub-files

@westonruter
Copy link
Member Author

Yeah, so then "stub" isn't technically the right term for this file, right? It's more of a compat file.

@thelovekesh
Copy link
Member

Yeah, so then "stub" isn't technically the right term for this file, right? It's more of a compat file.

Humm sounds right.

@westonruter
Copy link
Member Author

OK, with 2cb9ab0 the ignoreErrors is eliminated in favor of stub functions which are discovered via scanFiles.

// The return value is validated in JavaScript at:
// <https://github.com/WordPress/wordpress-develop/blob/d1e0a6241dcc34f4a5ed464a741116461a88d43b/src/js/_enqueues/admin/site-health.js#L65-L114>
// If the value lacks the required keys of test, label, and description then it is omitted.
return array( 'omitted' => true );
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 somewhat of a fix identified here through the use of stricter typing.

Copy link
Member

Choose a reason for hiding this comment

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

Should we keep the file extension as .php? So that PHPCS can lint it and have syntax highlighting in places like GitHub UI?

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 had it as a PHP file at first, but then static analysis complains. The use of .stub as the file extension is recommended in https://phpstan.org/user-guide/stub-files

Copy link
Member

Choose a reason for hiding this comment

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

Valid point. In that case, can we include it in PHPCS config?

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.

I don't think it's necessary because then PHPCS will complain about this as well:

FILE: /home/westonruter/repos/performance/bin/phpstan/functions.stub
----------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------------------------------------------------------------------------
 19 | ERROR | Function return type is not void, but function has no return statement (Squiz.Commenting.FunctionComment.InvalidNoReturn)
 35 | ERROR | Function return type is not void, but function has no return statement (Squiz.Commenting.FunctionComment.InvalidNoReturn)
----------------------------------------------------------------------------------------------------------------------------------------

Time: 232ms; Memory: 14MB

@@ -96,74 +108,104 @@ public function test_perflab_sanitize_server_timing_setting( $unsanitized, $expe
}

public function data_perflab_sanitize_server_timing_setting() {
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.

Changes in this data provider (and the test above) are due to https://github.com/WordPress/performance/pull/1188/files#r1589523090

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines 182 to +188
if ( ! is_array( $value ) ) {
return array();
$value = array();
}

$value = array_intersect_key( $value, $allowed_keys );
$value = wp_array_slice_assoc(
array_merge( perflab_get_server_timing_setting_default_value(), $value ),
array_keys( perflab_get_server_timing_setting_default_value() )
);
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 ensures the default value is always stored, while also being the default option in case it is not stored.

?>
<style type="text/css">
Copy link
Member Author

Choose a reason for hiding this comment

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

The type attribute is obsolete.

check_admin_referer( 'perflab_aao_update_autoload' );

if ( ! isset( $_GET['option_name'], $_GET['autoload'] ) ) {
wp_die( esc_html__( 'Missing required parameter.', 'performance-lab' ) );
}

$option_name = sanitize_text_field( wp_unslash( $_GET['option_name'] ) );
$autoload = isset( $_GET['autoload'] ) ? rest_sanitize_boolean( $_GET['autoload'] ) : false;
$autoload = rest_sanitize_boolean( $_GET['autoload'] );
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 the removal of the isset( $_GET['autoload'] ) check since it is already checked above.

Copy link
Member

@thelovekesh thelovekesh left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

I have one change request, but looks good otherwise.

@@ -0,0 +1,38 @@
<?php
/**
* Function stubs for PHPStan static analysis.
Copy link
Member

Choose a reason for hiding this comment

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

Does PHPStan require us to duplicate these, or can we configure them to be ignored since they are polyfilled in WP 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.

I'm not entirely clear why these have to be duplicated. I think it's because wp-stubs doesn't include polyfilled functions? See php-stubs/wordpress-stubs#100

Copy link
Member

Choose a reason for hiding this comment

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

I‘d just ignore the errors instead, personallly

Copy link
Member Author

Choose a reason for hiding this comment

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

@swissspidy What about 828c87f?

The only issue I've seen with ignoring errors is that it causes problems when there are ignored errors which are not present in the subset of files being analyzed. Then you have to add reportUnmatchedIgnoredErrors: false. Clearly this doesn't matter if these two PHP8 functions are the only errors being ignored, but if you ignore other errors which no longer are present anymore then they could get ignored without being present.

Copy link
Member

Choose a reason for hiding this comment

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

I don't fully understand the problem you are describing about reportUnmatchedIgnoredErrors, but 828c87f works for me.

Comment on lines -18 to -30
ignoreErrors:
# False positive as WordPress ships with a polyfill.
-
message: "#^Function str_starts_with not found\\.$#"
paths:
- load.php
- plugins/optimization-detective/optimization.php
- plugins/speculation-rules/class-plsr-url-pattern-prefixer.php
-
message: "#^Function str_contains not found\\.$#"
paths:
- plugins/auto-sizes/hooks.php
- plugins/dominant-color-images/hooks.php
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep this, but use /* to ignore these rules globally, rather than copying the shims to our repo.

Copy link
Member Author

Choose a reason for hiding this comment

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

So like what was done in 51d1894?

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 think I have a better solution. See 828c87f

@westonruter westonruter requested a review from joemcgill May 3, 2024 19:04
@@ -0,0 +1,38 @@
<?php
/**
* Function stubs for PHPStan static analysis.
Copy link
Member

Choose a reason for hiding this comment

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

I don't fully understand the problem you are describing about reportUnmatchedIgnoredErrors, but 828c87f works for me.

@@ -204,7 +219,7 @@ static function ( $hookname ) {
);
}

$value['output_buffering'] = ! empty( $value['output_buffering'] );
$value['output_buffering'] = (bool) $value['output_buffering'];
Copy link
Member

Choose a reason for hiding this comment

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

interesting, I assume phpstan flagged this?

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 it's something I applied based on #1091 (comment)

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.

Great!

@westonruter westonruter merged commit 603610d into trunk May 3, 2024
25 checks passed
@westonruter westonruter deleted the improve/perflab-static-analysis branch May 3, 2024 19:53
@@ -122,9 +122,9 @@ function perflab_aao_autoloaded_options_size() {
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return array Autoloaded data as option names and their sizes.
* @return array<string, object{option_name: string, option_value_length: int}> Autoloaded data as option names and their sizes.
Copy link
Member Author

@westonruter westonruter May 6, 2024

Choose a reason for hiding this comment

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

This is wrong. It is not an associative array. It should rather have been:

- * @return array<string, object{option_name: string, option_value_length: int}> Autoloaded data as option names and their sizes.
+ * @return array<object{option_name: string, option_value_length: int}> Autoloaded data as option names and their sizes.

Copy link
Member Author

Choose a reason for hiding this comment

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

@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
[Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only skip changelog PRs that should not be mentioned in changelogs [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants