Skip to content

Commit 54f7779

Browse files
committed
Handle include() and include_once()
1 parent 9896884 commit 54f7779

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/Rules/Keywords/RequireFileExistsRule.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,16 @@ public function getNodeType(): string
2626

2727
public function processNode(Node $node, Scope $scope): array
2828
{
29-
if ($this->shouldProcessNode($node)) {
30-
$filePath = $this->resolveFilePath($node, $scope);
31-
if (is_string($filePath) && !is_file($filePath)) {
32-
return [
33-
$this->getErrorMessage($node, $filePath),
34-
];
35-
}
29+
$filePath = $this->resolveFilePath($node, $scope);
30+
if (is_string($filePath) && !is_file($filePath)) {
31+
return [
32+
$this->getErrorMessage($node, $filePath),
33+
];
3634
}
3735

3836
return [];
3937
}
4038

41-
private function shouldProcessNode(Node $node): bool
42-
{
43-
return $node instanceof Include_ && (
44-
$node->type === Include_::TYPE_REQUIRE
45-
|| $node->type === Include_::TYPE_REQUIRE_ONCE
46-
);
47-
}
48-
4939
private function getErrorMessage(Include_ $node, string $filePath): RuleError
5040
{
5141
switch ($node->type) {
@@ -55,6 +45,12 @@ private function getErrorMessage(Include_ $node, string $filePath): RuleError
5545
case Include_::TYPE_REQUIRE_ONCE:
5646
$message = 'Path in require_once() "%s" is not a file or it does not exist.';
5747
break;
48+
case Include_::TYPE_INCLUDE:
49+
$message = 'Path in include() "%s" is not a file or it does not exist.';
50+
break;
51+
case Include_::TYPE_INCLUDE_ONCE:
52+
$message = 'Path in include_once() "%s" is not a file or it does not exist.';
53+
break;
5854
default:
5955
throw new ShouldNotHappenException('Rule should have already validated the node type.');
6056
}

tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function testItCannotReadConstantsDefinedInTheAnalysedFile(): void
2525
public function testFileExistsButPathIsRelative(): void
2626
{
2727
$this->analyse([__DIR__ . '/data/file-exists-but-path-is-relative.php'], [
28+
[
29+
'Path in include() "include-me-to-prove-you-work.txt" is not a file or it does not exist.',
30+
3,
31+
],
32+
[
33+
'Path in include_once() "include-me-to-prove-you-work.txt" is not a file or it does not exist.',
34+
4,
35+
],
2836
[
2937
'Path in require() "include-me-to-prove-you-work.txt" is not a file or it does not exist.',
3038
5,
@@ -44,6 +52,14 @@ public function testFileExistsUsingClassConst(): void
4452
public function testFileDoesNotExistUsingClassConst(): void
4553
{
4654
$this->analyse([__DIR__ . '/data/file-does-not-exist-using-class-const.php'], [
55+
[
56+
'Path in include() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
57+
5,
58+
],
59+
[
60+
'Path in include_once() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
61+
6,
62+
],
4763
[
4864
'Path in require() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
4965
7,
@@ -67,6 +83,14 @@ public function testFileDoesNotExistUsingConst(): void
6783
define('FILE_DOES_NOT_EXIST', 'a-file-that-does-not-exist.php');
6884

6985
$this->analyse([__DIR__ . '/data/file-does-not-exist-using-constant.php'], [
86+
[
87+
'Path in include() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
88+
5,
89+
],
90+
[
91+
'Path in include_once() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
92+
6,
93+
],
7094
[
7195
'Path in require() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
7296
7,
@@ -91,6 +115,14 @@ public function testFileExistsUsingVariables(): void
91115
public function testFileDoesNotExistButUsesVariables(): void
92116
{
93117
$this->analyse([__DIR__ . '/data/file-does-not-exist-but-uses-a-variable.php'], [
118+
[
119+
'Path in include() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
120+
5,
121+
],
122+
[
123+
'Path in include_once() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
124+
6,
125+
],
94126
[
95127
'Path in require() "a-file-that-does-not-exist.php" is not a file or it does not exist.',
96128
7,

0 commit comments

Comments
 (0)