diff --git a/src/Rules/Keywords/RequireFileExistsRule.php b/src/Rules/Keywords/RequireFileExistsRule.php index b0d4def309..b47817e4d6 100644 --- a/src/Rules/Keywords/RequireFileExistsRule.php +++ b/src/Rules/Keywords/RequireFileExistsRule.php @@ -3,7 +3,10 @@ namespace PHPStan\Rules\Keywords; use PhpParser\Node; +use PhpParser\Node\Arg; +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; @@ -41,6 +44,10 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if ($this->isInFileExists($node, $scope)) { + return []; + } + $errors = []; $paths = $this->resolveFilePaths($node, $scope); @@ -140,4 +147,19 @@ private function resolveFilePaths(Include_ $node, Scope $scope): array return $paths; } + private function isInFileExists(Include_ $node, Scope $scope): bool + { + foreach (['file_exists', 'is_file'] as $funcName) { + $expr = new FuncCall(new FullyQualified($funcName), [ + new Arg($node->expr), + ]); + + if ($scope->getType($expr)->isTrue()->yes()) { + return true; + } + } + + return false; + } + } diff --git a/tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php b/tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php index 732819b506..6599e0defe 100644 --- a/tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php +++ b/tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php @@ -136,4 +136,9 @@ public function testBug12203(): void ]); } + public function testInFileExists(): void + { + $this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php b/tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php new file mode 100644 index 0000000000..f3b1e22633 --- /dev/null +++ b/tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php @@ -0,0 +1,23 @@ +