Passed
Pull Request — main (#285)
by Jesús
06:19 queued 03:18
created

AllAppModulesFinder::createAppModule()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.0071

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
crap 7.0071
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\AllAppModules;
6
7
use Gacela\Framework\AbstractFacade;
8
use OuterIterator;
9
use ReflectionClass;
10
use SplFileInfo;
11
12
final class AllAppModulesFinder
13
{
14 4
    public function __construct(
15
        private OuterIterator $fileIterator,
16
    ) {
17 4
    }
18
19
    /**
20
     * @return list<AppModule>
21
     */
22 4
    public function findAllAppModules(?string $filter): array
23
    {
24 4
        $result = [];
25
26
        /** @var SplFileInfo $fileInfo */
27 4
        foreach ($this->fileIterator as $fileInfo) {
28 4
            $appModule = $this->createAppModule($fileInfo, $filter);
29 4
            if ($appModule !== null && $this->isFacade($appModule)) {
30 4
                $result[$appModule->facadeClass()] = $appModule;
31
            }
32
        }
33 4
        uksort($result, static fn ($a, $b) => $a <=> $b);
34
35 4
        return array_values($result);
36
    }
37
38 4
    private function createAppModule(SplFileInfo $fileInfo, ?string $filter): ?AppModule
39
    {
40 4
        if (!$fileInfo->isFile()
41 4
            || $fileInfo->getExtension() !== 'php'
42 4
            || str_contains($fileInfo->getRealPath(), 'vendor/')
43
        ) {
44 3
            return null;
45
        }
46
47 4
        $namespace = $this->getNamespace($fileInfo);
48 4
        $className = $this->buildClassName($fileInfo);
49
50 4
        if (isset($filter)) {
51 2
            $filterNamespace = str_replace('/', '\\', $filter);
52 2
            if (!str_contains($namespace, $filterNamespace)) {
53 2
                return null;
54
            }
55
        }
56
57 4
        $fullyQualifiedClassName = sprintf(
58 4
            '%s\\%s',
59 4
            $namespace,
60 4
            $className,
61 4
        );
62
63 4
        if (!class_exists($fullyQualifiedClassName)) {
64
            return null;
65
        }
66
67 4
        return AppModule::fromClass($fullyQualifiedClassName);
68
    }
69
70 4
    private function getNamespace(SplFileInfo $fileInfo): string
71
    {
72 4
        $fileContent = (string)file_get_contents($fileInfo->getRealPath());
73
74 4
        preg_match('#namespace (.*);#', $fileContent, $matches);
75
76 4
        return $matches[1] ?? '';
77
    }
78
79 4
    private function buildClassName(SplFileInfo $fileInfo): string
80
    {
81 4
        $pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename());
82 4
        $filename = end($pieces);
83
84 4
        return substr($filename, 0, strpos($filename, '.') ?: 1);
85
    }
86
87 4
    private function isFacade(AppModule $appModule): bool
88
    {
89 4
        $rc = new ReflectionClass($appModule->facadeClass());
90 4
        $parentClass = $rc->getParentClass();
91
92 4
        return $parentClass
93 4
            && $parentClass->name === AbstractFacade::class;
94
    }
95
}
96