CI and Mega-Linter with PHPStan #9
llaville
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
A little enhancement, to allow both locally docker run and Github Action Workflow working in same way.Remember that the difference to location containing files to lint :
In Step 7, we have used hardcoded path (using DEFAULT_WORKSPACE). It's time now to use the expanding paths feature of PHPStan And contents become |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
After a quick enhancement guide about Mega-Linter with PHPCS, two days ago in llaville/php-compatinfo-db project ...
Today I'll present my solution to fix issue oxsecurity/megalinter#725
TL;DR (unless you really want it ;-)
Pre-requis
We are agree that PHPStan default rule level is zero as specified in user guide
We are agree that the default PHPStan config file is now called
TEMPLATES/phpstan.neon.distinstead of TEMPLATES/phpstan.neonCurrent version of Mega-Linter tested 4.47.0-dev since commit cf01fd2
List of potential issues we can found when using PHPStan linter
For demo, and follow examples, we will use the llaville/docker-php-toolbox repository.
Step 1: Install only source code with Git
git clone https://github.com/llaville/docker-php-toolbox.gitStep 2: Define a
.mega-linter.ymlconfig file with only PHP_PHPSTAN linter activeStep 3: Run Mega-Linter on source code
docker-php-toolboxfolder.You can see before analysis, that ML found 33 PHP files
That's TRUE, and we can verify this result with following commands:
That gave us :
And finally, more than 20 seconds later
It seems slow, and we have Errors detected (that is not true, as we can see soon)
Question: Why did we found errors.
Answer: Because PHPStan needs to be able to locate symbols. And since v0.12.26 it uses Discovering Symbols feature
In summary, that means we always HAVE TO install Composer Dependencies (if project have it), before running analysis with PHPStan.
Step 4: Install Composer Dependencies with command:
composer updateYou should have a
vendordirectory with 491 php files. Confirmed by command:So in total we have 524 php files in project.
Step 5: Run Mega-Linter analysis again without changing anything else
You can see before analysis, that ML found 524 PHP files
And finally, more than 5 minutes later
It seems slow, and we have Errors detected (that is not true, as we can see soon)
Step 6: First fix is to exclude Composer dependencies from analysis
Add in your
.mega-linter.ymlconfig file, following entry:NOTE I recommand also to exclude GIT config folder
EXCLUDED_DIRECTORIES: [".git", "vendor"]That is faster than previous, and but not yet correct, because some errors were detected
Step 7: Second fix is to set Discovering Symbols feature of PHPStan
Declare a custom PHPStan config file in your
.mega-linter.ymlWith contents:
at the root of source code.
We have raise up rule level from 0 to 6 (but it's not mandatory for demo, just for the project itself)
We have added
excludePaths, just in case you forgot to excludevendoras previously seen withAnd run Mega-Linter again, to finally see !
It's not really fast, but at least results are correct !
Remember how the linting is performed
Step 8: Third fix, if you want to improve perf, switch to
projectmodeIn your
.mega-linter.ymlconfig file, add:And specify in your
phpstan.neonconfig file, paths to scan:Run Mega-Linter once again, and look at final results.
Amazing :)
Hope you've enjoy this walkthrough
Beta Was this translation helpful? Give feedback.
All reactions