Skip to content

Commit 3aca30d

Browse files
eiriksmmglaman
authored andcommitted
Cache scan results. Make new rule a service
1 parent caf2f83 commit 3aca30d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

extension.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ parametersSchema:
2323
entityTypeStorageMapping: arrayOf(string())
2424
])
2525
rules:
26-
- PHPStan\Rules\Drupal\LoadIncludes
2726
- PHPStan\Rules\Classes\PluginManagerInspectionRule
2827
- PHPStan\Rules\Drupal\Coder\DiscouragedFunctionsRule
2928
- PHPStan\Rules\Drupal\GlobalDrupalDependencyInjectionRule
@@ -44,3 +43,8 @@ services:
4443
-
4544
class: PHPStan\Reflection\EntityFieldsViaMagicReflectionExtension
4645
tags: [phpstan.broker.propertiesClassReflectionExtension]
46+
-
47+
class: PHPStan\Rules\Drupal\LoadIncludes
48+
tags: [phpstan.rules.rule]
49+
arguments:
50+
- %drupal.drupal_root%

src/Drupal/ExtensionDiscovery.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ public function __construct($root)
118118
*/
119119
public function scan($type)
120120
{
121+
static $scanresult;
122+
if (!$scanresult) {
123+
$scanresult = [];
124+
}
125+
126+
if (isset($scanresult[$type])) {
127+
return $scanresult[$type];
128+
}
129+
121130
$searchdirs = [];
122131
// Search the core directory.
123132
$searchdirs[static::ORIGIN_CORE] = 'core';
@@ -152,7 +161,8 @@ public function scan($type)
152161
$files = $this->sort($files, $origin_weights);
153162

154163
// Process and return the list of extensions keyed by extension name.
155-
return $this->process($files);
164+
$scanresult[$type] = $this->process($files);
165+
return $scanresult[$type];
156166
}
157167

158168
/**

src/Rules/Drupal/LoadIncludes.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414

1515
class LoadIncludes implements Rule
1616
{
17+
18+
/**
19+
* The project root.
20+
*
21+
* @var string
22+
*/
23+
protected $projectRoot;
24+
25+
/**
26+
* LoadIncludes constructor.
27+
*/
28+
public function __construct($project_root)
29+
{
30+
$this->projectRoot = $project_root;
31+
}
32+
1733
public function getNodeType(): string
1834
{
1935
return Node\Expr\MethodCall::class;
@@ -46,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array
4662
}
4763
// Try to invoke it similarily as the module handler itself.
4864
$finder = new DrupalFinder();
49-
$finder->locateRoot(dirname($GLOBALS['autoloaderInWorkingDirectory']));
65+
$finder->locateRoot($this->projectRoot);
5066
$drupal_root = $finder->getDrupalRoot();
5167
$extensionDiscovery = new ExtensionDiscovery($drupal_root);
5268
$modules = $extensionDiscovery->scan('module');

0 commit comments

Comments
 (0)