From 4d6e3570e88971bd9d799c654ad09f8eafe4551b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 2 Jun 2026 11:45:38 +0200 Subject: [PATCH] Add --exclude option to check-conflicts command --- README.md | 6 ++ src/Command/CheckConflictsCommand.php | 5 +- src/Finder/FilesFinder.php | 24 +++++++- tests/Finder/FilesFinderFixture/keep.txt | 1 + .../FilesFinderFixture/skip-dir/skipped.txt | 1 + tests/Finder/FilesFinderTest.php | 55 +++++++++++++++++++ 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 tests/Finder/FilesFinderFixture/keep.txt create mode 100644 tests/Finder/FilesFinderFixture/skip-dir/skipped.txt create mode 100644 tests/Finder/FilesFinderTest.php diff --git a/README.md b/README.md index 35e9a9d59..8a50dee48 100644 --- a/README.md +++ b/README.md @@ -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 +``` +
## 2. Detect Commented Code diff --git a/src/Command/CheckConflictsCommand.php b/src/Command/CheckConflictsCommand.php index 75121b749..a7c0a25bb 100644 --- a/src/Command/CheckConflictsCommand.php +++ b/src/Command/CheckConflictsCommand.php @@ -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) { diff --git a/src/Finder/FilesFinder.php b/src/Finder/FilesFinder.php index 401a78784..18b687299 100644 --- a/src/Finder/FilesFinder.php +++ b/src/Finder/FilesFinder.php @@ -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) { @@ -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()); } diff --git a/tests/Finder/FilesFinderFixture/keep.txt b/tests/Finder/FilesFinderFixture/keep.txt new file mode 100644 index 000000000..2fa992c0b --- /dev/null +++ b/tests/Finder/FilesFinderFixture/keep.txt @@ -0,0 +1 @@ +keep diff --git a/tests/Finder/FilesFinderFixture/skip-dir/skipped.txt b/tests/Finder/FilesFinderFixture/skip-dir/skipped.txt new file mode 100644 index 000000000..e32e76dd5 --- /dev/null +++ b/tests/Finder/FilesFinderFixture/skip-dir/skipped.txt @@ -0,0 +1 @@ +skip diff --git a/tests/Finder/FilesFinderTest.php b/tests/Finder/FilesFinderTest.php new file mode 100644 index 000000000..2d2c73a4f --- /dev/null +++ b/tests/Finder/FilesFinderTest.php @@ -0,0 +1,55 @@ +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()); + } +}