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
2 changes: 1 addition & 1 deletion packages/fractor/src/Application/FractorRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(

public function run(Configuration $configuration): ProcessResult
{
$filePaths = $this->fileFinder->findFiles($configuration->getPaths(), $configuration->getFileExtensions());
$filePaths = $this->fileFinder->findFilesInPaths($configuration->getPaths(), $configuration);

// no files found
if ($filePaths === []) {
Expand Down
4 changes: 3 additions & 1 deletion packages/fractor/src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
public function createFromInput(InputInterface $input): Configuration
{
$dryRun = (bool) $input->getOption(Option::DRY_RUN);
$shouldClearCache = (bool) $input->getOption(Option::CLEAR_CACHE);

$outputFormat = (string) $input->getOption(Option::OUTPUT_FORMAT);
$showProgressBar = $this->shouldShowProgressBar($input, $outputFormat);
Expand All @@ -47,6 +48,7 @@ public function createFromInput(InputInterface $input): Configuration
return new Configuration(
$dryRun,
$showProgressBar,
$shouldClearCache,
$outputFormat,
$fileExtensions,
$paths,
Expand All @@ -68,7 +70,7 @@ public function createForTests(array $paths): Configuration

$fileExtensions = $this->allowedFileExtensionsResolver->resolve();

return new Configuration(false, true, ConsoleOutputFormatter::NAME, $fileExtensions, $paths);
return new Configuration(false, true, false, ConsoleOutputFormatter::NAME, $fileExtensions, $paths);
}

private function shouldShowProgressBar(InputInterface $input, string $outputFormat): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public function __construct(
private bool $dryRun = false,
private bool $showProgressBar = true,
private bool $shouldClearCache = false,
private string $outputFormat = ConsoleOutputFormatter::NAME,
private array $fileExtensions = [],
private array $paths = [],
Expand All @@ -41,6 +42,11 @@ public function shouldShowProgressBar(): bool
return $this->showProgressBar;
}

public function shouldClearCache(): bool
{
return $this->shouldClearCache;
}

public function getOutputFormat(): string
{
return $this->outputFormat;
Expand Down
7 changes: 0 additions & 7 deletions packages/fractor/src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace a9f\Fractor\Console\Command;

use a9f\Fractor\Application\FractorRunner;
use a9f\Fractor\Caching\Detector\ChangedFilesDetector;
use a9f\Fractor\ChangesReporting\Output\ConsoleOutputFormatter;
use a9f\Fractor\Configuration\ConfigInitializer;
use a9f\Fractor\Configuration\ConfigurationFactory;
Expand All @@ -32,7 +31,6 @@ public function __construct(
private readonly FractorRunner $runner,
private readonly ConfigurationFactory $configurationFactory,
private readonly OutputFormatterCollector $outputFormatterCollector,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly ConfigInitializer $configInitializer,
private readonly MemoryLimiter $memoryLimiter,
private readonly ConfigurationRuleFilter $configurationRuleFilter
Expand Down Expand Up @@ -130,11 +128,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
if (! $application instanceof FractorApplication) {
throw new ShouldNotHappenException();
}
// clear cache
$optionClearCache = (bool) $input->getOption(Option::CLEAR_CACHE);
if ($optionClearCache) {
$this->changedFilesDetector->clear();
}
}

/**
Expand Down
26 changes: 21 additions & 5 deletions packages/fractor/src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@

namespace a9f\Fractor\FileSystem;

use a9f\Fractor\Caching\Detector\ChangedFilesDetector;
use a9f\Fractor\Caching\UnchangedFilesFilter;
use a9f\Fractor\Configuration\ValueObject\Configuration;
use Symfony\Component\Finder\Finder;
use Webmozart\Assert\Assert;

final readonly class FilesFinder
{
public function __construct(
private WildcardResolver $wildcardResolver,
private FilesystemTweaker $filesystemTweaker,
private FileAndDirectoryFilter $fileAndDirectoryFilter,
private PathSkipper $pathSkipper,
private UnchangedFilesFilter $unchangedFilesFilter
private UnchangedFilesFilter $unchangedFilesFilter,
private ChangedFilesDetector $changedFilesDetector,
) {
}

/**
* @param list<non-empty-string> $source
* @param string[] $source
* @param string[] $suffixes
* @return string[]
*/
public function findFiles(array $source, array $suffixes, bool $sortByName = true): array
public function findInDirectoriesAndFiles(array $source, array $suffixes, bool $sortByName = true): array
{
Assert::allStringNotEmpty($source, 'Please provide some paths');

$filesAndDirectories = $this->wildcardResolver->resolveAllWildcards($source);
$filesAndDirectories = $this->filesystemTweaker->resolveWithFnmatch($source);

$files = $this->fileAndDirectoryFilter->filterFiles($filesAndDirectories);

Expand All @@ -51,6 +54,19 @@ public function findFiles(array $source, array $suffixes, bool $sortByName = tru
return $this->unchangedFilesFilter->filterFilePaths($filePaths);
}

/**
* @param string[] $paths
* @return string[]
*/
public function findFilesInPaths(array $paths, Configuration $configuration): array
{
if ($configuration->shouldClearCache()) {
$this->changedFilesDetector->clear();
}

return $this->findInDirectoriesAndFiles($paths, $configuration->getFileExtensions(), true);
}

/**
* @param string[] $directories
* @param string[] $suffixes
Expand Down
60 changes: 60 additions & 0 deletions packages/fractor/src/FileSystem/FilesystemTweaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace a9f\Fractor\FileSystem;

final class FilesystemTweaker
{
/**
* 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 (\str_contains($path, '*')) {
$foundPaths = $this->foundInGlob($path);
$absolutePathsFound = $this->appendPaths($foundPaths, $absolutePathsFound);
} else {
$absolutePathsFound = $this->appendPaths([$path], $absolutePathsFound);
}
}

return $absolutePathsFound;
}

/**
* @param string[] $foundPaths
* @param string[] $absolutePathsFound
* @return string[]
*/
private function appendPaths(array $foundPaths, array $absolutePathsFound): array
{
foreach ($foundPaths as $foundPath) {
$foundPath = realpath($foundPath);

if ($foundPath === false) {
continue;
}

$absolutePathsFound[] = $foundPath;
}

return $absolutePathsFound;
}

/**
* @return string[]
*/
private function foundInGlob(string $path): array
{
/** @var string[] $paths */
$paths = (array) glob($path);

return array_filter($paths, file_exists(...));
}
}
40 changes: 0 additions & 40 deletions packages/fractor/src/FileSystem/WildcardResolver.php

This file was deleted.

19 changes: 14 additions & 5 deletions packages/fractor/tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,42 @@ protected function setUp(): void
#[Test]
public function findAllNonEmptyFilesInGivenDirectories(): void
{
self::assertCount(4, $this->subject->findFiles([__DIR__ . '/Fixtures/Source'], []));
self::assertCount(4, $this->subject->findInDirectoriesAndFiles([__DIR__ . '/Fixtures/Source'], []));
}

#[Test]
public function findAllNonEmptyFilesInGivenDirectoriesWithGivenExtensions(): void
{
self::assertCount(2, $this->subject->findFiles([__DIR__ . '/Fixtures/Source'], ['txt', 'json']));
self::assertCount(
2,
$this->subject->findInDirectoriesAndFiles([__DIR__ . '/Fixtures/Source'], ['txt', 'json'])
);
}

#[Test]
public function withFollowingBrokenSymlinks(): void
{
$foundFiles = $this->subject->findFiles([__DIR__ . '/Fixtures/SourceWithBrokenSymlinks'], []);
$foundFiles = $this->subject->findInDirectoriesAndFiles([__DIR__ . '/Fixtures/SourceWithBrokenSymlinks'], []);
self::assertCount(0, $foundFiles);
}

#[Test]
public function directoriesWithGlobPattern(): void
{
$foundDirectories = $this->subject->findFiles([__DIR__ . '/Fixtures/SourceWithSubFolders/folder*/*'], []);
$foundDirectories = $this->subject->findInDirectoriesAndFiles(
[__DIR__ . '/Fixtures/SourceWithSubFolders/folder*/*'],
[]
);
self::assertCount(2, $foundDirectories);
}

#[Test]
public function filesWithGlobPattern(): void
{
$foundFiles = $this->subject->findFiles([__DIR__ . '/Fixtures/SourceWithSubFolders/**/foo.txt'], ['txt']);
$foundFiles = $this->subject->findInDirectoriesAndFiles(
[__DIR__ . '/Fixtures/SourceWithSubFolders/**/foo.txt'],
['txt']
);
self::assertCount(2, $foundFiles);

/** @var string $foundFile */
Expand Down