Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Skipper] Allow skip relative path on command line vendor/bin/ecs che…
…ck path/to/relative/path (#193) * [Skipper] Allow skip relative path on command line vendor/bin/ecs check path/to/relative/file * clean up
- Loading branch information
1 parent
104c1c8
commit edbb408
Showing
3 changed files
with
51 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Skipper; | ||
|
||
final class RealpathMatcher | ||
{ | ||
public function match(string $matchingPath, string $filePath): bool | ||
{ | ||
/** @var string|false $realPathMatchingPath */ | ||
$realPathMatchingPath = realpath($matchingPath); | ||
if ($realPathMatchingPath === false) { | ||
return false; | ||
} | ||
|
||
/** @var string|false $realpathFilePath */ | ||
$realpathFilePath = realpath($filePath); | ||
if ($realpathFilePath === false) { | ||
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 str_starts_with($normalizedFilePath, $normalizedMatchingPath); | ||
} | ||
|
||
private function normalizePath(string $path): string | ||
{ | ||
return str_replace('\\', '/', $path); | ||
} | ||
} |