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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
"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",
"symfony/finder": "^8.1",
"webmozart/assert": "^2.4"
},
"require-dev": {
Expand Down
27 changes: 12 additions & 15 deletions src/Command/NamespaceToPSR4Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
* @see \Rector\SwissKnife\Tests\Command\NamespaceToPSR4CommandTest
Expand All @@ -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 . ';';
Expand Down Expand Up @@ -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 FileFinder::find([$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 === '') {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +20,6 @@
use Rector\SwissKnife\ValueObject\ClassConstantFetch\CurrentClassConstantFetch;
use Rector\SwissKnife\ValueObject\VisibilityChangeStats;
use Rector\SwissKnife\YAML\YamlConfigConstantExtractor;
use Symfony\Component\Finder\SplFileInfo;

final readonly class PrivatizeConstantsCommand implements CommandInterface
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public function run(
* @param ClassConstantFetchInterface[] $classConstantFetches
*/
private function processFileInfo(
SplFileInfo $phpFileInfo,
FileInfo $phpFileInfo,
array $classConstantFetches,
bool $dryRun
): VisibilityChangeStats {
Expand Down
6 changes: 3 additions & 3 deletions src/EntityClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Rector\SwissKnife;

use Entropy\FileSystem\FileInfo;
use Nette\Utils\Strings;
use PhpParser\NodeTraverser;
use Rector\SwissKnife\Finder\FilesFinder;
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Rector\SwissKnife\PhpParser\CachedPhpParser;
use Rector\SwissKnife\PhpParser\NodeTraverserFactory;
use Rector\SwissKnife\PhpParser\NodeVisitor\EntityClassNameCollectingNodeVisitor;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\Assert\Assert;

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public function resolve(array $paths, ?callable $progressClosure = null): array
}

/**
* @param SplFileInfo[] $phpFileInfos
* @param FileInfo[] $phpFileInfos
*/
private function traverseFileInfos(
array $phpFileInfos,
Expand Down Expand Up @@ -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);

Expand Down
114 changes: 54 additions & 60 deletions src/Finder/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,65 @@

namespace Rector\SwissKnife\Finder;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Entropy\FileSystem\FileFinder;
use Entropy\FileSystem\FileInfo;
use Webmozart\Assert\Assert;

/**
* @see \Rector\SwissKnife\Tests\Finder\FilesFinderTest
*/
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 FileFinder::find($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 FileFinder::find(
$directories,
static fn (FileInfo $fileInfo): bool => $fileInfo->getExtension() === 'twig'
);
}

/**
* @param string[] $sources
* @return SplFileInfo[]
* @return FileInfo[]
*/
public static function findJsonFiles(array $sources): array
{
Expand All @@ -87,40 +71,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 = FileFinder::find(
$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 FileFinder::find(
$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;
}
}
Loading
Loading