Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\DrupalKernel;
$autoload = require __DIR__ . '/vendor/autoload.php';
$cwd = getcwd();
chdir('docroot');
$drupalKernel = DrupalKernel::createFromRequest(
Request::createFromGlobals(),
$autoload,
'dev',
true,
__DIR__
);
$drupalKernel->boot();
$drupalKernel->getContainer()->get('module_handler')->loadAll();
chdir($cwd);
return $autoload;
parameters:
bootstrap: ./drupal_phpstan_autoload.php
ignoreErrors:
- '#Function pager_.*#'
- ... ignore some other errors here, not _all_ functions are registered in the bootstrap
@Spudley

This comment has been minimized.

Copy link

@Spudley Spudley commented Aug 30, 2018

Thank you for this. It's a good starting point.

I'm trying to use it to run phpstan against our custom drupal modules, adapted for our folder paths. My commandline looks like this:

vendor/bin/phpstan analyse -c build/phpstan.neon web/modules/custom --level=4

However, I'm still getting errors on most (but not all?) of my custom module classes telling me that <classname> was not found while trying to analyse it - autoloading is probably not configured properly. .

This bootstrap does set up autoloading, but I looked at the autoloader and it doesn't seem to know about the modules folder. Do I need to add something else?

@Spudley

This comment has been minimized.

Copy link

@Spudley Spudley commented Aug 30, 2018

I resolved the above by adding:

    autoload_directories:
        - %rootDir%/../../../web/modules/custom

to the parameters in phpstan.neon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.