From effe4d38f68cfe64a38f7c2422befed3a872142e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 11 Sep 2022 18:59:51 +0700 Subject: [PATCH] [Skipper] Handle provide direct relative path in Skipper (#2921) Co-authored-by: GitHub Action --- .../Skipper/Fixture/SomeSkippedPath/any.txt | 1 + .../Fixture/SomeSkippedPathToFile/any.txt | 1 + .../Skipper/Skipper/Skipper/SkipperTest.php | 2 + .../Skipper/Skipper/Skipper/config/config.php | 3 ++ packages/Skipper/Matcher/FileInfoMatcher.php | 10 ++++- packages/Skipper/RealpathMatcher.php | 42 +++++++++++++++++++ packages/Skipper/Skipper/Skipper.php | 6 ++- .../NullToStrictStringFuncCallArgRector.php | 6 ++- 8 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt create mode 100644 packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt create mode 100644 packages/Skipper/RealpathMatcher.php diff --git a/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt b/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt new file mode 100644 index 00000000000..5537ec2bd7a --- /dev/null +++ b/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt @@ -0,0 +1 @@ +any content diff --git a/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt b/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt new file mode 100644 index 00000000000..5537ec2bd7a --- /dev/null +++ b/packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt @@ -0,0 +1 @@ +any content diff --git a/packages-tests/Skipper/Skipper/Skipper/SkipperTest.php b/packages-tests/Skipper/Skipper/Skipper/SkipperTest.php index 9e3d596ce22..2239ca92cc8 100644 --- a/packages-tests/Skipper/Skipper/Skipper/SkipperTest.php +++ b/packages-tests/Skipper/Skipper/Skipper/SkipperTest.php @@ -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]; } /** diff --git a/packages-tests/Skipper/Skipper/Skipper/config/config.php b/packages-tests/Skipper/Skipper/Skipper/config/config.php index 8c819b46530..e79c559bafd 100644 --- a/packages-tests/Skipper/Skipper/Skipper/config/config.php +++ b/packages-tests/Skipper/Skipper/Skipper/config/config.php @@ -11,6 +11,9 @@ // windows like path '*\SomeSkipped\*', + __DIR__ . '/../Fixture/SomeSkippedPath', + __DIR__ . '/../Fixture/SomeSkippedPathToFile/any.txt', + // elements FifthElement::class, SixthSense::class, diff --git a/packages/Skipper/Matcher/FileInfoMatcher.php b/packages/Skipper/Matcher/FileInfoMatcher.php index 6462d4cf7fa..eb504bfad94 100644 --- a/packages/Skipper/Matcher/FileInfoMatcher.php +++ b/packages/Skipper/Matcher/FileInfoMatcher.php @@ -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 ) { } @@ -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); } } diff --git a/packages/Skipper/RealpathMatcher.php b/packages/Skipper/RealpathMatcher.php new file mode 100644 index 00000000000..161486681dc --- /dev/null +++ b/packages/Skipper/RealpathMatcher.php @@ -0,0 +1,42 @@ +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); + } +} diff --git a/packages/Skipper/Skipper/Skipper.php b/packages/Skipper/Skipper/Skipper.php index 81c2d75a5d1..e3d22bed5ed 100644 --- a/packages/Skipper/Skipper/Skipper.php +++ b/packages/Skipper/Skipper/Skipper.php @@ -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; diff --git a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php index 798c2e00392..d9131cdcc5a 100644 --- a/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php +++ b/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php @@ -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(); } }