Skip to content

Commit

Permalink
[Skipper] Handle provide direct relative path in Skipper (#2921)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 11, 2022
1 parent 2975a1b commit effe4d3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
any content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
any content
2 changes: 2 additions & 0 deletions packages-tests/Skipper/Skipper/Skipper/SkipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function provideDataShouldSkipFileInfo(): Iterator
{
yield [__DIR__ . '/Fixture/SomeRandom/file.txt', false];
yield [__DIR__ . '/Fixture/SomeSkipped/any.txt', true];
yield ['packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt', true];
yield ['packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt', true];
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages-tests/Skipper/Skipper/Skipper/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// windows like path
'*\SomeSkipped\*',

__DIR__ . '/../Fixture/SomeSkippedPath',
__DIR__ . '/../Fixture/SomeSkippedPathToFile/any.txt',

// elements
FifthElement::class,
SixthSense::class,
Expand Down
10 changes: 8 additions & 2 deletions packages/Skipper/Matcher/FileInfoMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use Rector\Skipper\FileSystem\FnMatchPathNormalizer;
use Rector\Skipper\Fnmatcher;
use Rector\Skipper\RealpathMatcher;

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

Expand Down Expand Up @@ -52,6 +54,10 @@ private function doesFileMatchPattern(string $filePath, string $ignoredPath): bo
return true;
}

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

return $this->realpathMatcher->match($ignoredPath, $filePath);
}
}
42 changes: 42 additions & 0 deletions packages/Skipper/RealpathMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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 str_starts_with($normalizedFilePath, $normalizedMatchingPath);
}

private function normalizePath(string $path): string
{
return \str_replace('\\', '/', $path);
}
}
6 changes: 5 additions & 1 deletion packages/Skipper/Skipper/Skipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function shouldSkipElementAndFilePath(string | object $element, string $f
continue;
}

return $skipVoter->shouldSkip($element, $filePath);
if (! $skipVoter->shouldSkip($element, $filePath)) {
continue;
}

return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ private function resolveOriginalPositions(FuncCall $funcCall): array
private function shouldSkip(FuncCall $funcCall): bool
{
$functionNames = array_keys(self::ARG_POSITION_NAME_NULL_TO_STRICT_STRING);
return ! $this->nodeNameResolver->isNames($funcCall, $functionNames) || $funcCall->isFirstClassCallable();
if (! $this->nodeNameResolver->isNames($funcCall, $functionNames)) {
return true;
}

return $funcCall->isFirstClassCallable();
}
}

0 comments on commit effe4d3

Please sign in to comment.