Skip to content

Commit d91af03

Browse files
committed
support nested concat
1 parent d8a9e52 commit d91af03

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/Rules/Keywords/RequireFileExistsRule.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
5454

5555
$errors = [];
5656
$usedMagicDirFallback = false;
57-
$paths = $this->resolveFilePaths($node, $scope, $usedMagicDirFallback);
57+
$paths = $this->resolveFilePaths($node->expr, $scope, $usedMagicDirFallback);
5858

5959
foreach ($paths as $path) {
6060
$path = $path->getValue();
@@ -147,10 +147,9 @@ private function getErrorMessage(Include_ $node, string $filePath): IdentifierRu
147147
/**
148148
* @return list<ConstantStringType>
149149
*/
150-
private function resolveFilePaths(Include_ $node, Scope $scope, bool &$magicDirFallback): array
150+
private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallback): array
151151
{
152152
$magicDirFallback = false;
153-
$expr = $node->expr;
154153

155154
if (!$expr instanceof Expr\BinaryOp\Concat) {
156155
return $scope->getType($expr)->getConstantStrings();
@@ -166,7 +165,14 @@ private function resolveFilePaths(Include_ $node, Scope $scope, bool &$magicDirF
166165
return $paths;
167166
}
168167

169-
return $scope->getType($expr)->getConstantStrings();
168+
$paths = [];
169+
$rightPaths = $this->resolveFilePaths($expr->right, $scope, $magicDirFallback);
170+
foreach ($this->resolveFilePaths($expr->left, $scope, $magicDirFallback) as $left) {
171+
foreach ($rightPaths as $rightPath) {
172+
$paths[] = new ConstantStringType($left->getValue() . $rightPath->getValue());
173+
}
174+
}
175+
return $paths;
170176
}
171177

172178
private function isInFileExists(Include_ $node, Scope $scope): bool

tests/PHPStan/Rules/Keywords/RequireFileExistsRuleNoConstantPathTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function testBug12203NoConstantPath(): void
3333
"Path in require_once() __DIR__ . '/../bug-12203-sure-does-not-exist.php' is not a file or it does not exist.",
3434
6,
3535
],
36+
[
37+
"Path in require_once() __DIR__ . '/' . \$path . '/' . \$file is not a file or it does not exist.",
38+
10,
39+
],
3640
]);
3741
}
3842

tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public function testBug12203(): void
136136
"Path in require_once() __DIR__ . '/../bug-12203-sure-does-not-exist.php' is not a file or it does not exist.",
137137
6,
138138
],
139+
[
140+
"Path in require_once() __DIR__ . '/' . \$path . '/' . \$file is not a file or it does not exist.",
141+
10,
142+
],
139143
]);
140144
}
141145

tests/PHPStan/Rules/Keywords/data/bug-12203.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44

55
require_once '../bug-12203-sure-does-not-exist.php';
66
require_once __DIR__ . '/../bug-12203-sure-does-not-exist.php';
7+
8+
$path = '..';
9+
$file = 'bug-12203-sure-does-not-exist.php';
10+
require_once __DIR__ . '/'. $path .'/'. $file;

0 commit comments

Comments
 (0)