Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit f3f9e46

Browse files
committed
Add support for constants
1 parent 1e6d7c3 commit f3f9e46

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/RequireFileExistsRule.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bellangelo\PHPStanRequireFileExists;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ConstFetch;
89
use PhpParser\Node\Expr\Include_;
910
use PhpParser\Node\Expr\BinaryOp\Concat;
1011
use PhpParser\Node\Expr\ClassConstFetch;
@@ -65,6 +66,10 @@ private function resolveFilePath(Node $node, Scope $scope): ?string
6566
return $this->resolveClassConstant($node);
6667
}
6768

69+
if ($node instanceof ConstFetch) {
70+
return $this->resolveConstant($node);
71+
}
72+
6873
if ($node instanceof Concat) {
6974
$left = $this->resolveFilePath($node->left, $scope);
7075
$right = $this->resolveFilePath($node->right, $scope);
@@ -95,4 +100,18 @@ private function resolveClassConstant(ClassConstFetch $node): ?string
95100
}
96101
return null;
97102
}
103+
104+
private function resolveConstant(ConstFetch $node): ?string
105+
{
106+
if ($node->name instanceof Node\Name) {
107+
$constantName = (string) $node->name;
108+
if (defined($constantName)) {
109+
$constantValue = constant($constantName);
110+
if (is_string($constantValue)) {
111+
return $constantValue;
112+
}
113+
}
114+
}
115+
return null;
116+
}
98117
}

0 commit comments

Comments
 (0)