$ composer require phpstan/phpstan
Drupal dynamically adds to the class autoloader at runtime, so it is necessary to bootstrap Drupal in order that it is fully populated. Note that this may require that the database be accessible (i.e. may be problematic when working with Docker). This is why we use a custom autoload file below.
$ ./bin/phpstan analyse --level=7 -c phpstan.neon docroot/modules/custom