Passed
Push — bugfix/support-windows ( 0e99e7...b5ecf8 )
by Jesús
06:33 queued 02:55
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 5
    public function __construct(
15
        private OuterIterator $fileIterator,
16
    ) {
17 5
    }
18
19
    /**
20
     * @return list<AppModule>
21
     */
22 5
    public function findAllAppModules(string $filter): array
23
    {
24 5
        $result = [];
25
26
        /** @var SplFileInfo $fileInfo */
27 5
        foreach ($this->fileIterator as $fileInfo) {
28 5
            $appModule = $this->createAppModule($fileInfo, $filter);
29 5
            if ($appModule !== null && $this->isFacade($appModule)) {
30 5
                $result[$appModule->facadeClass()] = $appModule;
31
            }
32
        }
33 5
        uksort($result, static fn ($a, $b) => $a <=> $b);
34
35 5
        return array_values($result);
36
    }
37
38 5
    private function createAppModule(SplFileInfo $fileInfo, string $filter): ?AppModule
39
    {
40 5
        if (!$fileInfo->isFile()
41 5
            || $fileInfo->getExtension() !== 'php'
42 5
            || str_contains($fileInfo->getRealPath(), 'vendor/')
43
        ) {
44 3
            return null;
45
        }
46
47 5
        $namespace = $this->getNamespace($fileInfo);
48 5
        $className = $this->buildClassName($fileInfo);
49
50 5
        $fullyQualifiedClassName = sprintf(
51 5
            '%s\\%s',
52 5
            $namespace,
53 5
            $className,
54 5
        );
55
56 5
        if ($filter !== '') {
57 3
            $filterNamespace = str_replace('/', '\\', $filter);
58 3
            if (!str_contains($fullyQualifiedClassName, $filterNamespace)) {
59 3
                return null;
60
            }
61
        }
62
63 5
        if (!class_exists($fullyQualifiedClassName)) {
64
            return null;
65
        }
66
67 5
        return AppModule::fromClass($fullyQualifiedClassName);
68
    }
69
70 5
    private function getNamespace(SplFileInfo $fileInfo): string
71
    {
72 5
        $fileContent = (string)file_get_contents($fileInfo->getRealPath());
73
74 5
        preg_match('#namespace (.*);#', $fileContent, $matches);
75
76 5
        return $matches[1] ?? '';
77
    }
78
79 5
    private function buildClassName(SplFileInfo $fileInfo): string
80
    {
81 5
        $pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename());
82 5
        $filename = end($pieces);
83
84 5
        return substr($filename, 0, strpos($filename, '.') ?: 1);
85
    }
86
87 5
    private function isFacade(AppModule $appModule): bool
88
    {
89 5
        $rc = new ReflectionClass($appModule->facadeClass());
90 5
        $parentClass = $rc->getParentClass();
91
92 5
        return $parentClass
93 5
            && $parentClass->name === AbstractFacade::class;
94
    }
95
}
96