Passed
Push — master ( 9ec1b2...596585 )
by Juuso
03:58
created

ReflectionStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leinonen\Yii2Monolog\CreationStrategies;
6
7
use ReflectionParameter;
8
use InvalidArgumentException;
9
10
class ReflectionStrategy implements CreationStrategyInterface
11
{
12
    /**
13
     * @var \ReflectionClass
14
     */
15
    private $handlerReflectionClass;
16
17
    /**
18
     * Initializes a new ReflectionStrategy.
19
     *
20
     * @param string $class
21
     */
22 10
    public function __construct(string $class)
23
    {
24 10
        $this->handlerReflectionClass = new \ReflectionClass($class);
25 10
    }
26
27
    /**
28
     * Returns required parameter names as array.
29
     *
30
     * @return string[]
31
     */
32 2
    public function getRequiredParameters(): array
33
    {
34 2
        if ($this->handlerReflectionClass->getConstructor() === null) {
35 1
            return [];
36
        }
37
38 1
        $requiredParameters = \array_values(
39
            \array_filter(
40 1
                $this->handlerReflectionClass->getConstructor()->getParameters(),
41
                function (ReflectionParameter $constructorParameter) {
42 1
                    return ! $constructorParameter->isOptional();
43 1
                }
44
            )
45
        );
46
47 1
        return \array_map(
48
            function (ReflectionParameter $parameter) {
49 1
                return $parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
50 1
            },
51
            $requiredParameters
52
        );
53
    }
54
55
    /**
56
     * @param array $config
57
     *
58
     * @return array
59
     *
60
     * @throws \yii\di\NotInstantiableException
61
     * @throws \yii\base\InvalidConfigException
62
     * @throws \InvalidArgumentException
63
     */
64 8
    public function getConstructorParameters(array $config): array
65
    {
66 8
        if ($this->handlerReflectionClass->getConstructor() === null) {
67 1
            return [];
68
        }
69
70 7
        return array_map(
71 7
            function (ReflectionParameter $constructorParameter) use ($config) {
72 7
                return $this->resolveConstructorParameterValue($constructorParameter, $config);
73 7
            },
74 7
            $this->handlerReflectionClass->getConstructor()->getParameters()
75
        );
76
    }
77
78
    /**
79
     * Returns the value for the constructorParameter from given configuration array.
80
     *
81
     * @param ReflectionParameter $constructorParameter
82
     * @param array $config
83
     *
84
     * @return mixed
85
     *
86
     * @throws \yii\di\NotInstantiableException
87
     * @throws \yii\base\InvalidConfigException
88
     * @throws \InvalidArgumentException
89
     */
90 7
    private function resolveConstructorParameterValue(ReflectionParameter $constructorParameter, array $config)
91
    {
92 7
        foreach ($config as $parameterName => $configuredParameterValue) {
93 5
            if ($constructorParameter->getName() === $parameterName) {
0 ignored issues
show
Bug introduced by
Consider using $constructorParameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
94 5
                return $configuredParameterValue;
95
            }
96
        }
97
98 6
        if ($constructorParameter->isDefaultValueAvailable()) {
99 4
            return $constructorParameter->getDefaultValue();
100
        }
101
102 3
        if ($constructorParameter->hasType() && ! $constructorParameter->getType()->isBuiltin()) {
103 2
            return \Yii::$container->get($constructorParameter->getClass()->getName());
0 ignored issues
show
Bug introduced by
Consider using $constructorParameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
104
        }
105
106 1
        throw new InvalidArgumentException(
107 1
            "Expected to find key: '{$constructorParameter->getName()}' in the given config array but none found."
0 ignored issues
show
Bug introduced by
Consider using $constructorParameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
108
        );
109
    }
110
}
111