Skip to content

Commit

Permalink
♻️: use symfony finder
Browse files Browse the repository at this point in the history
  • Loading branch information
FaSe22 committed Dec 24, 2024
1 parent 6ae9134 commit d42d900
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Laravel/LaravelClassExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use Symfony\Component\Finder\Finder;

class LaravelClassExtractor
{
Expand All @@ -35,15 +36,19 @@ public function extractFromDirectory(string $directory): array

private function findPhpFiles(string $directory): array
{
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory)
);
$iterator = Finder::create()
->ignoreDotFiles(true)
->ignoreVCS(true)
->ignoreUnreadableDirs()
->ignoreVCSIgnored(true)
->in($directory)
->filter(fn ($el) => $el->getExtension() === 'php')
->getIterator();

$files = [];

foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$files[] = $file->getPathname();
}
$files[] = $file;
}

return $files;
Expand All @@ -58,12 +63,12 @@ private function analyzeFiles(array $files): array
$ast = $this->parser->parse($code);

$namespace = $this->nodeFinder->findFirst($ast, fn (Node $node) => $node instanceof Namespace_);
if (! $namespace) {
if (!$namespace) {
continue;
}

$namespaceName = $this->normalizeNamespace($namespace->name->toString());
if (! isset($namespaces[$namespaceName])) {
if (!isset($namespaces[$namespaceName])) {
$namespaces[$namespaceName] = [];
}

Expand All @@ -73,7 +78,7 @@ private function analyzeFiles(array $files): array
}
}

return array_filter($namespaces, fn ($classes) => ! empty($classes));
return array_filter($namespaces, fn ($classes) => !empty($classes));
}

private function normalizeNamespace(string $namespace): string
Expand Down Expand Up @@ -144,7 +149,7 @@ private function extractParameters(ClassMethod $method): array
private function getTypeName($type): string
{
if ($type instanceof Node\NullableType) {
return '?'.$this->getTypeName($type->type);
return '?' . $this->getTypeName($type->type);
}

if ($type instanceof Node\UnionType) {
Expand Down

0 comments on commit d42d900

Please sign in to comment.