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

DocBlockResolverAwareTrait::hasParentClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 6
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