|
2 | 2 |
|
3 | 3 | namespace mglaman\PHPStanDrupal\Drush\Commands;
|
4 | 4 |
|
| 5 | +use DrupalFinder\DrupalFinder; |
5 | 6 | use Drush\Commands\DrushCommands;
|
6 | 7 |
|
7 | 8 | final class PhpstanDrupalDrushCommands extends DrushCommands
|
8 | 9 | {
|
9 | 10 |
|
10 | 11 | /**
|
11 |
| - * @command phpstan-drupal:setup |
12 |
| - * @option file |
| 12 | + * Creates configuration for PHPStan based on your Drupal site. |
| 13 | + * |
| 14 | + * @command phpstan:setup |
| 15 | + * @option drupal_root The path to Drupal. |
| 16 | + * @option file The output file. |
13 | 17 | * @bootstrap full
|
14 | 18 | *
|
15 |
| - * @param array{file: string} $options |
| 19 | + * @phpstan-param array{drupal_root: string, file: string} $options |
16 | 20 | */
|
17 |
| - public function setup(array $options = ['file' => '']): void |
| 21 | + public function setup(array $options = ['drupal_root' => '', 'file' => '']): void |
18 | 22 | {
|
| 23 | + $finder = new DrupalFinder(); |
| 24 | + |
| 25 | + if ($options['drupal_root'] === '') { |
| 26 | + $options['drupal_root'] = getcwd(); |
| 27 | + } |
| 28 | + |
| 29 | + $finder->locateRoot($options['drupal_root']); |
| 30 | + if (!$finder->locateRoot($options['drupal_root'])) { |
| 31 | + throw new \RuntimeException('Unable to detect Drupal at ' . $options['drupal_root']); |
| 32 | + } |
| 33 | + |
| 34 | + $drupalRoot = str_replace($finder->getComposerRoot() . DIRECTORY_SEPARATOR, '', $finder->getDrupalRoot()); |
| 35 | + |
19 | 36 | $parameters = [
|
20 | 37 | 'level' => 2,
|
21 | 38 | 'paths' => [
|
22 |
| - // @todo use drupal-finder for docroot |
23 |
| - 'web/modules/custom', |
24 |
| - 'web/themes/custom', |
25 |
| - 'web/profiles/custom', |
| 39 | + "$drupalRoot/modules/custom", |
| 40 | + "$drupalRoot/themes/custom", |
| 41 | + "$drupalRoot/profiles/custom", |
26 | 42 | ],
|
27 | 43 | 'drupal' => [
|
| 44 | + 'drupal_root' => $drupalRoot, |
28 | 45 | // @todo can we have this override _everything_ phpstan-drupal provides? or is it a merge.
|
29 | 46 | 'entityMapping' => [
|
30 | 47 | ],
|
31 | 48 | ],
|
32 |
| - |
33 | 49 | ];
|
34 | 50 |
|
35 | 51 | $entity_type_manager = \Drupal::entityTypeManager();
|
|
0 commit comments