Fix PHPStan errors

Created on 13 November 2023, 8 months ago
Updated 19 December 2023, 6 months ago

Problem/Motivation

The PHPStan static analyzer helps developers find coding errors by checking parameter types and functions. The biggest issue is:
Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.. This issue will also be identified by users of the module, leading to spurious errors being reported by PHPStan.

Steps to reproduce

Install phpstan-drupal in a site with:
composer require --dev mglaman/phpstan-drupal

Run PHPStan's analyze command against the module:

vendor/bin/phpstan analyze --level 5 web/modules/contrib/telephone_formatter/

 5/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ---------------------------------------------------------------------------------------------------------- 
  Line   src/Formatter.php                                                                                         
 ------ ---------------------------------------------------------------------------------------------------------- 
  51     Parameter #2 $numberFormat of method libphonenumber\PhoneNumberUtil::format() expects int, string given.  
 ------ ---------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   src/Plugin/Field/FieldFormatter/TelephoneFormatter.php                                                             
 ------ ------------------------------------------------------------------------------------------------------------------- 
  77     Unsafe usage of new static().                                                                                      
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static                                  
  184    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.                                      
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                               
  222    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.                                      
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                               
  222    Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  250    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.                                      
         💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property                               
 ------ ------------------------------------------------------------------------------------------------------------------- 

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/src/Functional/FieldFormatterTest.php                                                                                                                                                                            
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  15     Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.                                                                
  22     PHPDoc type array of property Drupal\Tests\telephone_formatter\Functional\FieldFormatterTest::$modules is not covariant with PHPDoc type array<string> of overridden property Drupal\Tests\BrowserTestBase::$modules.  
         💡 You can fix 3rd party PHPDoc types with stub files:                                                                                                                                                                 
         💡 https://phpstan.org/user-guide/stub-files                                                                                                                                                                           
                                                                                                                                                                                                                                
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   tests/src/Unit/FormatterTest.php                                                                                   
 ------ ------------------------------------------------------------------------------------------------------------------- 
  51     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  52     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  53     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  54     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  64     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
  75     Parameter #2 $format of method Drupal\telephone_formatter\FormatterInterface::format() expects string, int given.  
 ------ ------------------------------------------------------------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 14 errors                                                                                                                                               

Proposed resolution

Change the argument type of Drupal\telephone_formatter\FormatterInterface::format()'s second parameter to int.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States adamfranco

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

Comments & Activities

  • Issue created by @adamfranco
  • 🇺🇸United States adamfranco

    Attached is a patch to fix the argument type for $format parameter to match that used by libphonenumber\PhoneNumberFormat (string --> int).

    After this patch PHPStan reports only more minor errors:

    vendor/bin/phpstan analyze --level 5 ../drupal-other/telephone_formatter/
     5/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
    
     ------ -----------------------------------------------------------------------------------------
      Line   drupal-other/telephone_formatter/src/Plugin/Field/FieldFormatter/TelephoneFormatter.php
     ------ -----------------------------------------------------------------------------------------
      77     Unsafe usage of new static().
             💡 See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
      184    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.
             💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
      222    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.
             💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
      250    Access to an undefined property Drupal\Core\Field\FieldItemInterface::$value.
             💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
     ------ -----------------------------------------------------------------------------------------
    
     ------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Line   drupal-other/telephone_formatter/tests/src/Functional/FieldFormatterTest.php
     ------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      15     Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.
      22     PHPDoc type array of property Drupal\Tests\telephone_formatter\Functional\FieldFormatterTest::$modules is not covariant with PHPDoc type array<string> of overridden property Drupal\Tests\BrowserTestBase::$modules.
             💡 You can fix 3rd party PHPDoc types with stub files:
             💡 https://phpstan.org/user-guide/stub-files
    
     ------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
                                                                                                                            
     [ERROR] Found 6 errors                                                                                                 
    
  • First commit to issue fork.
  • Status changed to Needs review 6 months ago
  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 6 months ago
    Waiting for branch to pass
  • 🇮🇳India viren18febS

    Hi @adamfranco
    I have fixed the remaining issues and added a patch . please review
    Thanks

Production build 0.69.0 2024