Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Extension for [PHPStan](https://phpstan.org/) to allow analysis of Drupal code.

PHPStan is able to [discover symbols](https://phpstan.org/user-guide/discovering-symbols) by using autoloading provided
by Composer. However, Drupal does not provide autoloading information for modules and themes. This project registers
those namespaces so that PHPStan can properly discover symbols in your Drupal code base automatically.
those namespaces so that PHPStan can properly discover symbols in your Drupal code base automatically. It also
registers Drush's Symfony compatibility classes, such as `DrushStyle`.

> [!NOTE]
> With Drupal 11.2, Drupal core is now using PHPStan 2.0. The 1.x branch of phpstan-drupal will be supported until
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ parameters:
- tests/src/DeprecatedScope/data/*.php
dynamicConstantNames:
- Drupal::VERSION
- Symfony\Component\HttpKernel\Kernel::MAJOR_VERSION
includes:
- phpstan-baseline.neon
8 changes: 8 additions & 0 deletions src/Drupal/DrupalAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Yaml\Yaml;
use Throwable;
use function array_map;
Expand Down Expand Up @@ -198,6 +199,13 @@ public function register(Container $container): void
foreach (Finder::create()->files()->name('*.inc')->in($drushDir . '/includes') as $file) {
require_once $file->getPathname();
}
// Some Drush versions map classes to a Symfony-version
// specific directory at runtime, mirror its preflight.
// @see \Drush\Preflight\Preflight::loadSymfonyCompatabilityAutoloader()
$compatibilityDir = $drushDir . '/src-symfony-compatibility/' . (Kernel::MAJOR_VERSION < 6 ? 'v4' : 'v6');
if (is_dir($compatibilityDir)) {
$this->autoloader->addPsr4('Drush\\', $compatibilityDir);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/src/DrushIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public function testPaths(): void
], []);
}

public function testSymfonyCompatibilityClasses(): void
{
$reflect = new \ReflectionClass(\Drush\Drush::class);
$fileName = $reflect->getFileName();
self::assertNotFalse($fileName);
if (!is_dir(dirname($fileName, 2) . '/src-symfony-compatibility')) {
self::markTestSkipped('Drush does not ship Symfony compatibility classes.');
}
self::assertTrue($this->createReflectionProvider()->hasClass('Drush\\Symfony\\LessStrictArgvInput'));
}

}
Loading