Passed
Push — master ( 54c9d7...129e56 )
by Chema
01:29 queued 13s
created

DocBlockResolverAwareTrait::createCustomServicesCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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\DocBlockResolver;
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
        $docBlockResolver = DocBlockResolver::fromCaller($this);
28 13
        $resolvable = $docBlockResolver->getDocBlockResolvable($method);
29
30 11
        $resolved = (new DocBlockServiceResolver($resolvable->resolvableType()))
31 11
            ->resolve($resolvable->className());
32
33 11
        if ($resolved !== null) {
34 11
            return $resolved;
35
        }
36
37
        if ($docBlockResolver->hasParentCallMethod()) {
38
            /** @psalm-suppress ParentNotFound, MixedAssignment, UndefinedMethod */
39
            $parentReturn = parent::__call($method, $parameters); // @phpstan-ignore-line
40
            $this->customServices[$method] = $parentReturn;
41
42
            return $parentReturn;
43
        }
44
45
        return null;
46
    }
47
}
48