Skip to content

Commit 6421134

Browse files
authored
Support --skip-file with fnmatch patterns (#99)
* Support `--skip-file` with `fnmatch` patterns * Update PhpFilesFinderTest.php * Update PhpFilesFinder.php * Update PhpFilesFinder.php
1 parent 577f44a commit 6421134

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ No more! Add this command to CI to spot these:
5252
5353
```bash
5454
vendor/bin/swiss-knife check-commented-code <directory>
55-
vendor/bin/swiss-knife check-commented-code packages --line-limit 5
55+
vendor/bin/swiss-knife check-commented-code packages --line-limit 5 --skip-file '*Controller.php'
5656
```
5757
58+
59+
5860
<br>
5961
6062
## 3. Reach full PSR-4
@@ -114,6 +116,12 @@ Do you want to skip file or two?
114116
vendor/bin/swiss-knife finalize-classes src tests --skip-file src/SpecialProxy.php
115117
```
116118
119+
Skip is also support with `fnmatch()` patterns:
120+
121+
```bash
122+
vendor/bin/swiss-knife finalize-classes src tests --skip-file '*Controller.php'
123+
```
124+
117125
<br>
118126
119127
## 5. Privatize local class constants

src/Finder/PhpFilesFinder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ private static function createFinderForPathsAndExcludedPaths(array $paths, array
3333
Assert::allFileExists($paths);
3434

3535
Assert::allString($excludedPaths);
36-
Assert::allFileExists($excludedPaths);
36+
$excludedFileNames = [];
37+
foreach ($excludedPaths as $excludedPath) {
38+
if (! str_contains($excludedPath, '*')) {
39+
$excludedFileNames[] = $excludedPath;
40+
}
41+
}
42+
Assert::allFileExists($excludedFileNames);
3743

3844
return Finder::create()
3945
->files()
@@ -46,7 +52,12 @@ private static function createFinderForPathsAndExcludedPaths(array $paths, array
4652
// exclude paths, as notPaths() does no work
4753
->filter(static function (SplFileInfo $splFileInfo) use ($excludedPaths): bool {
4854
foreach ($excludedPaths as $excludedPath) {
49-
if (str_contains($splFileInfo->getRealPath(), $excludedPath)) {
55+
$realpath = $splFileInfo->getRealPath();
56+
if (str_contains($realpath, $excludedPath)) {
57+
return false;
58+
}
59+
60+
if (str_contains($excludedPath, '*') && \fnmatch($excludedPath, $realpath)) {
5061
return false;
5162
}
5263
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// intentional left empty

tests/Finder/Fixture/AModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// intentional left empty
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// intentional left empty
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\SwissKnife\Tests\Finder;
6+
7+
use Rector\SwissKnife\Tests\AbstractTestCase;
8+
9+
final class PhpFilesFinderTest extends AbstractTestCase
10+
{
11+
public function testExcludeByFnMatch(): void
12+
{
13+
$files = \Rector\SwissKnife\Finder\PhpFilesFinder::find([__DIR__ . '/Fixture'], ['*Controller.php']);
14+
15+
$this->assertSame(1, count($files));
16+
17+
sort($files); // make order predictable across operating systems
18+
$file = array_pop($files);
19+
$this->assertNotNull($file);
20+
$this->assertStringContainsString('AModel.php', $file->getRealPath());
21+
}
22+
23+
public function testExcludeByFnMatch2(): void
24+
{
25+
$files = \Rector\SwissKnife\Finder\PhpFilesFinder::find([__DIR__ . '/Fixture'], ['*Model.php']);
26+
27+
$this->assertSame(2, count($files));
28+
29+
sort($files); // make order predictable across operating systems
30+
$file = array_pop($files);
31+
$this->assertNotNull($file);
32+
$this->assertStringContainsString('BController.php', $file->getRealPath());
33+
34+
$file = array_pop($files);
35+
$this->assertNotNull($file);
36+
$this->assertStringContainsString('AController.php', $file->getRealPath());
37+
}
38+
}

0 commit comments

Comments
 (0)