Skip to content

Instantly share code, notes, and snippets.

@zviryatko
Last active December 3, 2018 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zviryatko/505428ecb085a6f541f83137d208b560 to your computer and use it in GitHub Desktop.
Save zviryatko/505428ecb085a6f541f83137d208b560 to your computer and use it in GitHub Desktop.
phpstan for Drupal 8

Installation

  1. composer require --dev phpstan/phpstan
  2. Add composer patch https://github.com/phpstan/phpstan/pull/1080.patch
  3. Put this files to your project in phpstan directory
  4. Run it: ./bin/phpstan analyse -c ./phpstan/phpstan.neon -l max docroot/modules/custom/
<?php
/**
* @file
* Improve phpstan's excludes_analyse parameter to support file masks.
*/
/** @var \Nette\DI\Container $container */
$excludes = [];
foreach ($container->parameters['excludes_analyse'] as $exclude) {
if (strpos($exclude, '*') === FALSE) {
continue;
}
$excludes = array_merge($excludes, glob($exclude));
}
$container->parameters['excludes_analyse'] = array_merge($container->parameters['excludes_analyse'], array_unique($excludes));
<?php
/**
* @file
* Bootstrap drupal to allow autoload from modules src directory.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
foreach (spl_autoload_functions() as list($autoloader)) {
if ($autoloader instanceof \Composer\Autoload\ClassLoader) {
break;
}
}
$request = Request::createFromGlobals();
chdir(dirname(__DIR__) . '/docroot');
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE, dirname(__DIR__) . '/docroot');
new Settings(NestedArray::mergeDeepArray([
Settings::getAll(),
[
'bootstrap_config_storage' => function () {
return new FileStorage(config_get_config_directory(CONFIG_SYNC_DIRECTORY));
},
'container_yamls' => [DRUPAL_ROOT . '/sites/development.services.yml'],
'cache' => [
'bins' => [
'bootstrap' => 'cache.backend.null',
'config' => 'cache.backend.null',
'render' => 'cache.backend.null',
'discovery' => 'cache.backend.null',
'default' => 'cache.backend.null',
'dynamic_page_cache' => 'cache.backend.null',
],
],
]
]));
$kernel->invalidateContainer();
$kernel->boot();
$kernel->preHandle($request);
chdir(dirname(__DIR__));
parameters:
bootstrap: %currentWorkingDirectory%/phpstan/bootstrap.php
fileExtensions:
- php
- module
- inc
autoload_directories:
- %currentWorkingDirectory%/vendor
autoload_files:
- %currentWorkingDirectory%/phpstan/autoload-fix.php
ignoreErrors:
- '#Function [a-zA-Z0-9\\_]+ not found.#'
excludes_analyse:
- %currentWorkingDirectory%/docroot/docroot/core/tests
- %currentWorkingDirectory%/docroot/modules/contrib/*/tests/*
- %currentWorkingDirectory%/docroot/modules/contrib/*/src/Tests/*
- %currentWorkingDirectory%/docroot/modules/contrib/**/test/*
- %currentWorkingDirectory%/vendor/*/*/scenarios
- %currentWorkingDirectory%/vendor/*/*/examples/*
- %currentWorkingDirectory%/vendor/*/*/tests/*
- %currentWorkingDirectory%/vendor/*/*/Tests/*
- %currentWorkingDirectory%/vendor/squizlabs/php_codesniffer
- %currentWorkingDirectory%/vendor/drupal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment