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
8 changes: 6 additions & 2 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public function normalizePath(string $originalPath, string $directorySeparator =
$path = $originalPath;
}

$path = str_replace(['\\', '//', '///', '////'], '/', $path);

$path = $this->normalizeSeparator($path);
$pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';
$pathParts = explode('/', trim($path, '/'));

Expand All @@ -113,4 +112,9 @@ public function normalizePath(string $originalPath, string $directorySeparator =
return self::$normalizedPathsCache[$originalPath][$directorySeparator] = ($scheme !== null ? $scheme . '://' : '') . $pathRoot . implode($directorySeparator, $normalizedPathParts);
}

public function normalizeSeparator(string $path): string
{
return str_replace(['\\', '//', '///', '////'], '/', $path);
}

}
11 changes: 9 additions & 2 deletions src/Rules/Keywords/RequireFileExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct(
private ExprPrinter $exprPrinter,
#[AutowiredParameter(ref: '%featureToggles.magicDirInInclude%')]
private bool $checkMagicDirInInclude,
private FileHelper $fileHelper,
)
{
}
Expand Down Expand Up @@ -188,10 +189,16 @@ private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallb
$rightPaths = $this->resolveFilePaths($expr->right, $scope, $magicDirFallback);
foreach ($this->resolveFilePaths($expr->left, $scope, $magicDirFallback) as $left) {
foreach ($rightPaths as $rightPath) {
$paths[] = new ConstantStringType($left->getValue() . $rightPath->getValue());
$normalizedPath = $this->fileHelper->normalizeSeparator($left->getValue() . $rightPath->getValue());
$paths[$normalizedPath] = $normalizedPath;
}
}
return $paths;

$list = [];
foreach ($paths as $path) {
$list[] = new ConstantStringType($path);
}
return $list;
}

private function isInFileExists(Include_ $node, Scope $scope): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Keywords;

use PHPStan\File\FileHelper;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -20,6 +21,7 @@ protected function getRule(): Rule
$this->currentWorkingDirectory,
self::getContainer()->getByType(ExprPrinter::class),
true,
self::getContainer()->getByType(FileHelper::class),
);
}

Expand All @@ -42,6 +44,14 @@ public function testBug12203NoConstantPath(): void
'Path in require_once() __DIR__ . "{$path}/{$file}" is not a file or it does not exist.',
12,
],
[
'Path in require_once() __DIR__ . DIRECTORY_SEPARATOR . $path . \'/\' . $file is not a file or it does not exist.',
14,
],
[
'Path in require_once() "../bug-12203-sure-does-not-exist.php" is not a file or it does not exist.',
15,
],
]);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Keywords/RequireFileExistsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Keywords;

use PHPStan\File\FileHelper;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -25,6 +26,7 @@ protected function getRule(): Rule
$this->currentWorkingDirectory,
self::getContainer()->getByType(ExprPrinter::class),
true,
self::getContainer()->getByType(FileHelper::class),
);
}

Expand Down Expand Up @@ -145,6 +147,14 @@ public function testBug12203(): void
'Path in require_once() __DIR__ . "{$path}/{$file}" is not a file or it does not exist.',
12,
],
[
'Path in require_once() __DIR__ . DIRECTORY_SEPARATOR . $path . \'/\' . $file is not a file or it does not exist.',
14,
],
[
'Path in require_once() "../bug-12203-sure-does-not-exist.php" is not a file or it does not exist.',
15,
],
]);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/bug-12203.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
require_once __DIR__ . '/'. $path .'/'. $file;

require_once __DIR__ . "$path/$file";

require_once __DIR__ . DIRECTORY_SEPARATOR. $path .'/'. $file;
require_once '..'. \DIRECTORY_SEPARATOR .'bug-12203-sure-does-not-exist.php';
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
include_once $fileThatDoesNotExist;
require $fileThatDoesNotExist;
require_once $fileThatDoesNotExist;

$fileThatExists = __DIR__ . \DIRECTORY_SEPARATOR . 'include-me-to-prove-you-work.txt';
Loading