Fix PHPStan errors

Created on 16 July 2023, over 1 year ago

Problem/Motivation

After running PHPstan Drupal at level 9, I got the following errors.

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------ 
  Line   src/Plugin/views/display/ViewsBlockOverride.php                                                                                                 
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------ 
  71     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::optionsSummary() has no return type specified.                      
  71     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::optionsSummary() has parameter $categories with no type specified.  
  71     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::optionsSummary() has parameter $options with no type specified.     
  155    \Drupal calls should be avoided in classes, use dependency injection instead                                                                    
  172    \Drupal calls should be avoided in classes, use dependency injection instead                                                                    
  236    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockSubmit() has no return type specified.                         
  242    Argument of an invalid type mixed supplied for foreach, only iterables are supported.                                                           
  242    Cannot access offset string|null on mixed.                                                                                                      
  243    Argument of an invalid type mixed supplied for foreach, only iterables are supported.                                                           
  274    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::preBlockBuild() has no return type specified.                       
  290    Access to an undefined property Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::$block.                                    
         πŸ’‘ Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                                            
  296    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::execute() has no return type specified.                             
  297    Access to an undefined property Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::$block.                                    
         πŸ’‘ Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                                                            
  329    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::usesExposed() has no return type specified.                         
  336    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::displaysExposed() has no return type specified.                     
  343    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::buildOptionsForm() has no return type specified.                    
  343    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::buildOptionsForm() has parameter $form with no type specified.      
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------ 


 [ERROR] Found 17 errors         

Steps to reproduce

Install PHPstan Drupal and the needed packages, run on level 9. The errors listed above are displayed.

Proposed resolution

Fix the errors.

Remaining tasks

The code has already been modified and the errors fixed, I now just need to test thoroughly on both an existing site (not my D9 clean install) and also on a D10 instance.

Then review by someone else and merge the code.

User interface changes

None

API changes

None

Data model changes

None

πŸ“Œ Task
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @dtfabio
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

    After testing on an existing site and briefly going through the views, I got the following error. This was because the parent execute function did not always return an empty array, it also returned NULL. To prevent this, I made the return type of the modules execute function nullable.

    The website encountered an unexpected error. Please try again later.
    TypeError: Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::execute(): Return value must be of type array, null returned in Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride->execute() (line 392 of modules/contrib/views_block_override/src/Plugin/views/display/ViewsBlockOverride.php).
    
  • Merge request !18Issue #3374860: Fix PHPStan errors β†’ (Open) created by dtfabio
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove
  • Status changed to Needs work about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium tim-diels Belgium πŸ‡§πŸ‡ͺ

    Just tested and still has errors on level 9:

     ------ -------------------------------------------------------------------------------------------------------------
      Line   src/Plugin/views/display/ViewsBlockOverride.php
     ------ -------------------------------------------------------------------------------------------------------------
      64     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::__construct() has parameter
             $configuration with no value type specified in iterable type array.
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
      84     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::create() has parameter
             $configuration with no value type specified in iterable type array.
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
      85     Unsafe usage of new static().
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
      99     Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::defineOptions() 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
      118    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockSettings() has parameter
             $settings with no value type specified in iterable type array.
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
      118    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockSettings() 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
      177    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockForm() has parameter $form
             with no value type specified in iterable type array.
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
      177    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockForm() 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
      302    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::blockSubmit() has parameter
             $form with no value type specified in iterable type array.
             πŸ’‘ See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
      365    Method Drupal\views_block_override\Plugin\views\display\ViewsBlockOverride::execute() 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
     ------ -------------------------------------------------------------------------------------------------------------
    
    
    
     [ERROR] Found 10 errors
  • Assigned to dtfabio
  • Status changed to Active 11 months ago
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

    dtfabio β†’ changed the visibility of the branch 3374860-fix-phpstan-errors to hidden.

  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

    dtfabio β†’ changed the visibility of the branch 3374860-fix-phpstan-errors to active.

  • Issue was unassigned.
  • Status changed to Needs work 11 months ago
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

    Hi Tim,

    I tried to fix the last issues, but saw there were merge conflicts so wanted to merge the dev branch into my branch.

    Had apparently accidentally used the 1.2.x branch where changes happened to it, preventing me from creating a working patch. I then tried to fix this, without success.

    I finally decided to just make a new branch with the latest changes, only with this I still can't make a working patch. I don't immediately see what the problem is, so I just provided the code and maybe you know the reason?

    Greetings,

    Fabio

  • πŸ‡ΊπŸ‡¦Ukraine Anna D

    Added comments to the MR and honestly don't understand why I'm anonymous above.

  • Pipeline finished with Failed
    13 days ago
    Total: 163s
    #360506
  • πŸ‡ΊπŸ‡¦Ukraine Anna D

    Added .gitlab-ci for automated GitLab tests. Phpcs and phpstan pipelines still fails.

  • Pipeline finished with Failed
    10 days ago
    Total: 138s
    #362974
  • Pipeline finished with Failed
    10 days ago
    Total: 140s
    #362994
  • Pipeline finished with Failed
    10 days ago
    #362996
  • Hello @anna!!
    I have solved the phpcs errors and tried to solve phpstan error but there is a error in code in use of elseif that is causing the another error.
    I think that one need to be solved first.

  • πŸ‡ΊπŸ‡¦Ukraine Anna D
  • πŸ‡§πŸ‡ͺBelgium tim-diels Belgium πŸ‡§πŸ‡ͺ

    You should focus on fixing one task at a time to make it easier to follow up. You can create separate tasks to fix other things and I would suggest on doing so. I will then make some time to review tasks when they are split to simpler tasks.

  • πŸ‡ΊπŸ‡¦Ukraine Anna D

    Hello @tim-diels,

    I didn't saw that the 1.0.x-dev version was targeted instead of 1.2.x-dev. I created a separate issue for GitLab CI/CD for 1.2.x-dev, and there are only two PHPStan errors:

    Line src/Plugin/views/display/ViewsBlockOverride.php
    ------ ----------------------------------------------------------------------
    181 \Drupal calls should be avoided in classes, use dependency injection
    instead
    198 \Drupal calls should be avoided in classes, use dependency injection
    instead
    ------ ----------------------------------------------------------------------
    [ERROR] Found 2 errors

    I've created a separate issue specifically to address the dependency injection in the class. I'm not sure if you'd prefer to fix everything separately for 1.0.x-dev or just close this issue.

Production build 0.71.5 2024