Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LoadIncludes.php

class LoadIncludes

@extends LoadIncludeBase<Node\Expr\MethodCall>

Hierarchy

  • class \mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludeBase implements \PHPStan\Rules\Rule
    • class \mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludes extends \mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludeBase

Expanded class hierarchy of LoadIncludes

File

vendor/mglaman/phpstan-drupal/src/Rules/Drupal/LoadIncludes.php, line 18

Namespace

mglaman\PHPStanDrupal\Rules\Drupal
View source
class LoadIncludes extends LoadIncludeBase {
    public function getNodeType() : string {
        return Node\Expr\MethodCall::class;
    }
    public function processNode(Node $node, Scope $scope) : array {
        if (!$node->name instanceof Node\Identifier) {
            return [];
        }
        $method_name = $node->name
            ->toString();
        if ($method_name !== 'loadInclude') {
            return [];
        }
        $args = $node->getArgs();
        if (count($args) < 2) {
            return [];
        }
        $type = $scope->getType($node->var);
        $moduleHandlerInterfaceType = new ObjectType(ModuleHandlerInterface::class);
        if (!$type->isSuperTypeOf($moduleHandlerInterfaceType)
            ->yes()) {
            return [];
        }
        try {
            // Try to invoke it similarly as the module handler itself.
            [
                $moduleName,
                $filename,
            ] = $this->parseLoadIncludeArgs($args[0], $args[1], $args[2] ?? null, $scope);
            if (!$moduleName && !$filename) {
                // Couldn't determine module- nor file-name, most probably
                // because it's a variable. Nothing to load, bail now.
                return [];
            }
            $module = $this->extensionMap
                ->getModule($moduleName);
            if ($module === null) {
                return [
                    RuleErrorBuilder::message(sprintf('File %s could not be loaded from %s::loadInclude because %s module is not found.', $filename, ModuleHandlerInterface::class, $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 %s::loadInclude', $module->getPath() . '/' . $filename, ModuleHandlerInterface::class))
                    ->line($node->getStartLine())
                    ->build(),
            ];
        } catch (Throwable $e) {
            return [
                RuleErrorBuilder::message(sprintf('A file could not be loaded from %s::loadInclude', ModuleHandlerInterface::class))->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
LoadIncludes::getNodeType public function
LoadIncludes::processNode public function
RSS feed
Powered by Drupal