1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace leinonen\Yii2Monolog\CreationStrategies; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use ReflectionParameter; |
9
|
|
|
use InvalidArgumentException; |
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
|
17 |
|
public function __construct(string $class) |
24
|
|
|
{ |
25
|
17 |
|
$this->handlerReflectionClass = new \ReflectionClass($class); |
26
|
17 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
7 |
View Code Duplication |
public function getRequiredParameters(): array |
|
|
|
|
32
|
|
|
{ |
33
|
7 |
|
if ($this->handlerReflectionClass->getConstructor() === null) { |
34
|
1 |
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
return $this->getReflectionParametersFromReflectionClass()->reject( |
38
|
|
|
function (ReflectionParameter $constructorParameter) { |
39
|
6 |
|
return $constructorParameter->isOptional(); |
40
|
6 |
|
} |
41
|
6 |
|
)->map( |
42
|
|
|
function (ReflectionParameter $constructorParameter) { |
43
|
2 |
|
return $constructorParameter->name; |
44
|
6 |
|
} |
45
|
6 |
|
)->all(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
13 |
View Code Duplication |
public function getConstructorParameters(array $config): array |
|
|
|
|
52
|
|
|
{ |
53
|
13 |
|
if ($this->handlerReflectionClass->getConstructor() === null) { |
54
|
1 |
|
return []; |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
return $this->getReflectionParametersFromReflectionClass()->map( |
58
|
|
|
function (ReflectionParameter $constructorParameter) use ($config) { |
59
|
12 |
|
return $this->resolveConstructorParameterValue($constructorParameter, $config); |
60
|
12 |
|
} |
61
|
11 |
|
)->all(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getConfigurationCallable(array $config): callable |
68
|
|
|
{ |
69
|
7 |
|
return $config['configure'] ?? function ($instance) { |
70
|
2 |
|
return $instance; |
71
|
7 |
|
}; |
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
|
12 |
|
private function resolveConstructorParameterValue(ReflectionParameter $constructorParameter, array $config) |
87
|
|
|
{ |
88
|
12 |
|
foreach ($config as $parameterName => $configuredParameterValue) { |
89
|
10 |
|
if ($constructorParameter->name === $parameterName) { |
90
|
10 |
|
return $configuredParameterValue; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
if ($constructorParameter->isDefaultValueAvailable()) { |
95
|
9 |
|
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
|
13 |
|
private function getReflectionParametersFromReflectionClass() |
113
|
|
|
{ |
114
|
13 |
|
return collect($this->handlerReflectionClass->getConstructor()->getParameters()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.