Writing better Drupal code with static analysis using PHPStan

PHPStan executed against the Address module, without the Drupal extension
PHPStan run against the state_machine module, with the Drupal extension
composer require mglaman/phpstan-drupal --dev
parameters:
# Ignore tests
excludes_analyse:
- *Test.php
- *TestBase.php
# PHPStan Level 1
level: 1
includes:
# Add the phpstan-drupal extension
- vendor/mglaman/phpstan-drupal/extension.neon
$this->extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
$this->extensionDiscovery->setProfileDirectories([]);
$profiles = $this->extensionDiscovery->scan('profile');
$profile_directories = array_map(function ($profile) {
return $profile->getPath();
}, $profiles);
$this->extensionDiscovery->setProfileDirectories($profile_directories);
$this->moduleData = $this->extensionDiscovery->scan('module');
$this->themeData = $this->extensionDiscovery->scan('theme');
foreach ($extensionDiscovery->scan('module') as $extension) {
$module_dir = $this->drupalRoot . '/' . $extension->getPath();
$moduleName = $extension->getName();
$servicesFileName = $module_dir . '/' . $moduleName . '.services.yml';
if (file_exists($servicesFileName)) {
$serviceYamls[$moduleName] = $servicesFileName;
}
$camelized = $this->camelize($extension->getName());
$name = "{$camelized}ServiceProvider";
$class = "Drupal\\{$moduleName}\\{$name}";

if (class_exists($class)) {
$serviceClassProviders[$moduleName] = $class;
}
}
drupal:
entityTypeStorageMapping:
node: Drupal\node\NodeStorage
taxonomy_term: Drupal\taxonomy\TermStorage
user: Drupal\user\UserStorage

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store