Skip to content

Commit 61ac4c1

Browse files
authored
Load includes not working from Drupal::moduleHandler()->loadInclude() (#550)
1 parent 05654e0 commit 61ac4c1

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/Rules/Drupal/LoadIncludes.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\RuleErrorBuilder;
9-
use PHPStan\ShouldNotHappenException;
109
use PHPStan\Type\ObjectType;
1110

1211
class LoadIncludes extends LoadIncludeBase
@@ -31,17 +30,9 @@ public function processNode(Node $node, Scope $scope): array
3130
if (\count($args) < 2) {
3231
return [];
3332
}
34-
$variable = $node->var;
35-
if (!$variable instanceof Node\Expr\Variable) {
36-
return [];
37-
}
38-
$var_name = $variable->name;
39-
if (!is_string($var_name)) {
40-
throw new ShouldNotHappenException(sprintf('Expected string for variable in %s, please open an issue on GitHub https://github.com/mglaman/phpstan-drupal/issues', static::class));
41-
}
33+
$type = $scope->getType($node->var);
4234
$moduleHandlerInterfaceType = new ObjectType(ModuleHandlerInterface::class);
43-
$variableType = $scope->getVariableType($var_name);
44-
if (!$variableType->isSuperTypeOf($moduleHandlerInterfaceType)->yes()) {
35+
if (!$type->isSuperTypeOf($moduleHandlerInterfaceType)->yes()) {
4536
return [];
4637
}
4738

tests/src/Rules/LoadIncludesRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public function cases(): \Generator
5050
[__DIR__.'/data/bug-547.php'],
5151
[]
5252
];
53+
54+
yield 'bug-177.php' => [
55+
[__DIR__.'/data/bug-177.php'],
56+
[
57+
[
58+
'File core/modules/locale/locale.fetch.php could not be loaded from Drupal\Core\Extension\ModuleHandlerInterface::loadInclude',
59+
6
60+
],
61+
[
62+
'File core/modules/locale/locale.fetch.php could not be loaded from Drupal\Core\Extension\ModuleHandlerInterface::loadInclude',
63+
8
64+
],
65+
]
66+
];
5367
}
5468

5569
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Bug177;
4+
5+
function foo() {
6+
\Drupal::moduleHandler()->loadInclude('locale', 'fetch.php');
7+
$moduleHandler = \Drupal::moduleHandler();
8+
$moduleHandler->loadInclude('locale', 'fetch.php');
9+
}

0 commit comments

Comments
 (0)