[TASK] Take glob pattern for files into account (#22)

- Closes rectorphp/rector#6376
This commit is contained in:
Sebastian Schreiber 2021-05-12 12:44:28 +02:00 committed by GitHub
parent b3b001a445
commit 1480719b61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 8 deletions

View File

@ -9,6 +9,7 @@ use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Symplify\SmartFileSystem\FileSystemFilter;
use Symplify\SmartFileSystem\FileSystemGuard;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -55,8 +56,10 @@ final class FilesFinder
return $this->fileInfosBySourceAndSuffixes[$cacheKey];
}
$files = $this->fileSystemFilter->filterFiles($source);
$directories = $this->fileSystemFilter->filterDirectories($source);
$filesAndDirectories = $this->filesystemTweaker->resolveWithFnmatch($source);
$files = $this->fileSystemFilter->filterFiles($filesAndDirectories);
$directories = $this->fileSystemFilter->filterDirectories($filesAndDirectories);
$smartFileInfos = [];
foreach ($files as $file) {
@ -78,11 +81,6 @@ final class FilesFinder
return [];
}
$absoluteDirectories = $this->filesystemTweaker->resolveDirectoriesWithFnmatch($directories);
if ($absoluteDirectories === []) {
return [];
}
$suffixesPattern = $this->normalizeSuffixesToPattern($suffixes);
$finder = Finder::create()
@ -90,7 +88,7 @@ final class FilesFinder
->files()
// skip empty files
->size('> 0')
->in($absoluteDirectories)
->in($directories)
->name($suffixesPattern)
->sortByName();

View File

@ -55,4 +55,48 @@ final class FilesystemTweaker
return $foundDirectories;
}
/**
* This will turn paths like "src/Symfony/Component/*\/Tests" to existing directory paths
*
* @param string[] $paths
*
* @return string[]
*/
public function resolveWithFnmatch(array $paths): array
{
$absolutePathsFound = [];
foreach ($paths as $path) {
if (Strings::contains($path, '*')) {
$foundPaths = $this->foundInGlob($path);
$absolutePathsFound = array_merge($absolutePathsFound, $foundPaths);
} else {
$absolutePathsFound[] = $path;
}
}
return $absolutePathsFound;
}
/**
* @return string[]
*/
private function foundInGlob(string $path): array
{
$foundPaths = [];
foreach ((array) glob($path) as $foundPath) {
if (! is_string($foundPath)) {
continue;
}
if (! file_exists($foundPath)) {
continue;
}
$foundPaths[] = $foundPath;
}
return $foundPaths;
}
}

View File

@ -59,4 +59,20 @@ final class FilesFinderTest extends AbstractTestCase
sort($expectedFoundFileNames);
$this->assertSame($expectedFoundFileNames, $foundFileNames);
}
public function testDirectoriesWithGlobPattern(): void
{
$foundDirectories = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source/folder*/*'], []);
$this->assertCount(2, $foundDirectories);
}
public function testFilesWithGlobPattern(): void
{
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source/**/foo.txt'], ['txt']);
$this->assertCount(2, $foundFiles);
/** @var SmartFileInfo $foundFile */
$foundFile = array_pop($foundFiles);
$this->assertSame('foo.txt', $foundFile->getBasename());
}
}