Skip to content

Commit

Permalink
[TASK] Take glob pattern for files into account (rectorphp#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored May 12, 2021
1 parent b3b001a commit 1480719
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand Down Expand Up @@ -55,8 +56,10 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes): array
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) {
Expand All @@ -78,19 +81,14 @@ private function findInDirectories(array $directories, array $suffixes): array
return [];
}

$absoluteDirectories = $this->filesystemTweaker->resolveDirectoriesWithFnmatch($directories);
if ($absoluteDirectories === []) {
return [];
}

$suffixesPattern = $this->normalizeSuffixesToPattern($suffixes);

$finder = Finder::create()
->followLinks()
->files()
// skip empty files
->size('> 0')
->in($absoluteDirectories)
->in($directories)
->name($suffixesPattern)
->sortByName();

Expand Down
44 changes: 44 additions & 0 deletions src/FileSystem/FilesystemTweaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,48 @@ private function foundDirectoriesInGlob(string $directory): array

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;
}
}
16 changes: 16 additions & 0 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ public function testMultipleSuffixes(): void
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());
}
}
Empty file.
Empty file.
Empty file.

0 comments on commit 1480719

Please sign in to comment.