PHPStan, exclude all and specify files to check
Asked Answered
A

1

8

Im trying to setup PHPStan on a older, bigger, codebase. How could i exclude everything and than maybe by config define what to analyse.

I have thought about using a separate folder for it, but that would mean constantly moving files which might lead to breaking of the code. So i am hoping to exclude everything and then adding files to the analysers per file.

At this moment the only solution i was able to find is defining a script in composer.json

  "scripts": {
    "phpstan": "./vendor/bin/phpstan analyse --memory-limit=1G --no-progress --level 1 `cat phpstan_analyse_files`"
  }

And keeping a list of files to analyise in the file phpstan_analyse_files

Annotate answered 14/7, 2018 at 7:34 Comment(7)
You can make phpstan analyze individual files. php phpstan.phar analyse src/index.php src/blog.php so if you're using git, you can probably make a hood that triggers phpstan(or whatever tool you use phpstan with)Sri
Mauran, thats the only option i found as well, defining as params, but thats not a very sustainable solution. So what im thinking now is using php phpstan.phar analyse cat files_to_analyse. If you have any other solutions im all earsAnnotate
Actually i totally forgot. I don't think its a good idea to only analyse specific files and not the whole project. phpstan would probably say "Class not specified" when using classes thats not defined in new files etc.Sri
@MauranMuthiah This is not true. What directories do you analyze does not affect how classes are found. That's a concern of the autoloader and is unrelated.Anatolio
@Michal-sk Basically you're doing it right. Combining multiple simpler tools (phpstan, cat) to achieve a complex outcome is the way to go ;)Anatolio
@OndřejMirtes , guess i couldn't get a more confirming answer than this ;) Thanks for all your work and time!Annotate
You can also create a baseline phpstan.org/user-guide/baseline which ignores all current issues found. So any new code you add to your older codebase will be checked. This will allow you to scan your entire project.Glabrescent
A
27

The best way to do what you need is excludePaths section as of PHPStan 1.0:

# phpstan.neon
parameters:
    excludePaths:
        - 'old-code/OldClass.php'
        - 'another-old-code/*'

See docs or this real project phpstan.neon setup for inspiration.

Aspersorium answered 12/9, 2018 at 21:5 Comment(3)
Thank you for your response. But i think the best way is to just stick with what we got now regarding keep an file with what we do want to scan and cat that into the call.Annotate
Note: Using configuration file /var/www/phpstan.neon. Configuration parameters excludes_analyse and excludePaths cannot be used at the same time. Parameter excludes_analyse has been deprecated so use excludePaths only from now on.Reproduction
@Reproduction Thank you, I've updatd the answer to match new PHPStan config!Aspersorium

© 2022 - 2024 — McMap. All rights reserved.