|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Drupal; |
| 4 | + |
| 5 | +use Drupal\Core\Extension\ModuleHandler; |
| 6 | +use Drupal\Core\Extension\ModuleHandlerInterface; |
| 7 | +use DrupalFinder\DrupalFinder; |
| 8 | +use PhpParser\Node; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Drupal\ExtensionDiscovery; |
| 11 | +use PHPStan\Reflection\MethodReflection; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\ShouldNotHappenException; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | + |
| 16 | +class LoadIncludes implements Rule |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * The project root. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $projectRoot; |
| 25 | + |
| 26 | + /** |
| 27 | + * LoadIncludes constructor. |
| 28 | + * @param string $project_root |
| 29 | + */ |
| 30 | + public function __construct(string $project_root) |
| 31 | + { |
| 32 | + $this->projectRoot = $project_root; |
| 33 | + } |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return Node\Expr\MethodCall::class; |
| 38 | + } |
| 39 | + |
| 40 | + public function processNode(Node $node, Scope $scope): array |
| 41 | + { |
| 42 | + assert($node instanceof Node\Expr\MethodCall); |
| 43 | + if (!$node->name instanceof Node\Identifier) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + $method_name = $node->name->toString(); |
| 47 | + if ($method_name !== 'loadInclude') { |
| 48 | + return []; |
| 49 | + } |
| 50 | + $variable = $node->var; |
| 51 | + if (!$variable instanceof Node\Expr\Variable) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + $var_name = $variable->name; |
| 55 | + if (!is_string($var_name)) { |
| 56 | + 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())); |
| 57 | + } |
| 58 | + $type = $scope->getVariableType($var_name); |
| 59 | + assert($type instanceof ObjectType); |
| 60 | + if (!class_exists($type->getClassName()) && !interface_exists($type->getClassName())) { |
| 61 | + throw new ShouldNotHappenException(sprintf('Could not find class for %s from reflection.', get_called_class())); |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + $reflected = new \ReflectionClass($type->getClassName()); |
| 66 | + if (!$reflected->implementsInterface(ModuleHandlerInterface::class)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + // Try to invoke it similarily as the module handler itself. |
| 70 | + $finder = new DrupalFinder(); |
| 71 | + $finder->locateRoot($this->projectRoot); |
| 72 | + $drupal_root = $finder->getDrupalRoot(); |
| 73 | + $extensionDiscovery = new ExtensionDiscovery($drupal_root); |
| 74 | + $modules = $extensionDiscovery->scan('module'); |
| 75 | + $module_arg = $node->args[0]; |
| 76 | + assert($module_arg->value instanceof Node\Scalar\String_); |
| 77 | + $type_arg = $node->args[1]; |
| 78 | + assert($type_arg->value instanceof Node\Scalar\String_); |
| 79 | + $name_arg = $node->args[2] ?? null; |
| 80 | + |
| 81 | + if ($name_arg === null) { |
| 82 | + $name_arg = $module_arg; |
| 83 | + } |
| 84 | + assert($name_arg->value instanceof Node\Scalar\String_); |
| 85 | + |
| 86 | + $module_name = $module_arg->value->value; |
| 87 | + if (!isset($modules[$module_name])) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + $type_prefix = $name_arg->value->value; |
| 91 | + $type_filename = $type_arg->value->value; |
| 92 | + $module = $modules[$module_name]; |
| 93 | + $file = $drupal_root . '/' . $module->getPath() . "/$type_prefix.$type_filename"; |
| 94 | + if (is_file($file)) { |
| 95 | + require_once $file; |
| 96 | + return []; |
| 97 | + } |
| 98 | + return [sprintf('File %s could not be loaded from %s::loadInclude', $file, $type->getClassName())]; |
| 99 | + } catch (\Throwable $e) { |
| 100 | + return [sprintf('A file could not be loaded from %s::loadInclude', $type->getClassName())]; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments