Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Rules/Keywords/RequireFileExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +44,10 @@

public function processNode(Node $node, Scope $scope): array
{
if ($this->isInFileExists($node, $scope)) {
return [];
}

$errors = [];
$paths = $this->resolveFilePaths($node, $scope);

Expand Down Expand Up @@ -140,4 +147,19 @@
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()) {

Check warning on line 157 in src/Rules/Keywords/RequireFileExistsRule.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ new Arg($node->expr), ]); - if ($scope->getType($expr)->isTrue()->yes()) { + if ($scope->getType($expr)->toBoolean()->isTrue()->yes()) { return true; } }

Check warning on line 157 in src/Rules/Keywords/RequireFileExistsRule.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ new Arg($node->expr), ]); - if ($scope->getType($expr)->isTrue()->yes()) { + if ($scope->getType($expr)->toBoolean()->isTrue()->yes()) { return true; } }
return true;
}
}

return false;
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ public function testBug12203(): void
]);
}

public function testInFileExists(): void
{
$this->analyse([__DIR__ . '/data/include-in-file-exists.php'], []);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
}
}

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../../autoload.php')) {
require __DIR__ . '/../../autoload.php';
} else {
throw new \RuntimeException("Unable to locate vendor/autoload.php");
}
Loading