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

Some phpstan issues #2393

Merged
merged 24 commits into from
Mar 13, 2019
Merged

Some phpstan issues #2393

merged 24 commits into from
Mar 13, 2019

Conversation

belgattitude
Copy link
Contributor

Hey Grav Team

Would you be interested in adding phpstan ?

Here's a little example of setup and few fixes.

If you're not used to phpstan, have a look to the composer.json script section or run

$ composer phpstan

For now, I've just setup level 0 (it goes to 7, but there will be a lot of alerts then). I would be happy to help to fix some alerts... and help increase the level.

Let me know.

@belgattitude
Copy link
Contributor Author

An example:

 ------ -------------------------------------------------------------------------------------------------- 
  Line   Grav/Common/Assets/Traits/AssetUtilsTrait.php (in context of class Grav\Common\Assets\BaseAsset)  
 ------ -------------------------------------------------------------------------------------------------- 
  90     Call to an undefined method Grav\Common\Assets\BaseAsset::cssRewrite().                           
 ------ -------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------- 
  Line   Grav/Common/GPM/Remote/GravCore.php                                  
 ------ --------------------------------------------------------------------- 
  29     Grav\Common\GPM\Remote\GravCore::__construct() does not call parent  
         constructor from Grav\Common\GPM\Remote\AbstractPackageCollection.   
 ------ --------------------------------------------------------------------- 

 ------ ------------------------------------------------------- 
  Line   Grav/Common/Scheduler/Job.php                          
 ------ ------------------------------------------------------- 
  480    Call to static method sendEmail() on an unknown class  
         Grav\Plugin\Email\Utils.                               
 ------ ------------------------------------------------------- 

 ------ ---------------------------------------------------------------- 
  Line   Grav/Common/Service/AccountsServiceProvider.php                 
 ------ ---------------------------------------------------------------- 
  15     LogicException (User class was called too early!) thrown while  
         autoloading class Grav\Common\User\User.                        
  48     LogicException (User class was called too early!) thrown while  
         autoloading class Grav\Common\User\User.                        
  61     LogicException (User class was called too early!) thrown while  
         autoloading class Grav\Common\User\User.                        
 ------ ---------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------ 
  Line   Grav/Common/User/Group.php                                              
 ------ ------------------------------------------------------------------------ 
  115    Method Grav\Common\Data\Data::blueprints() invoked with 1 parameter, 0  
         required.                                                               
 ------ ------------------------------------------------------------------------ 

 ------ ---------------------------------------------------------------- 
  Line   Grav/Common/User/User.php                                       
 ------ ---------------------------------------------------------------- 
         LogicException (User class was called too early!) thrown while  
         autoloading class Grav\Common\User\User.                        
 ------ ---------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------- 
  Line   Grav/Console/ConsoleTrait.php (in context of class Grav\Console\ConsoleCommand)  
 ------ --------------------------------------------------------------------------------- 
  132    Access to an undefined property                                                  
         Grav\Console\ConsoleCommand::$local_config.                                      
 ------ --------------------------------------------------------------------------------- 

 ------ ------------------------------------------------ 
  Line   Grav/Framework/File/Formatter/CsvFormatter.php  
 ------ ------------------------------------------------ 
  47     Undefined variable: $lines                      
 ------ ------------------------------------------------ 

 ------ ----------------------------------------------------------------------------------------------------------- 
  Line   Grav/Framework/Flex/Traits/FlexAuthorizeTrait.php (in context of class Grav\Framework\Flex\FlexDirectory)  
 ------ ----------------------------------------------------------------------------------------------------------- 
  43     Call to an undefined method                                                                                
         Grav\Framework\Flex\FlexDirectory::exists().                                                               
 ------ ----------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------- 
  Line   Grav/Framework/Form/FormFlashFile.php  
 ------ --------------------------------------- 
  48     Call to an undefined static method     
         Grav\Framework\Psr7\Stream::create().  
 ------ --------------------------------------- 

Some of those problems are a little more complex and I could not find how to solve them easily:

  • Stream::create() family are solved PSR-17 (stream factories). Nyholm/psr7 implements them here.
  • FlexAuthorizeTrait.php as an example of an issue... Maybe create an abstract method exists(), so you're sure all classes using it, will have to implement it.
  • In general I don't really understand the need for PSR7 decorator and traits... You can inject the interface in the constructor (if null sets Nyholm/psr7 as default for example).
  • User.php and LogicException.... Can possibly break autoloading. This case can probably be solved by a factory and a Userinterface ?
    ....

Don't want to be sound negative ;) just some thoughts

@rhukster
Copy link
Member

rhukster commented Mar 2, 2019

I really like the idea of utilizing phpstan and resolving some of these issues reported. However, it might be something where we properly sort issues as part of Grav 1.7 (as 1.6 is so close to release). no real harm in adding into now though, and fixing simple things.

@belgattitude
Copy link
Contributor Author

