Passed
Pull Request — main (#333)
by Chema
08:07 queued 04:04
created

DocBlockParser::getClassFromMethod()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 33
ccs 9
cts 10
cp 0.9
rs 9.0777
cc 6
nc 9
nop 2
crap 6.0359
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver\DocBlockService;
6
7
use Gacela\Framework\AbstractFactory;
8
9 27
final class DocBlockParser
10
{
11 27
    public function getClassFromMethod(string $docBlock, string $method): string
12 1
    {
13
        if ($docBlock === '') {
14
            return '';
15 26
        }
16
17
        if (strcasecmp(substr(PHP_OS, 0, 3), 'WIN') === 0) {
18
            $docBlock = str_replace("\n", PHP_EOL, $docBlock);
19 26
        }
20 26
21 26
        $lines = array_filter(
22 26
            explode(PHP_EOL, $docBlock),
23
            static fn (string $l): bool => str_contains($l, $method),
24 26
        );
25
26 26
        /** @var array<int, string> $lineSplit */
27
        $lineSplit = explode(' ', (string)reset($lines));
28
29
        $classFromMethod = $lineSplit[3] ?? '';
30
        if ($classFromMethod !== '') {
31
            return $classFromMethod;
32
        }
33
34
        if ($method === 'getFactory') {
35
            $factoryType = $this->parseFacadeTemplate($docBlock);
36
            if ($factoryType !== '') {
37
                return $factoryType;
38
            }
39
40
            return AbstractFactory::class;
41
        }
42
43
        return '';
44
    }
45
46
    private function parseFacadeTemplate(string $docBlock): string
47
    {
48
        if (preg_match('/@extends\s+[^<]*AbstractFacade<\s*([^>\s]+)\s*>/i', $docBlock, $matches) === 1) {
49
            return $matches[1];
50
        }
51
52
        return '';
53
    }
54
}
55