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
43 changes: 23 additions & 20 deletions src/Rule/Rules/File/PhpFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Boundwize\StructArmed\File\SkipPathMatcher;
use Boundwize\StructArmed\Util\Path;

use function array_keys;
use function array_unique;
use function array_values;
use function is_dir;
Expand Down Expand Up @@ -66,37 +67,39 @@ public function filesFromScope(
array $scopeFiles,
array $skipPaths = [],
): array {
$sourcePaths = $this->sourcePaths($basePath);
$filesByPath = [];
$skipPathMatcher = SkipPathMatcher::compile($basePath, $skipPaths);
$skipPathMatcher = SkipPathMatcher::compile($basePath, $skipPaths);
$directoryPrefixes = [];

foreach ($this->sourcePaths($basePath) as $sourcePath) {
$directoryPrefixes[] = Path::normalise(Path::resolve($sourcePath, $basePath), canonicalise: true) . '/';
}

foreach ($scopeFiles as $file) {
$file = Path::normalise($file, canonicalise: true);
$files = [];

foreach ($scopeFiles as $scopeFile) {
$scopeFile = Path::normalise($scopeFile, canonicalise: true);

if (! str_ends_with($file, '.php') || $skipPathMatcher->isSkipped($file)) {
if (isset($files[$scopeFile])) {
continue;
}

$filesByPath[$file] = $file;
}

$files = [];
if (! str_ends_with($scopeFile, '.php')) {
continue;
}

foreach ($sourcePaths as $sourcePath) {
$resolvedSourcePath = Path::resolve($sourcePath, $basePath);
$directoryPrefix = Path::normalise($resolvedSourcePath, canonicalise: true) . '/';
if ($skipPathMatcher->isSkipped($scopeFile)) {
continue;
}

foreach ($filesByPath as $file) {
if (! str_starts_with($file, $directoryPrefix)) {
continue;
foreach ($directoryPrefixes as $directoryPrefix) {
if (str_starts_with($scopeFile, $directoryPrefix)) {
$files[$scopeFile] = true;
continue 2;
}

$files[] = $file;
unset($filesByPath[$file]);
}
}

return $files;
return array_keys($files);
}

/** @return list<string> */
Expand Down
14 changes: 14 additions & 0 deletions tests/Rule/File/PhpFileFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,18 @@ public function testEachFinderFiltersScopeUsingItsOwnSourcePaths(): void
(new PhpFileFinder(['custom/']))->filesFromScope($basePath, $scopeFiles),
);
}

public function testIgnoresDuplicateScopeFiles(): void
{
$basePath = $this->makeTemporaryDirectory('structarmed-php-file-finder');
mkdir($basePath . '/src');

$sourceFile = $basePath . '/src/Foo.php';
file_put_contents($sourceFile, '<?php');

$this->assertSame(
[Path::normalise($sourceFile, canonicalise: true)],
(new PhpFileFinder(['src/']))->filesFromScope($basePath, [$sourceFile, $sourceFile]),
);
}
}
Loading