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 problems reported by static analysis #93

Merged
merged 1 commit into from
May 4, 2022

Conversation

szepeviktor
Copy link
Contributor

@szepeviktor szepeviktor commented May 1, 2022

Description of the Change

Ran @phpstan and fixed reported problems.

Verification Process

Nope.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Fixed coding standards

@jeffpaul jeffpaul requested review from a team and dinhtungdu and removed request for a team May 2, 2022 15:29
@jeffpaul jeffpaul added this to the 2.5.0 milestone May 2, 2022
Copy link
Contributor

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

Hi @szepeviktor, thanks for the PR!

Can you please share the config and analysis result running PHPStan? Running PHPStan locally with rule level set to 6 and I got a different report:

My environment
% php -v                                                             
PHP 8.0.0 (cli) (built: Dec 17 2020 18:30:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies
    with Xdebug v3.0.0, Copyright (c) 2002-2020, by Derick Rethans

% composer show   
10up/phpcs-composer                            dev-master 2f5c360
dealerdirect/phpcodesniffer-composer-installer dev-master 7d5cb88 PHP_CodeSniffer Standards Composer Installer Plugin
phpcompatibility/php-compatibility             9.3.5              A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.
phpcompatibility/phpcompatibility-paragonie    1.3.1              A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accou...
phpcompatibility/phpcompatibility-wp           2.1.2              A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting fo...
phpstan/phpstan                                1.7.x-dev fc12215  PHPStan - PHP Static Analysis Tool
squizlabs/php_codesniffer                      dev-master f268ca4 PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
wp-coding-standards/wpcs                       2.3.0              PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions
Config file
parameters:
        level: 6
        paths:
                - ./simple-page-ordering.php
        scanDirectories:
                - /Users/tenup/Sites/oss/app/public/wp-includes/
Report
130 % ./vendor/bin/phpstan analyse -c phpstan.neon                                                                        
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ----------------------------------------------------------------------------------------------------------------------------- 
  Line   simple-page-ordering.php                                                                                                     
 ------ ----------------------------------------------------------------------------------------------------------------------------- 
  32     Class Simple_Page_Ordering referenced with incorrect case: Simple_page_Ordering.                                             
  54     Method Simple_Page_Ordering::add_actions() has no return type specified.                                                     
  64     Method Simple_Page_Ordering::load_textdomain() has no return type specified.                                                 
  71     Method Simple_Page_Ordering::load_edit_screen() has no return type specified.                                                
  72     Function get_current_screen not found.                                                                                       
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols                                                           
  102    Method Simple_Page_Ordering::wp() has no return type specified.                                                              
  104    Function get_current_screen not found.                                                                                       
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols                                                           
  124    Method Simple_Page_Ordering::admin_head() has no return type specified.                                                      
  125    Function get_current_screen not found.                                                                                       
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols                                                           
  191    Method Simple_Page_Ordering::page_ordering() has invalid return type obj.                                                    
  191    Method Simple_Page_Ordering::page_ordering() has parameter $excluded with no value type specified in iterable type array.    
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  369    Method Simple_Page_Ordering::page_ordering() should return obj|WP_Error but returns string.                                  
  375    Method Simple_Page_Ordering::page_ordering() should return obj|WP_Error but returns stdClass.                                
  385    Method Simple_Page_Ordering::sort_by_order_link() has parameter $views with no value type specified in iterable type array.  
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type                                     
  385    Method Simple_Page_Ordering::sort_by_order_link() 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                                     
  414    Method Simple_Page_Ordering::rest_api_init() has no return type specified.                                                   
  460    Method Simple_Page_Ordering::rest_page_ordering() has no return type specified.                                              
  468    Result of && is always false.                                                                                                
  468    Variable $nextid in isset() always exists and is not nullable.                                                               
  468    Variable $previd in isset() always exists and is not nullable.                                                               
 ------ ----------------------------------------------------------------------------------------------------------------------------- 


                                                                                                                        
 [ERROR] Found 20 errors                                                                                                

@szepeviktor
Copy link
Contributor Author

szepeviktor commented May 3, 2022

Just two commands.

# PHP 8.0
composer require --dev szepeviktor/phpstan-wordpress
vendor/bin/phpstan analyze -c vendor/szepeviktor/phpstan-wordpress/extension.neon -l 5 simple-page-ordering.php
 ------ -----------------------------------------------------------------------------------------------
  Line   simple-page-ordering.php
 ------ -----------------------------------------------------------------------------------------------
  32     Class Simple_Page_Ordering referenced with incorrect case: Simple_page_Ordering. 👈
  77     Function apply_filters invoked with 3 parameters, 2 required.
  191    Method Simple_Page_Ordering::page_ordering() has invalid return type obj. 👈
  369    Method Simple_Page_Ordering::page_ordering() should return obj|WP_Error but returns string. 👈
  375    Method Simple_Page_Ordering::page_ordering() should return obj|WP_Error but returns stdClass. 👈
  408    Function apply_filters invoked with 3 parameters, 2 required.
  468    Result of && is always false. 👈
  468    Variable $nextid in isset() always exists and is not nullable. 👈
  468    Variable $previd in isset() always exists and is not nullable. 👈
 ------ -----------------------------------------------------------------------------------------------

Marked ones are resolved in PR-s. The ones mentioning apply_filters could be ignored.

@szepeviktor
Copy link
Contributor Author

On higher levels (6, 7, 8, 9) you have to supply more information to PHPStan in docblocks/typehints.

@dinhtungdu
Copy link
Contributor

Just two commands.

@szepeviktor Running your command on this PR, I got this report

 ------ ---------------------------------------------------------------- 
  Line   simple-page-ordering.php                                        
 ------ ---------------------------------------------------------------- 
  77     Function apply_filters invoked with 3 parameters, 2 required.   
  408    Function apply_filters invoked with 3 parameters, 2 required.   
  468    Result of && is always false.                                   
  468    Variable $nextid in isset() always exists and is not nullable.  
  468    Variable $previd in isset() always exists and is not nullable.  
 ------ ---------------------------------------------------------------- 

The first two are safe to ignore, but do you think we should fix the remaining?

@szepeviktor
Copy link
Contributor Author

The first two are safe to ignore, but do you think we should fix the remaining?

Please see #94

@dinhtungdu dinhtungdu merged commit c0541f3 into 10up:develop May 4, 2022
@szepeviktor szepeviktor deleted the patch-1 branch May 4, 2022 05:32
@jeffpaul jeffpaul modified the milestones: 2.5.0, 2.4.1 May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants