Fix phpstan issues reported by GitLab CI

Created on 20 September 2024, 7 months ago

Problem/Motivation

GitLab CI phpstan scan reports some issues:

------ ------------------------------------------------------------------- 
  Line   src/Commands/EncryptCommands.php                                   
 ------ ------------------------------------------------------------------- 
  135    Method Drupal\encrypt\Commands\EncryptCommands::validateProfile()  
         should return array but return statement is missing.               
 ------ ------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   src/EncryptService.php                                                
 ------ ---------------------------------------------------------------------- 
  51     \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
  78     Method Drupal\encrypt\EncryptService::encrypt() should return string  
         but return statement is missing.                                      
  91     Method Drupal\encrypt\EncryptService::decrypt() should return string  
         but return statement is missing.                                      
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------- 
  Line   src/EncryptionMethodManager.php                     
 ------ ---------------------------------------------------- 
  12     Plugin definitions cannot be altered.               
  25     Missing cache backend declaration for performance.  
 ------ ---------------------------------------------------- 
 ------ ----------------------------------------------------------------------- 
  Line   src/Entity/EncryptionProfile.php                                       
 ------ ----------------------------------------------------------------------- 
  143    Method Drupal\encrypt\Entity\EncryptionProfile::getEncryptionMethod()  
         should return Drupal\encrypt\EncryptionMethodInterface but return      
         statement is missing.                                                  
 ------ ----------------------------------------------------------------------- 
 ------ ----------------------------------------------------------------- 
  Line   src/Form/EncryptionProfileForm.php                               
 ------ ----------------------------------------------------------------- 
  375    Method Drupal\encrypt\Form\EncryptionProfileForm::save() should  
         return int but return statement is missing.                      
 ------ ----------------------------------------------------------------- 
 ------ -------------------------------------------------------- 
  Line   tests/src/Unit/Entity/EncryptionProfileTest.php         
 ------ -------------------------------------------------------- 
  87     Call to deprecated method setMethods() of class         
         PHPUnit\Framework\MockObject\MockBuilder:               
         https://github.com/sebastianbergmann/phpunit/pull/3687  
  103    Call to deprecated method setMethods() of class         
         PHPUnit\Framework\MockObject\MockBuilder:               
         https://github.com/sebastianbergmann/phpunit/pull/3687  
  205    Call to deprecated method setMethods() of class         
         PHPUnit\Framework\MockObject\MockBuilder:               
         https://github.com/sebastianbergmann/phpunit/pull/3687  
  239    Call to deprecated method setMethods() of class         
         PHPUnit\Framework\MockObject\MockBuilder:               
         https://github.com/sebastianbergmann/phpunit/pull/3687  
  267    Call to deprecated method setMethods() of class         
         PHPUnit\Framework\MockObject\MockBuilder:               
         https://github.com/sebastianbergmann/phpunit/pull/3687  
 ------ -------------------------------------------------------- 
 [ERROR] Found 13 errors                                                

Let's fix these.

πŸ“Œ Task
Status

Needs review

Version

3.0

Component

Code

Created by

πŸ‡―πŸ‡΅Japan ptmkenny

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

Merge Requests

Comments & Activities

  • Issue created by @ptmkenny
  • Merge request !13fix phpstan issues β†’ (Merged) created by ptmkenny
  • Pipeline finished with Failed
    7 months ago
    Total: 219s
    #287825
  • Pipeline finished with Success
    7 months ago
    Total: 246s
    #287837
  • Pipeline finished with Success
    7 months ago
    Total: 147s
    #287839
  • Pipeline finished with Success
    7 months ago
    Total: 371s
    #287842
  • Pipeline finished with Success
    7 months ago
    Total: 147s
    #287853
  • Pipeline finished with Success
    7 months ago
    Total: 415s
    #287856
  • Status changed to Needs review 7 months ago
  • πŸ‡―πŸ‡΅Japan ptmkenny

    This fixes all phpstan issues identified by phpstan. Those that don't really need to be fixed are ignored appropriately, either in-line or in the phpstan.neon file (newly added with this MR).

    Most of these fixes are very minor (phpdoc types, for example), but there is one important one-- at present, the cache backend never gets set for EncryptionMethod (fixed in this MR).

  • πŸ‡―πŸ‡΅Japan ptmkenny

    This MR also requires phpstan to pass so that future MRs don't introduce new errors.

  • Pipeline finished with Failed
    7 months ago
    Total: 202s
    #287860
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States

    This is great. Thank you, @ptmkenny. I love to see all that green in the pipelines.

  • πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States

    Well, it looks as though we'll have to wait to require PHPStan to pass until after we drop backward compatibility for earlier versions of Drupal. The code to ignore PHPStan errors related to setMethods() can either be in phpstan.neon or as @phpstan-ignore-next-line lines in EncryptionProfileTest.php. Either would be fine with me, though I suppose I have a slight preference for the latter, since removal of those ignore lines could then just be part of the removal of the backward-compatibility code to which it relates. Either way, once we have a new release that supports Drupal 11, it can be the last one compatible with D8 and D9, and we can begin the process of stripping out backward-compatibility.

  • πŸ‡―πŸ‡΅Japan ptmkenny

    Thanks for looking at this. As for where to put the ignore for setMethods, I don't have a strong preference; I put it in phpstan.neon originally because phpstan will throw an error if you have ignores anywhere that never "hit", so as soon as all the deprecated code is removed, phpstan will throw an error (ignoring an error that isn't present), and then that line can get removed from phpstan.neon. But it works the same way with @phpstan-ignore-next-line; if the error is "fixed" for the line, then phpstan will throw an error if the ignore remains in place. So I think this is ready to go.

  • πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States

    I addressed the issue with PHPStan and backward compatibility, but now PHP CodeSniffer tests are failing on files that didn't change.

  • πŸ‡―πŸ‡΅Japan ptmkenny

    Very strange. I pulled your changes locally and ran phpcs-- everything seems fine.

  • Pipeline finished with Failed
    7 months ago
    Total: 317s
    #290813
  • πŸ‡―πŸ‡΅Japan ptmkenny

    Ok, I found the issue-- the coding standards themselves got updated overnight (drupal/coder (8.3.24 => 8.3.25)).

    I ran the auto-updates, but EncryptService's third argument is allowed to be null, which I'm guessing is a backwards-compatible addition for the 3.x branch. However, there is no formal deprecation for 4.x, so if it is supposed to be deprecated, perhaps we should create another issue and add a formal deprecation. Or, if it's not supposed to be deprecated, my comment should be revised.

  • Pipeline finished with Success
    7 months ago
    Total: 223s
    #290820
  • πŸ‡―πŸ‡΅Japan ptmkenny

    The coding standards are also fixed in a separate issue πŸ“Œ phpcs september 2024 update Active , so either that issue should be committed first and then this one, or this one should be committed and that one closed.

  • πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States
  • πŸ‡ΊπŸ‡ΈUnited States rlhawk Seattle, Washington, United States
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024