rector/packages/Skipper/RealpathMatcher.php
Tomas Votruba d1c230fe39 Updated Rector to commit effe4d38f68cfe64a38f7c2422befed3a872142e
effe4d38f6 [Skipper] Handle provide direct relative path in Skipper (#2921)
2022-09-11 12:05:24 +00:00

35 lines
1.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Skipper;
final class RealpathMatcher
{
public function match(string $matchingPath, string $filePath) : bool
{
$realPathMatchingPath = \realpath($matchingPath);
$realpathFilePath = \realpath($filePath);
if (!\is_string($realPathMatchingPath)) {
return \false;
}
if (!\is_string($realpathFilePath)) {
return \false;
}
$normalizedMatchingPath = $this->normalizePath($realPathMatchingPath);
$normalizedFilePath = $this->normalizePath($realpathFilePath);
// skip define direct path
if (\is_file($normalizedMatchingPath)) {
return $normalizedMatchingPath === $normalizedFilePath;
}
// ensure add / suffix to ensure no same prefix directory
if (\is_dir($normalizedMatchingPath)) {
$normalizedMatchingPath = \rtrim($normalizedMatchingPath, '/') . '/';
}
return \strncmp($normalizedFilePath, $normalizedMatchingPath, \strlen($normalizedMatchingPath)) === 0;
}
private function normalizePath(string $path) : string
{
return \str_replace('\\', '/', $path);
}
}