Skip to content

Commit 4680c6e

Browse files
committed
Make error message more descriptive
1 parent 4acc555 commit 4680c6e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Rules/Keywords/RequireFileExistsRule.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4343
$filePath = $this->resolveFilePath($node->expr, $scope);
4444
if (is_string($filePath) && !is_file($filePath)) {
4545
return [
46-
RuleErrorBuilder::message(
47-
sprintf(
48-
'Required file "%s" does not exist.',
49-
$filePath,
50-
),
51-
)->build(),
46+
$this->getErrorMessage($node, $filePath)->build()
5247
];
5348
}
5449
}
@@ -64,6 +59,21 @@ private function shouldProcessNode(Node $node): bool
6459
);
6560
}
6661

62+
private function getErrorMessage(Include_ $node, string $filePath): RuleErrorBuilder
63+
{
64+
$message = match ($node->type) {
65+
Include_::TYPE_REQUIRE => 'Path in require() "%s" is not a file or it does not exist.',
66+
Include_::TYPE_REQUIRE_ONCE => 'Path in require_once() "%s" is not a file or it does not exist.',
67+
};
68+
69+
return RuleErrorBuilder::message(
70+
sprintf(
71+
$message,
72+
$filePath,
73+
),
74+
);
75+
}
76+
6777
private function resolveFilePath(Node $node, Scope $scope): ?string
6878
{
6979
if ($node instanceof String_) {

tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function testFileExistsButPathIsRelative(): void
2626
{
2727
$this->analyse([__DIR__ . '/data/file-exists-but-path-is-relative.php'], [
2828
[
29-
'Required file "include-me-to-prove-you-work.txt" does not exist.',
29+
'Path in require() "include-me-to-prove-you-work.txt" is not a file or it does not exist.',
3030
5,
3131
],
3232
[
33-
'Required file "include-me-to-prove-you-work.txt" does not exist.',
33+
'Path in require_once() "include-me-to-prove-you-work.txt" is not a file or it does not exist.',
3434
6,
3535
],
3636
]);
@@ -45,11 +45,11 @@ public function testFileDoesNotExistUsingClassConst(): void
4545
{
4646
$this->analyse([__DIR__ . '/data/file-does-not-exist-using-class-const.php'], [
4747
[
48-
'Required file "a-file-that-does-not-exist.php" does not exist.',
48+
'Path in require() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
4949
7,
5050
],
5151
[
52-
'Required file "a-file-that-does-not-exist.php" does not exist.',
52+
'Path in require_once() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
5353
8,
5454
],
5555
]);
@@ -64,15 +64,15 @@ public function testFileExistsUsingConst(): void
6464

6565
public function testFileDoesNotExistUsingConst(): void
6666
{
67-
define('FILE_DOES_NOT_EXIST', 'a-file-that-does-not-exist.txt');
67+
define('FILE_DOES_NOT_EXIST', 'a-file-that-does-not-exist.php');
6868

6969
$this->analyse([__DIR__ . '/data/file-does-not-exist-using-constant.php'], [
7070
[
71-
'Required file "a-file-that-does-not-exist.txt" does not exist.',
71+
'Path in require() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
7272
7,
7373
],
7474
[
75-
'Required file "a-file-that-does-not-exist.txt" does not exist.',
75+
'Path in require_once() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
7676
8,
7777
],
7878
]);

0 commit comments

Comments
 (0)