Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Add this command to CI to spot these:
vendor/bin/swiss-knife check-conflicts .
```

You can skip paths with the `--exclude` option:

```bash
vendor/bin/swiss-knife check-conflicts . --exclude vendor --exclude tests/fixtures
```

<br>

## 2. Detect Commented Code
Expand Down
5 changes: 3 additions & 2 deletions src/Command/CheckConflictsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function __construct(

/**
* @param string[] $sources One or more path to project
* @param string[] $exclude Skip files or directories by path
* @return ExitCode::*
*/
public function run(array $sources): int
public function run(array $sources, array $exclude = []): int
{
$fileInfos = FilesFinder::find($sources);
$fileInfos = FilesFinder::find($sources, $exclude);

$filePaths = [];
foreach ($fileInfos as $fileInfo) {
Expand Down
24 changes: 23 additions & 1 deletion src/Finder/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ final class FilesFinder
{
/**
* @param string[] $sources
* @param string[] $excludedPaths
* @return SplFileInfo[]
*/
public static function find(array $sources): array
public static function find(array $sources, array $excludedPaths = []): array
{
$paths = [];
foreach ($sources as $source) {
Expand All @@ -30,6 +31,27 @@ public static function find(array $sources): array
->notPath('var/cache')
->sortByName();

if ($excludedPaths !== []) {
Assert::allString($excludedPaths);

// exclude paths, as notPath() does not work with absolute paths
$finder->filter(static function (SplFileInfo $splFileInfo) use ($excludedPaths): bool {
$realPath = $splFileInfo->getRealPath();

foreach ($excludedPaths as $excludedPath) {
if (str_contains($realPath, $excludedPath)) {
return false;
}

if (str_contains($excludedPath, '*') && fnmatch($excludedPath, $realPath)) {
return false;
}
}

return true;
});
}

return iterator_to_array($finder->getIterator());
}

Expand Down
1 change: 1 addition & 0 deletions tests/Finder/FilesFinderFixture/keep.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keep
1 change: 1 addition & 0 deletions tests/Finder/FilesFinderFixture/skip-dir/skipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skip
55 changes: 55 additions & 0 deletions tests/Finder/FilesFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\Finder;

use PHPUnit\Framework\TestCase;
use Rector\SwissKnife\Finder\FilesFinder;

final class FilesFinderTest extends TestCase
{
private string $originalCwd;

protected function setUp(): void
{
$originalCwd = getcwd();
$this->originalCwd = $originalCwd === false ? __DIR__ : $originalCwd;

// find() resolves sources relative to the current working directory
chdir(__DIR__);
}

protected function tearDown(): void
{
chdir($this->originalCwd);
}

public function testFindAll(): void
{
$files = FilesFinder::find(['FilesFinderFixture']);
$this->assertCount(2, $files);
}

public function testExcludeByPath(): void
{
$files = FilesFinder::find(['FilesFinderFixture'], ['skip-dir']);

$this->assertCount(1, $files);

$file = array_pop($files);
$this->assertNotNull($file);
$this->assertStringContainsString('keep.txt', $file->getRealPath());
}

public function testExcludeByFnMatch(): void
{
$files = FilesFinder::find(['FilesFinderFixture'], ['*skipped.txt']);

$this->assertCount(1, $files);

$file = array_pop($files);
$this->assertNotNull($file);
$this->assertStringContainsString('keep.txt', $file->getRealPath());
}
}
Loading