Passed
Push — feature/custom-services-v2 ( f3d61c...0bc47e )
by Chema
03:49
created

getClassFromDocComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\CustomService\CustomServiceResolver;
8
use Gacela\Framework\ClassResolver\CustomService\DocBlockParser;
9
use Gacela\Framework\ClassResolver\CustomService\MissingMethodException;
10
use ReflectionClass;
11
12
use function is_string;
13
14
trait CustomServicesResolverAwareTrait
15
{
16
    /** @var array<string,?object> */
17
    private array $customServices = [];
18
19 1
    public function __call(string $method, array $arguments = []): ?object
20
    {
21 1
        if (!isset($this->customServices[$method])) {
22 1
            $className = $this->getClassFromDocComment($method);
23 1
            $resolvableType = $this->normalizeResolvableType($className);
24
25 1
            $this->customServices[$method] = (new CustomServiceResolver($resolvableType))
26 1
                ->resolve($className);
27
        }
28
29 1
        return $this->customServices[$method];
30
    }
31
32
    /**
33
     * @return class-string
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
34
     */
35 1
    private function getClassFromDocComment(string $method): string
36
    {
37 1
        $reflectionClass = new ReflectionClass(static::class);
38 1
        $docBlock = (string)$reflectionClass->getDocComment();
39
40 1
        $repositoryClass = (new DocBlockParser())->getClassFromMethod($docBlock, $method);
41
42 1
        if (class_exists($repositoryClass)) {
43 1
            return $repositoryClass;
44
        }
45
46
        throw MissingMethodException::missingOverriding($method, static::class);
47
    }
48
49 1
    private function normalizeResolvableType(string $resolvableType): string
50
    {
51
        /** @var list<string> $resolvableTypeParts */
52 1
        $resolvableTypeParts = explode('\\', ltrim($resolvableType, '\\'));
53 1
        $normalizedResolvableType = end($resolvableTypeParts);
54
55 1
        return is_string($normalizedResolvableType)
56 1
            ? $normalizedResolvableType
57 1
            : $resolvableType;
58
    }
59
}
60