Repository navigation
Expand file tree
/
Copy pathRequireFileExistsRule.php
More file actions
173 lines (145 loc) · 4.29 KB
/
Copy pathRequireFileExistsRule.php
File metadata and controls
173 lines (145 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Keywords;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\File\FileExistenceChecker;
use PHPStan\File\FileHelper;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use function dirname;
use function sprintf;
/**
* @implements Rule<Include_>
*/
#[RegisteredRule(level: 0)]
final class RequireFileExistsRule implements Rule
{
public function __construct(
private ExprPrinter $exprPrinter,
#[AutowiredParameter(ref: '%featureToggles.magicDirInInclude%')]
private bool $checkMagicDirInInclude,
private FileHelper $fileHelper,
private FileExistenceChecker $fileExistenceChecker,
)
{
}
public function getNodeType(): string
{
return Include_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($this->isInFileExists($node, $scope)) {
return [];
}
$errors = [];
$usedMagicDirFallback = false;
$paths = $this->resolveFilePaths($node->expr, $scope, $usedMagicDirFallback);
foreach ($paths as $path) {
$path = $path->getValue();
if ($this->doesFileExist($path, $scope)) {
continue;
}
if ($usedMagicDirFallback) {
$pathExpr = $this->exprPrinter->printExpr($node->expr);
} else {
$pathExpr = '"' . $path . '"';
}
$errors[] = $this->getErrorMessage($node, $pathExpr);
}
return $errors;
}
private function doesFileExist(string $path, Scope $scope): bool
{
return $this->fileExistenceChecker->fileExists($path, dirname($scope->getFile()));
}
private function getErrorMessage(Include_ $node, string $filePath): IdentifierRuleError
{
$message = 'Path in %s() %s is not a file or it does not exist.';
switch ($node->type) {
case Include_::TYPE_REQUIRE:
$type = 'require';
$identifierType = 'require';
break;
case Include_::TYPE_REQUIRE_ONCE:
$type = 'require_once';
$identifierType = 'requireOnce';
break;
case Include_::TYPE_INCLUDE:
$type = 'include';
$identifierType = 'include';
break;
case Include_::TYPE_INCLUDE_ONCE:
$type = 'include_once';
$identifierType = 'includeOnce';
break;
default:
throw new ShouldNotHappenException('Rule should have already validated the node type.');
}
$identifier = sprintf('%s.fileNotFound', $identifierType);
return RuleErrorBuilder::message(
sprintf(
$message,
$type,
$filePath,
),
)->identifier($identifier)->build();
}
/**
* @return list<ConstantStringType>
*/
private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallback): array
{
$magicDirFallback = false;
if (!$this->checkMagicDirInInclude) {
return $scope->getType($expr)->getConstantStrings();
}
if (!$expr instanceof Expr\BinaryOp\Concat) {
return $scope->getType($expr)->getConstantStrings();
}
if ($expr->left instanceof Node\Scalar\MagicConst\Dir) {
$magicDirFallback = true;
$paths = [];
foreach ($scope->getType($expr->right)->getConstantStrings() as $constantString) {
$paths[] = new ConstantStringType(dirname($scope->getFile()) . $constantString->getValue());
}
return $paths;
}
$paths = [];
$rightPaths = $this->resolveFilePaths($expr->right, $scope, $magicDirFallback);
foreach ($this->resolveFilePaths($expr->left, $scope, $magicDirFallback) as $left) {
foreach ($rightPaths as $rightPath) {
$normalizedPath = $this->fileHelper->normalizeSeparator($left->getValue() . $rightPath->getValue());
$paths[$normalizedPath] = $normalizedPath;
}
}
$list = [];
foreach ($paths as $path) {
$list[] = new ConstantStringType($path);
}
return $list;
}
private function isInFileExists(Include_ $node, Scope $scope): bool
{
foreach (FileExistenceChecker::FILE_EXISTENCE_FUNCTIONS as $funcName) {
$expr = new FuncCall(new FullyQualified($funcName), [
new Arg($node->expr),
]);
if ($scope->getType($expr)->isTrue()->yes()) {
return true;
}
}
return false;
}
}