From ecc617696e165dcd7f1383ab0144cd913507e772 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 17 Sep 2026 23:22:12 +0200 Subject: [PATCH 1/2] Drop symfony/finder, replace with custom FileInfo value object and FileScanner --- composer.json | 1 - src/Command/NamespaceToPSR4Command.php | 27 ++--- src/Command/PrivatizeConstantsCommand.php | 4 +- src/EntityClassResolver.php | 6 +- src/Finder/FileScanner.php | 78 ++++++++++++ src/Finder/FilesFinder.php | 113 ++++++++---------- src/Finder/PhpFilesFinder.php | 64 +++++----- src/Finder/TraitFilesFinder.php | 50 ++++---- src/MockedClassResolver.php | 4 +- src/ParentClassResolver.php | 6 +- .../Finder/ClassConstantFetchFinder.php | 4 +- .../FileSystem/TestsDirectoryResolver.php | 47 ++++---- src/ValueObject/FileInfo.php | 34 ++++++ 13 files changed, 263 insertions(+), 175 deletions(-) create mode 100644 src/Finder/FileScanner.php create mode 100644 src/ValueObject/FileInfo.php diff --git a/composer.json b/composer.json index 96657ec4f..305c6bd11 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,6 @@ "nette/robot-loader": "^4.1", "nette/utils": "^4.1", "nikic/php-parser": "^5.9", - "symfony/finder": "^8.1", "webmozart/assert": "^2.4" }, "require-dev": { diff --git a/src/Command/NamespaceToPSR4Command.php b/src/Command/NamespaceToPSR4Command.php index 0d538cd3e..05bfd0049 100644 --- a/src/Command/NamespaceToPSR4Command.php +++ b/src/Command/NamespaceToPSR4Command.php @@ -9,8 +9,8 @@ use Entropy\Console\Output\OutputPrinter; use Nette\Utils\FileSystem; use Nette\Utils\Strings; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\Finder\FileScanner; +use Rector\SwissKnife\ValueObject\FileInfo; /** * @see \Rector\SwissKnife\Tests\Command\NamespaceToPSR4CommandTest @@ -37,7 +37,6 @@ public function run(string $path, string $namespaceRoot): int $changedFilesCount = 0; - /** @var SplFileInfo $fileInfo */ foreach ($fileInfos as $fileInfo) { $expectedNamespace = $this->resolveExpectedNamespace($namespaceRoot, $fileInfo); $expectedNamespaceLine = 'namespace ' . $expectedNamespace . ';'; @@ -88,23 +87,21 @@ public function getDescription(): string } /** - * @return SplFileInfo[] + * @return FileInfo[] */ private function findFilesInPath(string $path): array { - $finder = Finder::create() - ->files() - ->in([$path]) - ->name('*.php') - ->sortByName() - ->filter(static fn (SplFileInfo $fileInfo): bool => - // filter classes - str_contains($fileInfo->getContents(), 'class ')); - - return iterator_to_array($finder->getIterator()); + return FileScanner::scan([$path], static function (FileInfo $fileInfo): bool { + if ($fileInfo->getExtension() !== 'php') { + return false; + } + + // filter classes + return str_contains($fileInfo->getContents(), 'class '); + }); } - private function resolveExpectedNamespace(string $namespaceRoot, SplFileInfo $fileInfo): string + private function resolveExpectedNamespace(string $namespaceRoot, FileInfo $fileInfo): string { $relativePathNamespace = str_replace('/', '\\', $fileInfo->getRelativePath()); if ($relativePathNamespace === '') { diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index e53d911b6..65289d4bf 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -17,9 +17,9 @@ use Rector\SwissKnife\Twig\TwigTemplateConstantExtractor; use Rector\SwissKnife\ValueObject\ClassConstant; use Rector\SwissKnife\ValueObject\ClassConstantFetch\CurrentClassConstantFetch; +use Rector\SwissKnife\ValueObject\FileInfo; use Rector\SwissKnife\ValueObject\VisibilityChangeStats; use Rector\SwissKnife\YAML\YamlConfigConstantExtractor; -use Symfony\Component\Finder\SplFileInfo; final readonly class PrivatizeConstantsCommand implements CommandInterface { @@ -126,7 +126,7 @@ public function run( * @param ClassConstantFetchInterface[] $classConstantFetches */ private function processFileInfo( - SplFileInfo $phpFileInfo, + FileInfo $phpFileInfo, array $classConstantFetches, bool $dryRun ): VisibilityChangeStats { diff --git a/src/EntityClassResolver.php b/src/EntityClassResolver.php index 042067a08..fbbc40167 100644 --- a/src/EntityClassResolver.php +++ b/src/EntityClassResolver.php @@ -11,7 +11,7 @@ use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\EntityClassNameCollectingNodeVisitor; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; /** @@ -57,7 +57,7 @@ public function resolve(array $paths, ?callable $progressClosure = null): array } /** - * @param SplFileInfo[] $phpFileInfos + * @param FileInfo[] $phpFileInfos */ private function traverseFileInfos( array $phpFileInfos, @@ -85,7 +85,7 @@ private function resolveYamlEntityClassNames(array $paths): array $yamlEntityClassNames = []; - /** @var SplFileInfo $yamlFileInfo */ + /** @var FileInfo $yamlFileInfo */ foreach ($yamlFileInfos as $yamlFileInfo) { $matches = Strings::matchAll($yamlFileInfo->getContents(), self::YAML_ENTITY_CLASS_NAME_REGEX); diff --git a/src/Finder/FileScanner.php b/src/Finder/FileScanner.php new file mode 100644 index 000000000..705347e94 --- /dev/null +++ b/src/Finder/FileScanner.php @@ -0,0 +1,78 @@ +isFile()) { + continue; + } + + $filePath = (string) $splFileInfo->getRealPath(); + $relativePathname = self::resolveRelativePathname($filePath, $baseDirectory); + $relativeDirectory = dirname($relativePathname); + + $fileInfo = new FileInfo( + $filePath, + $relativeDirectory === '.' ? '' : $relativeDirectory, + $relativePathname + ); + + if (! $filter($fileInfo)) { + continue; + } + + $fileInfos[$filePath] = $fileInfo; + } + } + + ksort($fileInfos); + + return array_values($fileInfos); + } + + private static function resolveRelativePathname(string $filePath, string $baseDirectory): string + { + $filePath = str_replace('\\', '/', $filePath); + + if ($baseDirectory !== '' && str_starts_with($filePath, $baseDirectory . '/')) { + return substr($filePath, strlen($baseDirectory) + 1); + } + + return $filePath; + } +} diff --git a/src/Finder/FilesFinder.php b/src/Finder/FilesFinder.php index a17e90c8d..32fbd092d 100644 --- a/src/Finder/FilesFinder.php +++ b/src/Finder/FilesFinder.php @@ -4,8 +4,7 @@ namespace Rector\SwissKnife\Finder; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; /** @@ -13,72 +12,56 @@ */ final class FilesFinder { + /** + * @var string[] + */ + private const array SKIPPED_DIRECTORIES = ['node_modules', 'vendor', 'var/cache']; + /** * @param string[] $sources * @param string[] $excludedPaths - * @return SplFileInfo[] + * @return FileInfo[] */ public static function find(array $sources, array $excludedPaths = []): array { - $paths = []; + Assert::allString($excludedPaths); + + $directories = []; foreach ($sources as $source) { - $paths[] = getcwd() . DIRECTORY_SEPARATOR . $source; + $directories[] = getcwd() . DIRECTORY_SEPARATOR . $source; } - $finder = Finder::create() - ->files() - ->in($paths) + return FileScanner::scan($directories, static function (FileInfo $fileInfo) use ($excludedPaths): bool { // not our code - ->notPath('node_modules') - ->notPath('vendor') - ->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; - } + $normalizedRelativePath = '/' . str_replace('\\', '/', $fileInfo->getRelativePathname()); + foreach (self::SKIPPED_DIRECTORIES as $skippedDirectory) { + if (str_contains($normalizedRelativePath, '/' . $skippedDirectory . '/')) { + return false; } + } - return true; - }); - } - - return iterator_to_array($finder->getIterator()); + return ! self::isExcluded((string) $fileInfo->getRealPath(), $excludedPaths); + }); } /** * @param string[] $directories - * @return SplFileInfo[] + * @return FileInfo[] */ public static function findTwigFiles(array $directories): array { Assert::allString($directories); Assert::allDirectory($directories); - $twigFinder = Finder::create() - ->files() - ->name('*.twig') - ->in($directories) - ->sortByName(); - - return iterator_to_array($twigFinder->getIterator()); + return FileScanner::scan( + $directories, + static fn (FileInfo $fileInfo): bool => $fileInfo->getExtension() === 'twig' + ); } /** * @param string[] $sources - * @return SplFileInfo[] + * @return FileInfo[] */ public static function findJsonFiles(array $sources): array { @@ -87,40 +70,50 @@ public static function findJsonFiles(array $sources): array foreach ($sources as $source) { if (is_file($source)) { - $jsonFileInfos[] = new SplFileInfo($source, '', $source); + $jsonFileInfos[] = new FileInfo($source, '', $source); } else { $directories[] = $source; } } - $jsonFileFinder = Finder::create() - ->files() - ->in($directories) - ->name('*.json') - ->sortByName(); - - foreach ($jsonFileFinder->getIterator() as $fileInfo) { - $jsonFileInfos[] = $fileInfo; - } + $scannedFileInfos = FileScanner::scan( + $directories, + static fn (FileInfo $fileInfo): bool => $fileInfo->getExtension() === 'json' + ); - return $jsonFileInfos; + return array_merge($jsonFileInfos, $scannedFileInfos); } /** * @param string[] $paths - * @return SplFileInfo[] + * @return FileInfo[] */ public static function findYamlFiles(array $paths): array { Assert::allString($paths); Assert::allFileExists($paths); - $finder = Finder::create() - ->files() - ->in($paths) - ->name('*.yml') - ->name('*.yaml'); + return FileScanner::scan( + $paths, + static fn (FileInfo $fileInfo): bool => in_array($fileInfo->getExtension(), ['yml', 'yaml'], true) + ); + } + + /** + * @param string[] $excludedPaths + */ + private static function isExcluded(string $realPath, array $excludedPaths): bool + { + foreach ($excludedPaths as $excludedPath) { + if (str_contains($realPath, $excludedPath)) { + return true; + } + + if (str_contains($excludedPath, '*') && fnmatch($excludedPath, $realPath)) { + return true; + } + } - return iterator_to_array($finder); + return false; } } diff --git a/src/Finder/PhpFilesFinder.php b/src/Finder/PhpFilesFinder.php index c5f2f83f5..ed7c52116 100644 --- a/src/Finder/PhpFilesFinder.php +++ b/src/Finder/PhpFilesFinder.php @@ -4,8 +4,7 @@ namespace Rector\SwissKnife\Finder; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; /** @@ -14,28 +13,22 @@ final class PhpFilesFinder { /** - * @param string[] $paths - * @param string[] $excludedPaths - * - * @return SplFileInfo[] + * @var string[] */ - public static function find(array $paths, array $excludedPaths = []): array - { - $finder = self::createFinderForPathsAndExcludedPaths($paths, $excludedPaths); - - return iterator_to_array($finder->getIterator()); - } + private const array SKIPPED_DIRECTORIES = ['vendor', 'var', 'data-fixtures', 'node_modules']; /** * @param string[] $paths * @param string[] $excludedPaths + * + * @return FileInfo[] */ - private static function createFinderForPathsAndExcludedPaths(array $paths, array $excludedPaths): Finder + public static function find(array $paths, array $excludedPaths = []): array { Assert::allString($paths); Assert::allFileExists($paths); - Assert::allString($excludedPaths); + $excludedFileNames = []; foreach ($excludedPaths as $excludedPath) { if (! str_contains($excludedPath, '*')) { @@ -45,28 +38,31 @@ private static function createFinderForPathsAndExcludedPaths(array $paths, array Assert::allFileExists($excludedFileNames); - return Finder::create() - ->files() - ->in($paths) - ->name('*.php') - ->notPath('vendor') - ->notPath('var') - ->notPath('data-fixtures') - ->notPath('node_modules') - // exclude paths, as notPaths() does no work - ->filter(static function (SplFileInfo $splFileInfo) use ($excludedPaths): bool { - foreach ($excludedPaths as $excludedPath) { - $realpath = $splFileInfo->getRealPath(); - if (str_contains($realpath, $excludedPath)) { - return false; - } + return FileScanner::scan($paths, static function (FileInfo $fileInfo) use ($excludedPaths): bool { + if ($fileInfo->getExtension() !== 'php') { + return false; + } - if (str_contains($excludedPath, '*') && \fnmatch($excludedPath, $realpath)) { - return false; - } + $normalizedRelativePath = '/' . str_replace('\\', '/', $fileInfo->getRelativePathname()); + foreach (self::SKIPPED_DIRECTORIES as $skippedDirectory) { + if (str_contains($normalizedRelativePath, '/' . $skippedDirectory . '/')) { + return false; } + } + + $realPath = (string) $fileInfo->getRealPath(); + + foreach ($excludedPaths as $excludedPath) { + if (str_contains($realPath, $excludedPath)) { + return false; + } + + if (str_contains($excludedPath, '*') && fnmatch($excludedPath, $realPath)) { + return false; + } + } - return true; - }); + return true; + }); } } diff --git a/src/Finder/TraitFilesFinder.php b/src/Finder/TraitFilesFinder.php index ba3b473be..5f380f38a 100644 --- a/src/Finder/TraitFilesFinder.php +++ b/src/Finder/TraitFilesFinder.php @@ -5,53 +5,47 @@ namespace Rector\SwissKnife\Finder; use Nette\Utils\Strings; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; final class TraitFilesFinder { /** * @param string[] $directories - * @return SplFileInfo[] + * @return FileInfo[] */ public function findTraitUsages(array $directories): array { Assert::allString($directories); - $traitUsersFinder = Finder::create() - ->files() - ->in($directories) - ->name('*.php') - ->sortByName() - ->filter(function (SplFileInfo $fileInfo): bool { - $fileContent = $fileInfo->getContents(); - return str_contains($fileContent, ' use '); - }); - - return iterator_to_array($traitUsersFinder->getIterator()); + return FileScanner::scan($directories, static function (FileInfo $fileInfo): bool { + if ($fileInfo->getExtension() !== 'php') { + return false; + } + + return str_contains($fileInfo->getContents(), ' use '); + }); } /** * @param string[] $directories - * @return array + * @return FileInfo[] */ public function find(array $directories): array { Assert::allString($directories); - $traitFinder = Finder::create() - ->files() - ->in($directories) - ->name('*.php') - ->notPath('Entity') - ->notPath('Document') - ->sortByName() - ->filter(function (SplFileInfo $fileInfo): bool { - $fileContent = $fileInfo->getContents(); - return (bool) Strings::match($fileContent, '#^trait\s#m'); - }); - - return iterator_to_array($traitFinder->getIterator()); + return FileScanner::scan($directories, static function (FileInfo $fileInfo): bool { + if ($fileInfo->getExtension() !== 'php') { + return false; + } + + $normalizedPath = str_replace('\\', '/', (string) $fileInfo->getRealPath()); + if (str_contains($normalizedPath, '/Entity/') || str_contains($normalizedPath, '/Document/')) { + return false; + } + + return (bool) Strings::match($fileInfo->getContents(), '#^trait\s#m'); + }); } } diff --git a/src/MockedClassResolver.php b/src/MockedClassResolver.php index bff644534..4f77eed8b 100644 --- a/src/MockedClassResolver.php +++ b/src/MockedClassResolver.php @@ -9,7 +9,7 @@ use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; final readonly class MockedClassResolver @@ -41,7 +41,7 @@ public function resolve(array $paths, ?callable $progressClosure = null): array } /** - * @param SplFileInfo[] $phpFileInfos + * @param FileInfo[] $phpFileInfos */ private function traverseFileInfos( array $phpFileInfos, diff --git a/src/ParentClassResolver.php b/src/ParentClassResolver.php index 5cecba468..7727fb795 100644 --- a/src/ParentClassResolver.php +++ b/src/ParentClassResolver.php @@ -8,7 +8,7 @@ use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\ParentClassNameCollectingNodeVisitor; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; final readonly class ParentClassResolver { @@ -18,7 +18,7 @@ public function __construct( } /** - * @param SplFileInfo[] $phpFileInfos + * @param FileInfo[] $phpFileInfos * @return string[] */ public function resolve(array $phpFileInfos, callable $progressClosure): array @@ -32,7 +32,7 @@ public function resolve(array $phpFileInfos, callable $progressClosure): array } /** - * @param SplFileInfo[] $phpFileInfos + * @param FileInfo[] $phpFileInfos */ private function traverseFileInfos( array $phpFileInfos, diff --git a/src/PhpParser/Finder/ClassConstantFetchFinder.php b/src/PhpParser/Finder/ClassConstantFetchFinder.php index f19d5d78a..c3d88bffe 100644 --- a/src/PhpParser/Finder/ClassConstantFetchFinder.php +++ b/src/PhpParser/Finder/ClassConstantFetchFinder.php @@ -12,7 +12,7 @@ use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\FindClassConstFetchNodeVisitor; -use Symfony\Component\Finder\SplFileInfo; +use Rector\SwissKnife\ValueObject\FileInfo; /** * @see \Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\ClassConstantFetchFinderTest @@ -26,7 +26,7 @@ public function __construct( } /** - * @param SplFileInfo[] $phpFileInfos + * @param FileInfo[] $phpFileInfos * @return ClassConstantFetchInterface[] */ public function find(array $phpFileInfos, ProgressBar $progressBar, bool $isDebug): array diff --git a/src/SmokeTestgen/FileSystem/TestsDirectoryResolver.php b/src/SmokeTestgen/FileSystem/TestsDirectoryResolver.php index 4a85ac9ee..087096f7d 100644 --- a/src/SmokeTestgen/FileSystem/TestsDirectoryResolver.php +++ b/src/SmokeTestgen/FileSystem/TestsDirectoryResolver.php @@ -4,8 +4,6 @@ namespace Rector\SwissKnife\SmokeTestgen\FileSystem; -use Symfony\Component\Finder\Finder; - final class TestsDirectoryResolver { public function resolveSmokeUnitTestDirectory(string $projectDirectory): string @@ -27,33 +25,32 @@ public function resolveSmokeUnitTestDirectory(string $projectDirectory): string private function resolveUnitTestsDirectory(string $testDirectory): ?string { - // find test directory - $iterator = Finder::create() - ->directories() - ->name('#unit#i') - ->in($testDirectory) - ->depth(0) - ->getIterator(); - - foreach ($iterator as $unitTestDirectory) { - return $unitTestDirectory->getRelativePathname(); - } - - return null; + return $this->findFirstDirectoryByNameRegex($testDirectory, '#unit#i'); } private function resolveTestDirectory(string $projectDirectory): ?string { - // find test directory - $iterator = Finder::create() - ->directories() - ->name('#test#i') - ->in($projectDirectory) - ->depth(0) - ->getIterator(); - - foreach ($iterator as $testDirectory) { - return $testDirectory->getRelativePathname(); + return $this->findFirstDirectoryByNameRegex($projectDirectory, '#test#i'); + } + + private function findFirstDirectoryByNameRegex(string $directory, string $nameRegex): ?string + { + if (! is_dir($directory)) { + return null; + } + + $childDirectories = glob($directory . '/*', GLOB_ONLYDIR); + if ($childDirectories === false) { + return null; + } + + sort($childDirectories); + + foreach ($childDirectories as $childDirectory) { + $directoryName = basename($childDirectory); + if (preg_match($nameRegex, $directoryName) === 1) { + return $directoryName; + } } return null; diff --git a/src/ValueObject/FileInfo.php b/src/ValueObject/FileInfo.php new file mode 100644 index 000000000..3cc369adc --- /dev/null +++ b/src/ValueObject/FileInfo.php @@ -0,0 +1,34 @@ +getPathname()); + } + + public function getRelativePath(): string + { + return $this->relativePath; + } + + public function getRelativePathname(): string + { + return $this->relativePathname; + } +} From 1853a14ac448676011c68b2396a15912686e3dd2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 17 Sep 2026 23:54:57 +0200 Subject: [PATCH 2/2] Reuse Entropy FileFinder instead of local FileScanner --- composer.json | 2 +- src/Command/NamespaceToPSR4Command.php | 6 +- src/Command/PrivatizeConstantsCommand.php | 2 +- src/EntityClassResolver.php | 2 +- src/Finder/FileScanner.php | 78 ------------------- src/Finder/FilesFinder.php | 11 +-- src/Finder/PhpFilesFinder.php | 5 +- src/Finder/TraitFilesFinder.php | 7 +- src/MockedClassResolver.php | 2 +- src/ParentClassResolver.php | 2 +- .../Finder/ClassConstantFetchFinder.php | 2 +- src/ValueObject/FileInfo.php | 34 -------- 12 files changed, 22 insertions(+), 131 deletions(-) delete mode 100644 src/Finder/FileScanner.php delete mode 100644 src/ValueObject/FileInfo.php diff --git a/composer.json b/composer.json index 305c6bd11..7c73052a0 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=8.4", "ext-tokenizer": "*", - "entropy/entropy": "^0.4.13", + "entropy/entropy": "^0.4.14", "nette/robot-loader": "^4.1", "nette/utils": "^4.1", "nikic/php-parser": "^5.9", diff --git a/src/Command/NamespaceToPSR4Command.php b/src/Command/NamespaceToPSR4Command.php index 05bfd0049..c81d85015 100644 --- a/src/Command/NamespaceToPSR4Command.php +++ b/src/Command/NamespaceToPSR4Command.php @@ -7,10 +7,10 @@ use Entropy\Console\Contract\CommandInterface; use Entropy\Console\Enum\ExitCode; use Entropy\Console\Output\OutputPrinter; +use Entropy\FileSystem\FileFinder; +use Entropy\FileSystem\FileInfo; use Nette\Utils\FileSystem; use Nette\Utils\Strings; -use Rector\SwissKnife\Finder\FileScanner; -use Rector\SwissKnife\ValueObject\FileInfo; /** * @see \Rector\SwissKnife\Tests\Command\NamespaceToPSR4CommandTest @@ -91,7 +91,7 @@ public function getDescription(): string */ private function findFilesInPath(string $path): array { - return FileScanner::scan([$path], static function (FileInfo $fileInfo): bool { + return FileFinder::find([$path], static function (FileInfo $fileInfo): bool { if ($fileInfo->getExtension() !== 'php') { return false; } diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index 65289d4bf..2ab181777 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -8,6 +8,7 @@ use Entropy\Console\Enum\ExitCode; use Entropy\Console\Output\OutputPrinter; use Entropy\Console\Output\ProgressBar; +use Entropy\FileSystem\FileInfo; use Nette\Utils\FileSystem; use Nette\Utils\Strings; use Rector\SwissKnife\Contract\ClassConstantFetchInterface; @@ -17,7 +18,6 @@ use Rector\SwissKnife\Twig\TwigTemplateConstantExtractor; use Rector\SwissKnife\ValueObject\ClassConstant; use Rector\SwissKnife\ValueObject\ClassConstantFetch\CurrentClassConstantFetch; -use Rector\SwissKnife\ValueObject\FileInfo; use Rector\SwissKnife\ValueObject\VisibilityChangeStats; use Rector\SwissKnife\YAML\YamlConfigConstantExtractor; diff --git a/src/EntityClassResolver.php b/src/EntityClassResolver.php index fbbc40167..49186f60b 100644 --- a/src/EntityClassResolver.php +++ b/src/EntityClassResolver.php @@ -4,6 +4,7 @@ namespace Rector\SwissKnife; +use Entropy\FileSystem\FileInfo; use Nette\Utils\Strings; use PhpParser\NodeTraverser; use Rector\SwissKnife\Finder\FilesFinder; @@ -11,7 +12,6 @@ use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\EntityClassNameCollectingNodeVisitor; -use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; /** diff --git a/src/Finder/FileScanner.php b/src/Finder/FileScanner.php deleted file mode 100644 index 705347e94..000000000 --- a/src/Finder/FileScanner.php +++ /dev/null @@ -1,78 +0,0 @@ -isFile()) { - continue; - } - - $filePath = (string) $splFileInfo->getRealPath(); - $relativePathname = self::resolveRelativePathname($filePath, $baseDirectory); - $relativeDirectory = dirname($relativePathname); - - $fileInfo = new FileInfo( - $filePath, - $relativeDirectory === '.' ? '' : $relativeDirectory, - $relativePathname - ); - - if (! $filter($fileInfo)) { - continue; - } - - $fileInfos[$filePath] = $fileInfo; - } - } - - ksort($fileInfos); - - return array_values($fileInfos); - } - - private static function resolveRelativePathname(string $filePath, string $baseDirectory): string - { - $filePath = str_replace('\\', '/', $filePath); - - if ($baseDirectory !== '' && str_starts_with($filePath, $baseDirectory . '/')) { - return substr($filePath, strlen($baseDirectory) + 1); - } - - return $filePath; - } -} diff --git a/src/Finder/FilesFinder.php b/src/Finder/FilesFinder.php index 32fbd092d..5290bfe5f 100644 --- a/src/Finder/FilesFinder.php +++ b/src/Finder/FilesFinder.php @@ -4,7 +4,8 @@ namespace Rector\SwissKnife\Finder; -use Rector\SwissKnife\ValueObject\FileInfo; +use Entropy\FileSystem\FileFinder; +use Entropy\FileSystem\FileInfo; use Webmozart\Assert\Assert; /** @@ -31,7 +32,7 @@ public static function find(array $sources, array $excludedPaths = []): array $directories[] = getcwd() . DIRECTORY_SEPARATOR . $source; } - return FileScanner::scan($directories, static function (FileInfo $fileInfo) use ($excludedPaths): bool { + return FileFinder::find($directories, static function (FileInfo $fileInfo) use ($excludedPaths): bool { // not our code $normalizedRelativePath = '/' . str_replace('\\', '/', $fileInfo->getRelativePathname()); foreach (self::SKIPPED_DIRECTORIES as $skippedDirectory) { @@ -53,7 +54,7 @@ public static function findTwigFiles(array $directories): array Assert::allString($directories); Assert::allDirectory($directories); - return FileScanner::scan( + return FileFinder::find( $directories, static fn (FileInfo $fileInfo): bool => $fileInfo->getExtension() === 'twig' ); @@ -76,7 +77,7 @@ public static function findJsonFiles(array $sources): array } } - $scannedFileInfos = FileScanner::scan( + $scannedFileInfos = FileFinder::find( $directories, static fn (FileInfo $fileInfo): bool => $fileInfo->getExtension() === 'json' ); @@ -93,7 +94,7 @@ public static function findYamlFiles(array $paths): array Assert::allString($paths); Assert::allFileExists($paths); - return FileScanner::scan( + return FileFinder::find( $paths, static fn (FileInfo $fileInfo): bool => in_array($fileInfo->getExtension(), ['yml', 'yaml'], true) ); diff --git a/src/Finder/PhpFilesFinder.php b/src/Finder/PhpFilesFinder.php index ed7c52116..07ab6796b 100644 --- a/src/Finder/PhpFilesFinder.php +++ b/src/Finder/PhpFilesFinder.php @@ -4,7 +4,8 @@ namespace Rector\SwissKnife\Finder; -use Rector\SwissKnife\ValueObject\FileInfo; +use Entropy\FileSystem\FileFinder; +use Entropy\FileSystem\FileInfo; use Webmozart\Assert\Assert; /** @@ -38,7 +39,7 @@ public static function find(array $paths, array $excludedPaths = []): array Assert::allFileExists($excludedFileNames); - return FileScanner::scan($paths, static function (FileInfo $fileInfo) use ($excludedPaths): bool { + return FileFinder::find($paths, static function (FileInfo $fileInfo) use ($excludedPaths): bool { if ($fileInfo->getExtension() !== 'php') { return false; } diff --git a/src/Finder/TraitFilesFinder.php b/src/Finder/TraitFilesFinder.php index 5f380f38a..b44b27813 100644 --- a/src/Finder/TraitFilesFinder.php +++ b/src/Finder/TraitFilesFinder.php @@ -4,8 +4,9 @@ namespace Rector\SwissKnife\Finder; +use Entropy\FileSystem\FileFinder; +use Entropy\FileSystem\FileInfo; use Nette\Utils\Strings; -use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; final class TraitFilesFinder @@ -18,7 +19,7 @@ public function findTraitUsages(array $directories): array { Assert::allString($directories); - return FileScanner::scan($directories, static function (FileInfo $fileInfo): bool { + return FileFinder::find($directories, static function (FileInfo $fileInfo): bool { if ($fileInfo->getExtension() !== 'php') { return false; } @@ -35,7 +36,7 @@ public function find(array $directories): array { Assert::allString($directories); - return FileScanner::scan($directories, static function (FileInfo $fileInfo): bool { + return FileFinder::find($directories, static function (FileInfo $fileInfo): bool { if ($fileInfo->getExtension() !== 'php') { return false; } diff --git a/src/MockedClassResolver.php b/src/MockedClassResolver.php index 4f77eed8b..a2195fc15 100644 --- a/src/MockedClassResolver.php +++ b/src/MockedClassResolver.php @@ -4,12 +4,12 @@ namespace Rector\SwissKnife; +use Entropy\FileSystem\FileInfo; use PhpParser\NodeTraverser; use Rector\SwissKnife\Finder\PhpFilesFinder; use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\MockedClassNameCollectingNodeVisitor; -use Rector\SwissKnife\ValueObject\FileInfo; use Webmozart\Assert\Assert; final readonly class MockedClassResolver diff --git a/src/ParentClassResolver.php b/src/ParentClassResolver.php index 7727fb795..70ef7d689 100644 --- a/src/ParentClassResolver.php +++ b/src/ParentClassResolver.php @@ -4,11 +4,11 @@ namespace Rector\SwissKnife; +use Entropy\FileSystem\FileInfo; use PhpParser\NodeTraverser; use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\ParentClassNameCollectingNodeVisitor; -use Rector\SwissKnife\ValueObject\FileInfo; final readonly class ParentClassResolver { diff --git a/src/PhpParser/Finder/ClassConstantFetchFinder.php b/src/PhpParser/Finder/ClassConstantFetchFinder.php index c3d88bffe..b98f64e49 100644 --- a/src/PhpParser/Finder/ClassConstantFetchFinder.php +++ b/src/PhpParser/Finder/ClassConstantFetchFinder.php @@ -6,13 +6,13 @@ use Entropy\Console\Output\OutputPrinter; use Entropy\Console\Output\ProgressBar; +use Entropy\FileSystem\FileInfo; use Rector\SwissKnife\Contract\ClassConstantFetchInterface; use Rector\SwissKnife\Exception\NotImplementedYetException; use Rector\SwissKnife\Exception\ShouldNotHappenException; use Rector\SwissKnife\PhpParser\CachedPhpParser; use Rector\SwissKnife\PhpParser\NodeTraverserFactory; use Rector\SwissKnife\PhpParser\NodeVisitor\FindClassConstFetchNodeVisitor; -use Rector\SwissKnife\ValueObject\FileInfo; /** * @see \Rector\SwissKnife\Tests\PhpParser\ClassConstantFetchFinder\ClassConstantFetchFinderTest diff --git a/src/ValueObject/FileInfo.php b/src/ValueObject/FileInfo.php deleted file mode 100644 index 3cc369adc..000000000 --- a/src/ValueObject/FileInfo.php +++ /dev/null @@ -1,34 +0,0 @@ -getPathname()); - } - - public function getRelativePath(): string - { - return $this->relativePath; - } - - public function getRelativePathname(): string - { - return $this->relativePathname; - } -}