class ModuleLoadInclude
Handles module_load_include dynamic file loading.
@note may become deprecated and removed in D10
@extends LoadIncludeBase<Node\Expr\FuncCall>
Hierarchy
- class \mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludeBase implements \PHPStan\Rules\Rule
- class \mglaman\PHPStanDrupal\Rules\Drupal\ModuleLoadInclude extends \mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludeBase
Expanded class hierarchy of ModuleLoadInclude
See also
https://www.drupal.org/project/drupal/issues/697946
File
-
vendor/
mglaman/ phpstan-drupal/ src/ Rules/ Drupal/ ModuleLoadInclude.php, line 22
Namespace
mglaman\PHPStanDrupal\Rules\DrupalView source
class ModuleLoadInclude extends LoadIncludeBase {
public function getNodeType() : string {
return Node\Expr\FuncCall::class;
}
public function processNode(Node $node, Scope $scope) : array {
if (!$node->name instanceof Name) {
return [];
}
$name = (string) $node->name;
if ($name !== 'module_load_include') {
return [];
}
$args = $node->getArgs();
if (count($args) < 2) {
return [];
}
try {
// Try to invoke it similarly as the module handler itself.
[
$moduleName,
$filename,
] = $this->parseLoadIncludeArgs($args[1], $args[0], $args[2] ?? null, $scope);
$module = $this->extensionMap
->getModule($moduleName);
if ($module === null) {
return [
RuleErrorBuilder::message(sprintf('File %s could not be loaded from module_load_include because %s module is not found.', $filename, $moduleName))->line($node->getStartLine())
->build(),
];
}
$file = $module->getAbsolutePath() . DIRECTORY_SEPARATOR . $filename;
if (is_file($file)) {
require_once $file;
return [];
}
return [
RuleErrorBuilder::message(sprintf('File %s could not be loaded from module_load_include.', $module->getPath() . '/' . $filename))
->line($node->getStartLine())
->build(),
];
} catch (Throwable $e) {
return [
RuleErrorBuilder::message('A file could not be loaded from module_load_include')->line($node->getStartLine())
->build(),
];
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
LoadIncludeBase::$extensionMap | protected | property | |
LoadIncludeBase::getStringArgValue | private | function | |
LoadIncludeBase::parseLoadIncludeArgs | protected | function | |
LoadIncludeBase::__construct | public | function | |
ModuleLoadInclude::getNodeType | public | function | |
ModuleLoadInclude::processNode | public | function |