Skip to content

Commit

Permalink
[Skipper] Allow skip relative path on command line vendor/bin/ecs che…
Browse files Browse the repository at this point in the history
…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
samsonasik committed Apr 18, 2024
1 parent 104c1c8 commit edbb408
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/Skipper/FileSystem/FnMatchPathNormalizer.php
Expand Up @@ -4,8 +4,6 @@

namespace Symplify\EasyCodingStandard\Skipper\FileSystem;

use Nette\Utils\Strings;

/**
* @see \Symplify\EasyCodingStandard\Tests\Skipper\FileSystem\FnMatchPathNormalizerTest
*/
Expand Down
10 changes: 8 additions & 2 deletions src/Skipper/Matcher/FileInfoMatcher.php
Expand Up @@ -7,12 +7,14 @@
use SplFileInfo;
use Symplify\EasyCodingStandard\Skipper\FileSystem\FnMatchPathNormalizer;
use Symplify\EasyCodingStandard\Skipper\Fnmatcher;
use Symplify\EasyCodingStandard\Skipper\RealpathMatcher;

final readonly class FileInfoMatcher
{
public function __construct(
private FnMatchPathNormalizer $fnMatchPathNormalizer,
private Fnmatcher $fnmatcher
private Fnmatcher $fnmatcher,
private RealpathMatcher $realpathMatcher,
) {
}

Expand Down Expand Up @@ -55,6 +57,10 @@ private function doesFileInfoMatchPattern(SplFileInfo | string $file, string $ig
return true;
}

return $this->fnmatcher->match($ignoredPath, $filePath);
if ($this->fnmatcher->match($ignoredPath, $filePath)) {
return true;
}

return $this->realpathMatcher->match($ignoredPath, $filePath);
}
}
43 changes: 43 additions & 0 deletions src/Skipper/RealpathMatcher.php
@@ -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);
}
}

0 comments on commit edbb408

Please sign in to comment.