Test Setup Failed
Pull Request — master (#15)
by
unknown
08:18
created

resolveConstructorParameterValue()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 8.9777
cc 6
nc 7
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace bessonov87\Yii2Monolog\CreationStrategies;
6
7
use ReflectionParameter;
8
use InvalidArgumentException;
9
use Illuminate\Support\Collection;
10
11
class ReflectionStrategy implements CreationStrategyInterface
12
{
13
    /**
14
     * @var \ReflectionClass
15
     */
16
    private $handlerReflectionClass;
17
18
    /**
19
     * Initializes a new ReflectionStrategy.
20
     *
21
     * @param string $class
22
     */
23 22
    public function __construct(string $class)
24
    {
25 22
        $this->handlerReflectionClass = new \ReflectionClass($class);
26 22
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 12 View Code Duplication
    public function getRequiredParameters(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33 12
        if ($this->handlerReflectionClass->getConstructor() === null) {
34 1
            return [];
35
        }
36
37 11
        return $this->getReflectionParametersFromReflectionClass()->reject(
38 11
            function (ReflectionParameter $constructorParameter) {
39 11
                return $constructorParameter->isOptional();
40 11
            }
41 11
        )->map(
42 11
            function (ReflectionParameter $constructorParameter) {
43 7
                return $constructorParameter->name;
44 11
            }
45 11
        )->all();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 18 View Code Duplication
    public function getConstructorParameters(array $config): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53 18
        if ($this->handlerReflectionClass->getConstructor() === null) {
54 1
            return [];
55
        }
56
57 17
        return $this->getReflectionParametersFromReflectionClass()->map(
58 17
            function (ReflectionParameter $constructorParameter) use ($config) {
59 17
                return $this->resolveConstructorParameterValue($constructorParameter, $config);
60 17
            }
61 16
        )->all();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getConfigurationCallable(array $config): callable
68
    {
69 12
        return $config['configure'] ?? function ($instance) {
70 7
            return $instance;
71 12
        };
72
    }
73
74
    /**
75
     * Returns the value for the constructorParameter from given configuration array.
76
     *
77
     * @param ReflectionParameter $constructorParameter
78
     * @param array $config
79
     *
80
     * @return mixed
81
     *
82
     * @throws \yii\di\NotInstantiableException
83
     * @throws \yii\base\InvalidConfigException
84
     * @throws \InvalidArgumentException
85
     */
86 17
    private function resolveConstructorParameterValue(ReflectionParameter $constructorParameter, array $config)
87
    {
88 17
        foreach ($config as $parameterName => $configuredParameterValue) {
89 15
            if ($constructorParameter->name === $parameterName) {
90 15
                return $configuredParameterValue;
91
            }
92
        }
93
94 16
        if ($constructorParameter->isDefaultValueAvailable()) {
95 14
            return $constructorParameter->getDefaultValue();
96
        }
97
98 3
        if ($constructorParameter->hasType() && ! $constructorParameter->getType()->isBuiltin()) {
99 2
            return \Yii::$container->get($constructorParameter->getClass()->name);
100
        }
101
102 1
        throw new InvalidArgumentException(
103 1
            "Expected to find key: '{$constructorParameter->name}' in the given config array but none found."
104
        );
105
    }
106
107
    /**
108
     * Returns the constructor parameters from the reflection class the strategy was initiated with.
109
     *
110
     * @return ReflectionParameter[]|Collection
111
     */
112 18
    private function getReflectionParametersFromReflectionClass()
113
    {
114 18
        return collect($this->handlerReflectionClass->getConstructor()->getParameters());
115
    }
116
}
117