Skip to content

Introduce phpstan#31810

Merged
PVince81 merged 15 commits intomasterfrom
add-phpstan
Oct 19, 2018
Merged

Introduce phpstan#31810
PVince81 merged 15 commits intomasterfrom
add-phpstan

Conversation

@patrickjahns
Copy link
Copy Markdown
Contributor

Description

Introduce https://github.com/phpstan/phpstan into owncloud/core

The phpstan.neon configuration is a very minimal configuration to get it running and prevent some of the problems/technical debt related to templates / routes.php etc.

We currently use level 0 - which is the most lenient level - in this level, we already have ~ 400 errors/violations which need to be gradually adressed.

Some of them are related to Migrations and autoloading

General noticed the issue with autoloading - right now our internal autoloader requires ownCloud to be properly installed (and apps enabled) to be able to provide the classes for phpstan.

I would consider this also quite some technical debt which at some point would need some ❤️ . If possible at some point I see it feasible to move towards composer as being the only autoloader, and our class being registered to composer as authoritative autoloader.

Motivation and Context

  • Improve code quality
  • Prevent bugs that can sneak

How Has This Been Tested?

🤖

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • improve code quality

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@patrickjahns
Copy link
Copy Markdown
Contributor Author

patrickjahns commented Jun 17, 2018

@DeepDiver1975 @PVince81

CI shows in https://drone.owncloud.com/owncloud/core/7982/69

 [ERROR] Found 313 errors     

errors regarding autoloading in migrations need some closer view if we skip that in phpstan - include it otherwise

 ------ ------------------------------------------------------------------------- 
  Line   core/Migrations/Version20171026130750.php                                
 ------ ------------------------------------------------------------------------- 
         Class OC\Migrations\Version20171026130750 was not found while trying to  
         analyse it - autoloading is probably not configured properly.            
 ------ ------------------------------------------------------------------------- 

Also autoloading related to apps/files_external/3rdparty + seems to be screwed up - I wonder if we need to set information on core to have this working.

Alternative approach would be - to point to every directory relevant for autoloading directly ( but from current experimentation I find it quite knowledgeable to see if our autoloader works properly )

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 17, 2018

Codecov Report

Merging #31810 into master will increase coverage by 0.02%.
The diff coverage is 79.34%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #31810      +/-   ##
============================================
+ Coverage        64%   64.03%   +0.02%     
+ Complexity    18190    18187       -3     
============================================
  Files          1178     1178              
  Lines         68726    68697      -29     
  Branches       1271     1271              
============================================
- Hits          43990    43987       -3     
+ Misses        24366    24340      -26     
  Partials        370      370
Flag Coverage Δ Complexity Δ
#javascript 52.88% <ø> (ø) 0 <ø> (ø) ⬇️
#phpunit 65.33% <79.34%> (+0.02%) 18187 <35> (-3) ⬇️
Impacted Files Coverage Δ Complexity Δ
apps/dav/lib/Upload/AssemblyStreamZsync.php 88.37% <ø> (ø) 33 <0> (ø) ⬇️
lib/private/legacy/util.php 68.19% <ø> (+0.29%) 240 <0> (-2) ⬇️
lib/private/Session/Session.php 100% <ø> (ø) 5 <0> (ø) ⬇️
lib/private/Files/Storage/Common.php 85.15% <ø> (ø) 141 <0> (ø) ⬇️
lib/private/Share/Share.php 13.26% <ø> (ø) 70 <0> (ø) ⬇️
lib/private/Session/CryptoWrapper.php 47.05% <ø> (ø) 6 <0> (ø) ⬇️
core/Command/Maintenance/DataFingerprint.php 100% <ø> (ø) 4 <0> (ø) ⬇️
lib/private/Files/Node/Folder.php 96.09% <ø> (-0.04%) 44 <0> (ø)
lib/private/Files/Cache/Wrapper/CacheWrapper.php 89.06% <ø> (+2.69%) 29 <0> (-1) ⬇️
apps/systemtags/appinfo/app.php 14.63% <ø> (ø) 0 <0> (ø) ⬇️
... and 73 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a698db1...11d2b3e. Read the comment docs.

@phil-davis
Copy link
Copy Markdown
Contributor

Note for when this gets to the "backport to stable10" stage:
It will be nice to get the "owncloud coding standard" changes backport merged to stable10 first.
Then changes in this PR are much more likely to backport without conflict.

@DeepDiver1975
Copy link
Copy Markdown
Member

errors regarding autoloading in migrations need some closer view if we skip that in phpstan - include it otherwise

migrations are never loaded using the autoloader but purly on basis of requiring the explicit file.
So we can easily savely ignore autoloading errors on migrations.

@ondrejmirtes
Copy link
Copy Markdown

Hi, author of PHPStan here 😊

migrations are never loaded using the autoloader but purly on basis of requiring the explicit file.
So we can easily savely ignore autoloading errors on migrations.

