Skip to content

Commit f285c02

Browse files
committed
use DrupalFinder for drupal_root and proper paths
1 parent be2f165 commit f285c02

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/Drush/Commands/PhpstanDrupalDrushCommands.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@
22

33
namespace mglaman\PHPStanDrupal\Drush\Commands;
44

5+
use DrupalFinder\DrupalFinder;
56
use Drush\Commands\DrushCommands;
67

78
final class PhpstanDrupalDrushCommands extends DrushCommands
89
{
910

1011
/**
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.
1317
* @bootstrap full
1418
*
15-
* @param array{file: string} $options
19+
* @phpstan-param array{drupal_root: string, file: string} $options
1620
*/
17-
public function setup(array $options = ['file' => '']): void
21+
public function setup(array $options = ['drupal_root' => '', 'file' => '']): void
1822
{
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+
1936
$parameters = [
2037
'level' => 2,
2138
'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",
2642
],
2743
'drupal' => [
44+
'drupal_root' => $drupalRoot,
2845
// @todo can we have this override _everything_ phpstan-drupal provides? or is it a merge.
2946
'entityMapping' => [
3047
],
3148
],
32-
3349
];
3450

3551
$entity_type_manager = \Drupal::entityTypeManager();

tests/src/Drush/Commands/PhpstanDrupalDrushCommandsTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public function testCreateNeon(): void
4343
self::assertEquals('parameters:
4444
level: 2
4545
paths:
46-
- web/modules/custom
47-
- web/themes/custom
48-
- web/profiles/custom
46+
- tests/fixtures/drupal/modules/custom
47+
- tests/fixtures/drupal/themes/custom
48+
- tests/fixtures/drupal/profiles/custom
4949
drupal:
50+
drupal_root: tests/fixtures/drupal
5051
entityMapping:
5152
node:
5253
class: Drupal\node\Entity\Node

0 commit comments

Comments
 (0)