|
4 | 4 |
|
5 | 5 | use Drupal\Core\Extension\ModuleHandlerInterface;
|
6 | 6 | use DrupalFinder\DrupalFinder;
|
| 7 | +use mglaman\PHPStanDrupal\Drupal\ExtensionDiscovery; |
7 | 8 | use PhpParser\Node;
|
8 | 9 | use PHPStan\Analyser\Scope;
|
9 |
| -use mglaman\PHPStanDrupal\Drupal\ExtensionDiscovery; |
10 | 10 | use PHPStan\Rules\Rule;
|
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
11 | 12 | use PHPStan\ShouldNotHappenException;
|
12 | 13 | use PHPStan\Type\ObjectType;
|
13 | 14 |
|
@@ -53,17 +54,13 @@ public function processNode(Node $node, Scope $scope): array
|
53 | 54 | if (!is_string($var_name)) {
|
54 | 55 | throw new ShouldNotHappenException(sprintf('Expected string for variable in %s, please open an issue on GitHub https://github.com/mglaman/phpstan-drupal/issues', get_called_class()));
|
55 | 56 | }
|
56 |
| - $type = $scope->getVariableType($var_name); |
57 |
| - assert($type instanceof ObjectType); |
58 |
| - if (!class_exists($type->getClassName()) && !interface_exists($type->getClassName())) { |
59 |
| - throw new ShouldNotHappenException(sprintf('Could not find class for %s from reflection.', get_called_class())); |
| 57 | + $moduleHandlerInterfaceType = new ObjectType(ModuleHandlerInterface::class); |
| 58 | + $variableType = $scope->getVariableType($var_name); |
| 59 | + if (!$variableType->isSuperTypeOf($moduleHandlerInterfaceType)->yes()) { |
| 60 | + return []; |
60 | 61 | }
|
61 | 62 |
|
62 | 63 | try {
|
63 |
| - $reflected = new \ReflectionClass($type->getClassName()); |
64 |
| - if (!$reflected->implementsInterface(ModuleHandlerInterface::class)) { |
65 |
| - return []; |
66 |
| - } |
67 | 64 | // Try to invoke it similarily as the module handler itself.
|
68 | 65 | $finder = new DrupalFinder();
|
69 | 66 | $finder->locateRoot($this->projectRoot);
|
@@ -97,9 +94,24 @@ public function processNode(Node $node, Scope $scope): array
|
97 | 94 | require_once $file;
|
98 | 95 | return [];
|
99 | 96 | }
|
100 |
| - return [sprintf('File %s could not be loaded from %s::loadInclude', $file, $type->getClassName())]; |
| 97 | + return [ |
| 98 | + RuleErrorBuilder::message(sprintf( |
| 99 | + 'File %s could not be loaded from %s::loadInclude', |
| 100 | + $file, |
| 101 | + ModuleHandlerInterface::class |
| 102 | + )) |
| 103 | + ->line($node->getLine()) |
| 104 | + ->build() |
| 105 | + ]; |
101 | 106 | } catch (\Throwable $e) {
|
102 |
| - return [sprintf('A file could not be loaded from %s::loadInclude', $type->getClassName())]; |
| 107 | + return [ |
| 108 | + RuleErrorBuilder::message(sprintf( |
| 109 | + 'A file could not be loaded from %s::loadInclude', |
| 110 | + ModuleHandlerInterface::class |
| 111 | + )) |
| 112 | + ->line($node->getLine()) |
| 113 | + ->build() |
| 114 | + ]; |
103 | 115 | }
|
104 | 116 | }
|
105 | 117 | }
|
0 commit comments