Skip to content

Commit effe4d3

Browse files
[Skipper] Handle provide direct relative path in Skipper (rectorphp#2921)
Co-authored-by: GitHub Action <[email protected]>
1 parent 2975a1b commit effe4d3

File tree

8 files changed

+67
-4
lines changed

8 files changed

+67
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
any content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
any content

packages-tests/Skipper/Skipper/Skipper/SkipperTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function provideDataShouldSkipFileInfo(): Iterator
4040
{
4141
yield [__DIR__ . '/Fixture/SomeRandom/file.txt', false];
4242
yield [__DIR__ . '/Fixture/SomeSkipped/any.txt', true];
43+
yield ['packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPath/any.txt', true];
44+
yield ['packages-tests/Skipper/Skipper/Skipper/Fixture/SomeSkippedPathToFile/any.txt', true];
4345
}
4446

4547
/**

packages-tests/Skipper/Skipper/Skipper/config/config.php

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// windows like path
1212
'*\SomeSkipped\*',
1313

14+
__DIR__ . '/../Fixture/SomeSkippedPath',
15+
__DIR__ . '/../Fixture/SomeSkippedPathToFile/any.txt',
16+
1417
// elements
1518
FifthElement::class,
1619
SixthSense::class,

packages/Skipper/Matcher/FileInfoMatcher.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
use Rector\Skipper\FileSystem\FnMatchPathNormalizer;
88
use Rector\Skipper\Fnmatcher;
9+
use Rector\Skipper\RealpathMatcher;
910

1011
final class FileInfoMatcher
1112
{
1213
public function __construct(
1314
private readonly FnMatchPathNormalizer $fnMatchPathNormalizer,
14-
private readonly Fnmatcher $fnmatcher
15+
private readonly Fnmatcher $fnmatcher,
16+
private readonly RealpathMatcher $realpathMatcher
1517
) {
1618
}
1719

@@ -52,6 +54,10 @@ private function doesFileMatchPattern(string $filePath, string $ignoredPath): bo
5254
return true;
5355
}
5456

55-
return $this->fnmatcher->match($ignoredPath, $filePath);
57+
if ($this->fnmatcher->match($ignoredPath, $filePath)) {
58+
return true;
59+
}
60+
61+
return $this->realpathMatcher->match($ignoredPath, $filePath);
5662
}
5763
}

packages/Skipper/RealpathMatcher.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Skipper;
6+
7+
final class RealpathMatcher
8+
{
9+
public function match(string $matchingPath, string $filePath): bool
10+
{
11+
$realPathMatchingPath = realpath($matchingPath);
12+
$realpathFilePath = realpath($filePath);
13+
14+
if (! is_string($realPathMatchingPath)) {
15+
return false;
16+
}
17+
18+
if (! is_string($realpathFilePath)) {
19+
return false;
20+
}
21+
22+
$normalizedMatchingPath = $this->normalizePath($realPathMatchingPath);
23+
$normalizedFilePath = $this->normalizePath($realpathFilePath);
24+
25+
// skip define direct path
26+
if (is_file($normalizedMatchingPath)) {
27+
return $normalizedMatchingPath === $normalizedFilePath;
28+
}
29+
30+
// ensure add / suffix to ensure no same prefix directory
31+
if (is_dir($normalizedMatchingPath)) {
32+
$normalizedMatchingPath = rtrim($normalizedMatchingPath, '/') . '/';
33+
}
34+
35+
return str_starts_with($normalizedFilePath, $normalizedMatchingPath);
36+
}
37+
38+
private function normalizePath(string $path): string
39+
{
40+
return \str_replace('\\', '/', $path);
41+
}
42+
}

packages/Skipper/Skipper/Skipper.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function shouldSkipElementAndFilePath(string | object $element, string $f
4242
continue;
4343
}
4444

45-
return $skipVoter->shouldSkip($element, $filePath);
45+
if (! $skipVoter->shouldSkip($element, $filePath)) {
46+
continue;
47+
}
48+
49+
return true;
4650
}
4751

4852
return false;

rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ private function resolveOriginalPositions(FuncCall $funcCall): array
500500
private function shouldSkip(FuncCall $funcCall): bool
501501
{
502502
$functionNames = array_keys(self::ARG_POSITION_NAME_NULL_TO_STRICT_STRING);
503-
return ! $this->nodeNameResolver->isNames($funcCall, $functionNames) || $funcCall->isFirstClassCallable();
503+
if (! $this->nodeNameResolver->isNames($funcCall, $functionNames)) {
504+
return true;
505+
}
506+
507+
return $funcCall->isFirstClassCallable();
504508
}
505509
}

0 commit comments

Comments
 (0)