Unfortunately, these kinds of errors are not permitted to be ignored (it led developers to think their configuration is fine, but it isn't), basically I won't let you shoot yourself in the foot 😊 Instead, you should exclude the directory from the analysis (or autoload those files in autoload-dev or with a custom autoloader).

@patrickjahns patrickjahns force-pushed the add-phpstan branch 2 times, most recently from 4396395 to 9602944 Compare June 22, 2018 12:21
@patrickjahns
Copy link
Copy Markdown
Contributor Author

After merging phan branch - we are now down to [ERROR] Found 302 errors

Need to schedule some more time to move this forward.

@ondrejmirtes
Thanks for sharing - quick question - would you rather recommend to temporarily include them via phpstan directly?

Also do you recommend for a large project like this, to use our (legacy) application bootstrapping file - or use autoloading capabilities for stan.

(On a sidenote - absolutely love phpstan 😍 )

@ondrejmirtes
Copy link
Copy Markdown

@patrickjahns One option I forgot to tell you about - yes, you can use autoload_files or autoload_directories PHPStan option to do this, it's absolutely fine - just be careful about side effects. If there's something else beside a class definition in an autoloaded file, it will get executed! (I need to tackle this in some future version.)

Thank you for your kind words 😊

@ondrejmirtes
Copy link
Copy Markdown

You have several options for autoloading and they are equal:

  • Built-in Composer autoloader - the easiest option, works out of the box
  • Add some files and directories using autoload_files and autoload_directories
  • Use a completely custom autoloader registered in PHPStan's bootstrap (config option) or --autoload-file (CLI option).

@patrickjahns
Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 @PVince81 - are we still using jcrop in the frontend?

It ships with some demo php code - which is not required and leads to static code analyzer failures i.e.

 ------ -------------------------------------------------------------- 
  Line   core/vendor/jcrop/demos/crop.php                              
 ------ -------------------------------------------------------------- 
  17     Call to function imagecreatetruecolor() with incorrect case:  
         ImageCreateTrueColor                                          
 ------ -------------------------------------------------------------- 

 ------ -------------------------------------------------------------- 
  Line   core/vendor/Jcrop/demos/crop.php                              
 ------ -------------------------------------------------------------- 
  17     Call to function imagecreatetruecolor() with incorrect case:  
         ImageCreateTrueColor                                          
 ------ -------------------------------------------------------------- 

We can ignore the core/vendor path - but all of the files/libraries in there seem to be very old?

@patrickjahns patrickjahns force-pushed the add-phpstan branch 2 times, most recently from d947ea5 to b05e8c0 Compare June 22, 2018 20:38
@patrickjahns
Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 @PVince81
I don't understand - why does a core entity use the AppFramework:

 ------ --------------------------------------------------- 
  Line   lib/private/Authentication/Token/DefaultToken.php  
 ------ --------------------------------------------------- 
  94     Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::getLoginName().        
  103    Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::getPassword().         
  122    Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::getLastCheck().        
  131    Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::setLastCheck().        
  141    Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::setName().             
  151    Call to an undefined static method                 
         OCP\AppFramework\Db\Entity::setLoginName().        
 ------ --------------------------------------------------- 

@PVince81
Copy link
Copy Markdown
Contributor

PVince81 commented Aug 8, 2018

@patrickjahns probaby because the app framework provides a DB layer based on entities which can be convenient in some cases. At some point there was the thought of using DB entities everywhere but we never got to plan porting everything over.

@patrickjahns
Copy link
Copy Markdown
Contributor Author

That just seems like a mess all over again - I am happy to introduce static code analysis all over the place to uncover and prevent further things ;-)

@patrickjahns patrickjahns mentioned this pull request Aug 30, 2018
4 tasks
@DeepDiver1975 DeepDiver1975 force-pushed the add-phpstan branch 2 times, most recently from 4be6974 to 3af5fcd Compare October 18, 2018 11:28
@DeepDiver1975
Copy link
Copy Markdown
Member

These errors are fixed in phpstan master branch

 ------ -------------------------------------------------------------- 
  Line   apps/dav/appinfo/v1/caldav.php                                
 ------ -------------------------------------------------------------- 
         Internal error: Call to undefined method                      
         PhpParser\Node\Scalar\String_::parseDocString()               
         Run PHPStan with --debug option and post the stack trace to:  
         https://github.com/phpstan/phpstan/issues/new                 
 ------ -------------------------------------------------------------- 

 ------ -------------------------------------------------------------- 
  Line   apps/dav/appinfo/v1/carddav.php                               
 ------ -------------------------------------------------------------- 
         Internal error: Call to undefined method                      
         PhpParser\Node\Scalar\String_::parseDocString()               
         Run PHPStan with --debug option and post the stack trace to:  
         https://github.com/phpstan/phpstan/issues/new                 
 ------ -------------------------------------------------------------- 

 ------ -------------------------------------------------------------- 
  Line   apps/dav/lib/Server.php                                       
 ------ -------------------------------------------------------------- 
         Internal error: Call to undefined method                      
         PhpParser\Node\Scalar\String_::parseDocString()               
         Run PHPStan with --debug option and post the stack trace to:  
         https://github.com/phpstan/phpstan/issues/new                 
------ -------------------------------------------------------------- 

@DeepDiver1975 DeepDiver1975 changed the title [WIP] Introduce phpstan Introduce phpstan Oct 18, 2018
@DeepDiver1975 DeepDiver1975 self-assigned this Oct 19, 2018
Copy link
Copy Markdown
Contributor

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@PVince81 PVince81 merged commit 11d30f0 into master Oct 19, 2018
@PVince81 PVince81 deleted the add-phpstan branch October 19, 2018 20:28
@phil-davis
Copy link
Copy Markdown
Contributor

Note: this might be reasonable to do in stable10 with or after drop PHP 7.0 (maybe it is not strictly dependent on that, but anyway)
I noticed this when preparing some "drop PHP7.0" stuff and I also applied some drone changes that ran phpstan on stable10

@phil-davis
Copy link
Copy Markdown
Contributor

phil-davis commented Jul 7, 2019

backport stable10 is in #35774

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.

6 participants