Passed
Push — master ( 021f1c...1478fc )
by Evgeniy
01:24 queued 11s
created

Reflection::tranform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Reflection\ParamTranfromer;
8
use ReflectionClass;
9
use ReflectionException;
10
use ReflectionMethod;
11
12
class Reflection
13
{
14
    private $instantiable = [];
15
    private $dependencies = [];
16
    private $variadic = [];
17
    /**
18
     * @var ParamTranfromer[]
19
     */
20
    private $transformers;
21
22 21
    public function __construct(ParamTranfromer ...$tranfromers)
23
    {
24 21
        $this->transformers = $tranfromers;
25 21
    }
26
27
    /**
28
     * @param string $name
29
     * @return string[]
30
     * @internal
31
     */
32 15
    public function getDependencies(string $name): array
33
    {
34 15
        if (!array_key_exists($name, $this->dependencies)) {
35 12
            $this->load($name);
36
        }
37 15
        return $this->dependencies[$name];
38
    }
39
40
    /**
41
     * @param string $name
42
     * @return bool
43
     * @internal
44
     */
45 6
    public function isVariadic(string $name): bool
46
    {
47 6
        if (!array_key_exists($name, $this->variadic)) {
48 3
            $this->load($name);
49
        }
50 6
        return $this->variadic[$name];
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return bool
56
     * @internal
57
     */
58 6
    public function isInstantiable(string $name): bool
59
    {
60 6
        if (!array_key_exists($name, $this->instantiable)) {
61 6
            $this->load($name);
62
        }
63 6
        return $this->instantiable[$name];
64
    }
65
66 21
    private function load(string $name): void
67
    {
68
        try {
69 21
            $class = new ReflectionClass($name);
70 18
            $this->instantiable[$name] = $class->isInstantiable();
71 18
            $dependencies = self::getMethodDependencies($class->getConstructor());
72 18
            $this->variadic[$name] = $dependencies[0];
73 18
            $this->dependencies[$name] = $this->tranform($class->getName(), $dependencies[1]);
74 3
        } catch (ReflectionException $exception) {
75 3
            $this->dependencies[$name] = [];
76 3
            $this->instantiable[$name] = false;
77 3
            $this->variadic[$name] = false;
78
        }
79 21
    }
80
81 18
    private static function getMethodDependencies(?ReflectionMethod $method): array
82
    {
83 18
        $variadic = false;
84 18
        $parameters = [];
85 18
        if ($method !== null) {
86 15
            foreach ($method->getParameters() as $parameter) {
87 15
                $variadic = $parameter->isVariadic();
88 15
                $class = $parameter->getClass();
89 15
                $parameters[] = $class && $variadic !== true ? $class->name : $parameter->name;
90
            }
91
        }
92 18
        return [$variadic, $parameters];
93
    }
94
95 18
    private function tranform(string $name, array $params)
96
    {
97 18
        foreach ($this->transformers as $tranfromer) {
98 3
            $params = $tranfromer->transform($name, $params);
99
        }
100 18
        return $params;
101
    }
102
}
103