Skip to content

Commit 05654e0

Browse files
Boegiemglaman
andauthored
Skip loadInclude if baseName is not resolveable (#548)
Co-authored-by: Matt Glaman <[email protected]>
1 parent a68d6f2 commit 05654e0

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/Rules/Drupal/LoadIncludeBase.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\Type\Constant\ConstantStringType;
109

1110
abstract class LoadIncludeBase implements Rule
1211
{
@@ -24,8 +23,9 @@ public function __construct(ExtensionMap $extensionMap)
2423
private function getStringArgValue(Node\Expr $expr, Scope $scope): ?string
2524
{
2625
$type = $scope->getType($expr);
27-
if ($type instanceof ConstantStringType) {
28-
return $type->getValue();
26+
$stringTypes = $type->getConstantStrings();
27+
if (count($stringTypes) > 0) {
28+
return $stringTypes[0]->getValue();
2929
}
3030
return null;
3131
}
@@ -43,6 +43,9 @@ protected function parseLoadIncludeArgs(Node\Arg $module, Node\Arg $type, ?Node\
4343
$baseName = null;
4444
if ($name !== null) {
4545
$baseName = $this->getStringArgValue($name->value, $scope);
46+
if ($baseName === null) {
47+
return [false, false];
48+
}
4649
}
4750
if ($baseName === null) {
4851
$baseName = $moduleName;

tests/src/Rules/LoadIncludesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function cases(): \Generator
4545
[__DIR__.'/data/bug-516.php'],
4646
[]
4747
];
48+
49+
yield 'bug-547.php' => [
50+
[__DIR__.'/data/bug-547.php'],
51+
[]
52+
];
4853
}
4954

5055
}

tests/src/Rules/data/bug-547.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug547;
4+
5+
/**
6+
* Tests moduleHandler::loadInclude where $name argument includes a variable.
7+
*/
8+
class TestClass {
9+
10+
/**
11+
* Code snippet from core/modules/system/tests/modules/entity_test/entity_test.install.
12+
*/
13+
function bug547(): void
14+
{
15+
$module_handler = \Drupal::moduleHandler();
16+
$index = \Drupal::state()->get('entity_test.db_updates.entity_definition_updates');
17+
$module_handler->loadInclude('entity_test', 'inc', 'update/entity_definition_updates_' . $index);
18+
}
19+
20+
}
21+

0 commit comments

Comments
 (0)