belgattitude commented Mar 2, 2019

Looks good to me.

So for the config:

  • Moved the phpstan.neon out of public/base directory. No need to worry of updating .htaccess and apache/nginx configs.
  • Added a phpstan bootstrap to define GRAV_USER_INSTANCE prior to analysis
  • Added a phpstan composer script for level 0.
  • Added initial exclusions.

For the bugs:

  • Major: Invalid private level in FileCache::init().
  • Medium: Fixed parent::__construct() calling where applicable.
  • Medium: Fixed some invalid cases (notably Twig_SimpleFunction)
  • Minor: Some invalid typehints (phpdoc) or missing properties.

To go forward, I would like to know if it's possible to run coverage. I saw that c3 is not installed. I can enable but I'd like to be sure it does not end in public directory. Let me know if you already have already something (codecov...) ? Will feel more confident about what I can work on ;)

From there, I can help with:

 ------ ------------------------------------------------ 
  Line   Grav/Framework/File/Formatter/CsvFormatter.php  
 ------ ------------------------------------------------ 
  47     Undefined variable: $lines                      
 ------ ------------------------------------------------ 

and with PHP7.3

------ ------------------------------------------------------------------------------------------------------------------- 
  Line   Grav/Common/GPM/Remote/GravCore.php                                                                                
 ------ ------------------------------------------------------------------------------------------------------------------- 
  76     Regex pattern is invalid: Compilation failed: invalid range in character class at offset 3 in pattern: /[\w-\.]+/  
 ------ ------------------------------------------------------------------------------------------------------------------- 

The next issues will need to be tackled in separate P/R.

For that I suggest to merge this one in 1.6 as soon as you feel good with it. (to not be trapped in rebase nightmares)

@belgattitude
Copy link
Contributor Author

FYI, current report with PHP7.3

 ------ -------------------------------------------------------------------------------------------------- 
  Line   Grav/Common/Assets/Traits/AssetUtilsTrait.php (in context of class Grav\Common\Assets\BaseAsset)  
 ------ -------------------------------------------------------------------------------------------------- 
  90     Call to an undefined method Grav\Common\Assets\BaseAsset::cssRewrite().                           
 ------ -------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   Grav/Common/GPM/Remote/GravCore.php                                                                                
 ------ ------------------------------------------------------------------------------------------------------------------- 
  76     Regex pattern is invalid: Compilation failed: invalid range in character class at offset 3 in pattern: /[\w-\.]+/  
 ------ ------------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------- 
  Line   Grav/Common/User/Group.php                                                        
 ------ ---------------------------------------------------------------------------------- 
  115    Method Grav\Common\Data\Data::blueprints() invoked with 1 parameter, 0 required.  
 ------ ---------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------- 
  Line   Grav/Console/ConsoleTrait.php (in context of class Grav\Console\ConsoleCommand)  
 ------ --------------------------------------------------------------------------------- 
  132    Access to an undefined property Grav\Console\ConsoleCommand::$local_config.      
 ------ --------------------------------------------------------------------------------- 

 ------ ------------------------------------------------ 
  Line   Grav/Framework/File/Formatter/CsvFormatter.php  
 ------ ------------------------------------------------ 
  47     Undefined variable: $lines                      
 ------ ------------------------------------------------ 

 ------ ----------------------------------------------------------------------------------------------------------- 
  Line   Grav/Framework/Flex/Traits/FlexAuthorizeTrait.php (in context of class Grav\Framework\Flex\FlexDirectory)  
 ------ ----------------------------------------------------------------------------------------------------------- 
  43     Call to an undefined method Grav\Framework\Flex\FlexDirectory::exists().                                   
 ------ ----------------------------------------------------------------------------------------------------------- 

@belgattitude
Copy link
Contributor Author

belgattitude commented Mar 2, 2019

Also this one is a little tricky:

PHP Warning:  Declaration of Grav\Framework\Cache\Adapter\FileCache::init($namespace, $directory) should be compatible with Grav\Framework\Cache\AbstractCache::init($namespace = '', $defaultLifetime = NULL) in /web/www/grav/system/src/Grav/Framework/Cache/Adapter/FileCache.php

This one is a definitely a bug... better to make a separate P/R

@belgattitude
Copy link
Contributor Author

I think for now I'll wait for merge or comments ;)

Thanks a lot for your work.

@rhukster
Copy link
Member

This is looking really good so far! I'm going to test it a little today.

@mahagr mahagr merged commit ecd3942 into getgrav:1.6 Mar 13, 2019
@mahagr
Copy link
Member

mahagr commented Mar 13, 2019

Level 0 has only a single error left, @rhukster can take a look into it. :)

@mahagr
Copy link
Member

mahagr commented Mar 13, 2019

Level 1 issues sorted!

@belgattitude
Copy link
Contributor Author

I see you're super motivated ;) Nice !

@belgattitude belgattitude deleted the 1.6 branch March 13, 2019 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants