-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle dynamic file loading with module_load_include
Fixes #166
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPStan\Rules\Drupal; | ||
|
||
use DrupalFinder\DrupalFinder; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Drupal\ExtensionDiscovery; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* Handles module_load_include dynamic file loading. | ||
* | ||
* @note may become deprecated and removed in D10 | ||
* @see https://www.drupal.org/project/drupal/issues/697946 | ||
*/ | ||
class ModuleLoadInclude implements Rule | ||
{ | ||
|
||
/** | ||
* The project root. | ||
* | ||
* @var string | ||
*/ | ||
protected $projectRoot; | ||
|
||
/** | ||
* ModuleLoadInclude constructor. | ||
* @param string $project_root | ||
*/ | ||
public function __construct(string $project_root) | ||
{ | ||
$this->projectRoot = $project_root; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof Node\Expr\FuncCall); | ||
if (!$node->name instanceof \PhpParser\Node\Name) { | ||
return []; | ||
} | ||
$name = (string) $node->name; | ||
if ($name !== 'module_load_include') { | ||
return []; | ||
} | ||
$stop = null; | ||
|
||
try { | ||
// Try to invoke it similarily as the module handler itself. | ||
$finder = new DrupalFinder(); | ||
$finder->locateRoot($this->projectRoot); | ||
$drupal_root = $finder->getDrupalRoot(); | ||
$extensionDiscovery = new ExtensionDiscovery($drupal_root); | ||
$modules = $extensionDiscovery->scan('module'); | ||
$type_arg = $node->args[0]; | ||
assert($type_arg->value instanceof Node\Scalar\String_); | ||
$module_arg = $node->args[1]; | ||
assert($module_arg->value instanceof Node\Scalar\String_); | ||
$name_arg = $node->args[2] ?? null; | ||
|
||
if ($name_arg === null) { | ||
$name_arg = $module_arg; | ||
} | ||
assert($name_arg->value instanceof Node\Scalar\String_); | ||
|
||
$module_name = $module_arg->value->value; | ||
if (!isset($modules[$module_name])) { | ||
return []; | ||
} | ||
$type_prefix = $name_arg->value->value; | ||
$type_filename = $type_arg->value->value; | ||
$module = $modules[$module_name]; | ||
$file = $drupal_root . '/' . $module->getPath() . "/$type_prefix.$type_filename"; | ||
if (is_file($file)) { | ||
require_once $file; | ||
return []; | ||
} | ||
return [sprintf('File %s could not be loaded from module_load_include', $file)]; | ||
} catch (\Throwable $e) { | ||
return ['A file could not be loaded from module_load_include']; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../fixtures/drupal/modules/module_load_include_fixture/module_load_include_fixture.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type: module | ||
name: Module with tests | ||
core: 8.x |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/drupal/modules/module_load_include_fixture/module_load_include_fixture.module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types=1); | ||
|
||
function test_locale_translation_inc_included() { | ||
module_load_include('bulk.inc', 'locale'); | ||
locale_translate_get_interface_translation_files(); | ||
module_load_include('compare.inc', 'locale'); | ||
locale_translation_build_projects(); | ||
module_load_include('translation.inc', 'locale'); | ||
locale_translation_build_sources(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters