mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
d1c230fe39
effe4d38f6
[Skipper] Handle provide direct relative path in Skipper (#2921)
35 lines
1.2 KiB
PHP
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);
|
|
}
|
|
}
|