Analyse and fix static code issues using PHPStan

Created on 6 March 2022, over 2 years ago
Updated 9 August 2023, over 1 year ago

Run PHPStan on this module's code to check for static code issues and / or unnecessary code. See core issue #3178534: Start running PHPStan on Drupal core (level 0) .

This issue is part of the 3.x Roadmap: 🌱 PWA module 3.x roadmap Active .

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇨🇦Canada ambient.impact Toronto

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany Grevil

    I don't quite understand this issue. Do you mean "Analyse this module using PHPStan"? Since I don't think it needs to provide its own phpstan.neon.

  • 🇨🇦Canada ambient.impact Toronto

    Yup, that's what I meant. Feel free to rename it and amend the issue summary, as it's just a placeholder for now.

  • Issue was unassigned.
  • 🇩🇪Germany Anybody Porta Westfalica

    No work has been done here by @Ambient.Impact so removing the assignment.

    @Grevil: Would you like to do this and provide a MR?

  • Assigned to Grevil
  • 🇩🇪Germany Grevil

    Here is the PHPStan output:

    14/14 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
    
     ------ -------------------------------------------------------------------------------------- 
      Line   modules/pwa_extras/pwa_extras.module
     ------ --------------------------------------------------------------------------------------
      32     Access to an undefined property DOMNode::$tagName.
             💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
      53     Function pwa_extras_apple_touch_icons invoked with 1 parameter, 0 required.
     ------ --------------------------------------------------------------------------------------
    
     ------ --------------------------------------------------------------------------------------------------------------------------------------
      Line   modules/pwa_extras/src/Form/PwaExtrasSettingsForm.php
     ------ --------------------------------------------------------------------------------------------------------------------------------------
      37     Function pwa_extras_apple_touch_icons invoked with 1 parameter, 0 required.
      114    Method Drupal\pwa_extras\Form\PwaExtrasSettingsForm::getEditableConfigNames() has invalid return type Drupal\pwa_extras\Form\config.
     ------ --------------------------------------------------------------------------------------------------------------------------------------
    
     ------ -----------------------------------------------------------------------------------------------------------------
      Line   src/Controller/PWAController.php
     ------ -----------------------------------------------------------------------------------------------------------------
      200    Variable $data in empty() always exists and is not falsy.
      222    Regex pattern is invalid: The /e modifier is no longer supported, use preg_replace_callback instead in pattern:
             #(/(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|"|'|:|<|$|\.\s)#ie
     ------ -----------------------------------------------------------------------------------------------------------------
    
     ------ -----------------------------------------------------------------------------------------
      Line   src/Form/ManifestConfigurationForm.php
     ------ -----------------------------------------------------------------------------------------
      405    Cannot call method basePath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
     ------ -----------------------------------------------------------------------------------------
    
     -- ----------------------------------------------------------------------------------------------------------------------------------
         Error
     -- ----------------------------------------------------------------------------------------------------------------------------------
         Ignored error pattern #^Plugin definitions cannot be altered.# was not matched in reported errors.
         Ignored error pattern #^Missing cache backend declaration for performance.# was not matched in reported errors.
         Ignored error pattern #cache tag might be unclear and does not contain the cache key in it.# was not matched in reported errors.
         Ignored error pattern #^Class .* extends @internal class# was not matched in reported errors.
     -- ----------------------------------------------------------------------------------------------------------------------------------
    
                                                                                                                            
     [ERROR] Found 11 errors                                                                                                
                                                                                                                            
    
    Failed to run phpstan.sh /var/www/html/web/modules/custom/pwa-3268062: exit status 1
    
  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany Grevil

    Fixed a few PHPStan issues. These are the remaining problems:

     ------ -------------------------------------------------------------------------------------- 
      Line   modules/pwa_extras/pwa_extras.module
     ------ --------------------------------------------------------------------------------------
      32     Access to an undefined property DOMNode::$tagName.
             💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
     ------ --------------------------------------------------------------------------------------
    
     ------ -----------------------------------------------------------------------------------------------------------------
      Line   src/Controller/PWAController.php
     ------ -----------------------------------------------------------------------------------------------------------------
      222    Regex pattern is invalid: The /e modifier is no longer supported, use preg_replace_callback instead in pattern:
             #(/(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|"|'|:|<|$|\.\s)#ie
     ------ -----------------------------------------------------------------------------------------------------------------
    
     ------ -----------------------------------------------------------------------------------------
      Line   src/Form/ManifestConfigurationForm.php
     ------ -----------------------------------------------------------------------------------------
      405    Cannot call method basePath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
     ------ -----------------------------------------------------------------------------------------
    

    But they are not so easy to fix.

    Access to an undefined property DOMNode::$tagName
    I think this one is ignorable, as we hopefully made sure, that tagName is 100% defined at this point of code.

    Regex pattern is invalid
    This is quite unclear, as it recommends using "preg_replace_callback", but simply removing the "e" from "#ie" should already do the trick? If we used "preg_replace_callback", what would the callback function hold? Simply escaping the same characters "e" did before?

    When using the deprecated e modifier, this function escapes some characters (namely ', ", \ and NULL) in the strings that replace the backreferences

    This is unclear.

    Cannot call method basePath() on bool|Drupal\Core\StreamWrapper\StreamWrapperInterface.
    This is also unclear.

          // Save new image.
          $wrapper = $this->streamWrapperManager->getViaScheme(
            $this->config('system.file')->get('default_scheme')
          );
    

    $wrapper could be "FALSE" if no registered handler could be found. Should we throw an error, if it is false? Should we simply log the message and exit the submitForm there?
    Also unclear.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • @grevil opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany Grevil

    I created a follow-up issue concerning the regex, see 🐛 [META][pwa_service_worker] Refactor 'pwa_service_worker' implementations and flaws Needs work .

    The other fixes can be reviewed now!

  • Issue was unassigned.
  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Left some comments!

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany Grevil

    Please review!

  • Status changed to RTBC over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • Status changed to Fixed over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024