Skip to content

Commit 21b6249

Browse files
authored
Ignore LoadIncludes rule if module and file name unknown (#524)
1 parent 46786f8 commit 21b6249

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

src/Rules/Drupal/LoadIncludeBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ protected function parseLoadIncludeArgs(Node\Arg $module, Node\Arg $type, ?Node\
3434
{
3535
$moduleName = $this->getStringArgValue($module->value, $scope);
3636
if ($moduleName === null) {
37-
return [];
37+
return [false, false];
3838
}
3939
$fileType = $this->getStringArgValue($type->value, $scope);
4040
if ($fileType === null) {
41-
return [];
41+
return [false, false];
4242
}
4343
$baseName = null;
4444
if ($name !== null) {

src/Rules/Drupal/LoadIncludes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public function processNode(Node $node, Scope $scope): array
4848
try {
4949
// Try to invoke it similarly as the module handler itself.
5050
[$moduleName, $filename] = $this->parseLoadIncludeArgs($args[0], $args[1], $args[2] ?? null, $scope);
51+
if (!$moduleName && !$filename) {
52+
// Couldn't determine module- nor file-name, most probably
53+
// because it's a variable. Nothing to load, bail now.
54+
return [];
55+
}
5156
$module = $this->extensionMap->getModule($moduleName);
5257
if ($module === null) {
5358
return [

tests/src/Rules/LoadIncludesRuleTest.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,39 @@ protected function getRule(): \PHPStan\Rules\Rule
1212
return self::getContainer()->getByType(LoadIncludes::class);
1313
}
1414

15-
public function testRule(): void
15+
/**
16+
* @dataProvider cases
17+
*/
18+
public function test(array $files, array $errors): void
1619
{
17-
$this->analyse([
18-
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module'
19-
],
20+
$this->analyse($files, $errors);
21+
}
22+
23+
public function cases(): \Generator
24+
{
25+
yield [
26+
[
27+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module'
28+
],
2029
[
2130
[
2231
'File modules/phpstan_fixtures/phpstan_fixtures.fetch.inc could not be loaded from Drupal\Core\Extension\ModuleHandlerInterface::loadInclude',
2332
30
2433
]
25-
]);
26-
}
34+
],
35+
];
2736

28-
public function testFormStateLoadInclude(): void
29-
{
30-
$this->analyse([
31-
__DIR__ . '/../../fixtures/drupal/core/tests/Drupal/Tests/Core/Form/FormStateTest.php'
32-
],
37+
yield [
3338
[
34-
]);
39+
__DIR__ . '/../../fixtures/drupal/core/tests/Drupal/Tests/Core/Form/FormStateTest.php'
40+
],
41+
[],
42+
];
43+
44+
yield 'bug-516.php' => [
45+
[__DIR__.'/data/bug-516.php'],
46+
[]
47+
];
3548
}
3649

3750
}

tests/src/Rules/data/bug-516.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Bug516;
4+
5+
use Drupal\Core\Extension\ModuleHandlerInterface;
6+
7+
/**
8+
* Tests moduleHandler::loadInclude where first argument is a variable.
9+
*/
10+
class TestClass {
11+
12+
/**
13+
* Code snippet from \Drupal\TestTools\Extension\SchemaInspector.
14+
*
15+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $handler
16+
* @param string $module
17+
*/
18+
function bug516(ModuleHandlerInterface $handler, string $module): void
19+
{
20+
$handler->loadInclude($module, 'install');
21+
}
22+
23+
/**
24+
* Code snippet from \_update_fix_missing_schema.
25+
*/
26+
function bug516_2(): void
27+
{
28+
$module_handler = \Drupal::moduleHandler();
29+
$enabled_modules = $module_handler->getModuleList();
30+
31+
foreach (array_keys($enabled_modules) as $module) {
32+
$module_handler->loadInclude($module, 'install');
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)