Passed
Push — feature/refactor-DocBlockResol... ( 6c9508 )
by Chema
04:22
created

DocBlockResolverAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 16
c 5
b 0
f 0
dl 0
loc 39
ccs 9
cts 15
cp 0.6
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\DocBlockService\DocBlockServiceResolver;
8
use Gacela\Framework\DocBlockResolver\DocBlockResolvable;
9
10
trait DocBlockResolverAwareTrait
11
{
12
    /** @var array<string,?mixed> */
13
    private array $customServices = [];
14
15
    /**
16
     * @param string $method
17
     * @param array $parameters
18
     *
19
     * @return mixed
20
     */
21 13
    public function __call($method, $parameters = [])
22
    {
23 13
        if (isset($this->customServices[$method])) {
24
            return $this->customServices[$method];
25
        }
26
27 13
        $docBlockResolvable = DocBlockResolvable::fromCaller($this);
28
29
        /** @var class-string $className */
30 13
        $className = $docBlockResolvable->getClassName($method);
31 11
        $resolvableType = $docBlockResolvable->normalizeResolvableType($className);
32
33 11
        $resolved = (new DocBlockServiceResolver($resolvableType))
34 11
            ->resolve($className);
35
36 11
        if ($resolved !== null) {
37 11
            return $resolved;
38
        }
39
40
        if ($docBlockResolvable->hasParentClass()) {
41
            /** @psalm-suppress ParentNotFound, MixedAssignment, UndefinedMethod */
42
            $parentReturn = parent::__call($method, $parameters); // @phpstan-ignore-line
43
            $this->customServices[$method] = $parentReturn;
44
45
            return $parentReturn;
46
        }
47
48
        return null;
49
    }
50
}
51