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

Michal
  • 1,010
  • 2
  • 8
  • 18
  • 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) – Mauran Muthiah Jul 14 '18 at 07:40
  • 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 ears – Michal Jul 14 '18 at 07:46
  • 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. – Mauran Muthiah Jul 14 '18 at 09:00
  • @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. – Ondřej Mirtes Jul 18 '18 at 16:22
  • @Michal-sk Basically you're doing it right. Combining multiple simpler tools (`phpstan`, `cat`) to achieve a complex outcome is the way to go ;) – Ondřej Mirtes Jul 18 '18 at 16:23
  • @OndřejMirtes , guess i couldn't get a more confirming answer than this ;) Thanks for all your work and time! – Michal Jul 18 '18 at 20:47
  • You can also create a baseline https://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. – R. Chappell Feb 15 '22 at 09:11

1 Answers1

25

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.

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
  • 1
    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. – Michal Sep 14 '18 at 11:31
  • 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. – thejimbirch Nov 15 '21 at 20:05
  • @thejimbirch Thank you, I've updatd the answer to match new PHPStan config! – Tomas Votruba Nov 15 '21 at 22:21