1 | <?php |
||
33 | final class ArgumentCompiler implements ArgumentCompilerInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * @var ArgumentFactory |
||
38 | */ |
||
39 | private $argumentFactory; |
||
40 | |||
41 | /** |
||
42 | * @var RequestStack |
||
43 | */ |
||
44 | private $requestStack; |
||
45 | |||
46 | /** |
||
47 | * @var ArgumentContextInterface |
||
48 | */ |
||
49 | private $argumentContext; |
||
50 | |||
51 | /** |
||
52 | * @var ControllerHelperInterface |
||
53 | */ |
||
54 | private $controllerHelper; |
||
55 | |||
56 | 11 | public function __construct( |
|
57 | ArgumentFactory $argumentFactory, |
||
58 | RequestStack $requestStack, |
||
59 | ArgumentContextInterface $argumentContext, |
||
60 | ControllerHelperInterface $controllerHelper |
||
61 | ) { |
||
62 | 11 | $this->argumentFactory = $argumentFactory; |
|
63 | 11 | $this->requestStack = $requestStack; |
|
64 | 11 | $this->argumentContext = $argumentContext; |
|
65 | 11 | $this->controllerHelper = $controllerHelper; |
|
66 | 11 | } |
|
67 | |||
68 | public function understandsArgumentString(string $argumentConfiguration): bool |
||
69 | { |
||
70 | return $this->argumentFactory->understandsString($argumentConfiguration); |
||
71 | } |
||
72 | |||
73 | /** @param array|string $argumentConfiguration */ |
||
74 | public function buildArgument($argumentConfiguration, array $additionalData = array()) |
||
75 | { |
||
76 | foreach ($additionalData as $key => $value) { |
||
77 | $this->argumentContext->set($key, $value); |
||
78 | } |
||
79 | |||
80 | return $this->resolveArgumentConfiguration($argumentConfiguration); |
||
81 | } |
||
82 | |||
83 | 6 | public function buildArguments( |
|
84 | array $argumentsConfiguration, |
||
85 | array $additionalData = array() |
||
86 | ): array { |
||
87 | /** @var array $argumentValues */ |
||
88 | 6 | $argumentValues = array(); |
|
89 | |||
90 | 6 | foreach ($additionalData as $key => $value) { |
|
91 | 3 | $this->argumentContext->set($key, $value); |
|
92 | } |
||
93 | |||
94 | 6 | foreach ($argumentsConfiguration as $key => $argumentConfiguration) { |
|
95 | /** @var array|string $argumentConfiguration */ |
||
96 | |||
97 | 5 | $argumentValues[$key] = $this->resolveArgumentConfiguration($argumentConfiguration); |
|
98 | } |
||
99 | |||
100 | 3 | return $argumentValues; |
|
101 | } |
||
102 | |||
103 | 5 | public function buildCallArguments( |
|
104 | ReflectionFunctionAbstract $methodReflection, |
||
105 | array $argumentsConfiguration, |
||
106 | array $predefinedArguments = array(), |
||
107 | array $additionalData = array() |
||
108 | ): array { |
||
109 | /** @var array<int, mixed> $callArguments */ |
||
110 | 5 | $callArguments = array(); |
|
111 | |||
112 | 5 | foreach ($additionalData as $key => $value) { |
|
113 | 4 | $this->argumentContext->set($key, $value); |
|
114 | } |
||
115 | |||
116 | 5 | foreach ($methodReflection->getParameters() as $index => $parameterReflection) { |
|
117 | /** @var ReflectionParameter $parameterReflection */ |
||
118 | |||
119 | 4 | if (isset($predefinedArguments[$index])) { |
|
120 | 2 | $callArguments[$index] = $predefinedArguments[$index]; |
|
121 | 2 | continue; |
|
122 | } |
||
123 | |||
124 | 4 | $callArguments[$index] = $this->resolveParameterReflection( |
|
125 | 4 | $parameterReflection, |
|
126 | $argumentsConfiguration, |
||
127 | $index |
||
128 | ); |
||
129 | } |
||
130 | |||
131 | 4 | return $callArguments; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param array|string|bool|object|null $argumentConfiguration |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | 8 | private function resolveArgumentConfiguration($argumentConfiguration) |
|
175 | |||
176 | /** |
||
177 | * @return mixed |
||
178 | */ |
||
179 | 4 | private function resolveParameterReflection( |
|
180 | ReflectionParameter $parameterReflection, |
||
181 | array $argumentsConfiguration, |
||
182 | int $index |
||
183 | ) { |
||
184 | try { |
||
185 | /** @var string $parameterName */ |
||
186 | 4 | $parameterName = $parameterReflection->getName(); |
|
187 | |||
188 | /** @var string|null $parameterTypeName */ |
||
189 | 4 | $parameterTypeName = $this->getTypeNameFromReflectionParameter($parameterReflection); |
|
190 | |||
191 | /** @var Request|null $request */ |
||
192 | 4 | $request = $this->requestStack->getCurrentRequest(); |
|
193 | |||
194 | /** @var mixed $result */ |
||
195 | 4 | $result = null; |
|
196 | |||
197 | 4 | if (isset($argumentsConfiguration[$parameterName])) { |
|
198 | 1 | $result = $this->resolveArgumentConfiguration($argumentsConfiguration[$parameterName]); |
|
199 | |||
200 | 3 | } elseif (array_key_exists($index, $argumentsConfiguration)) { |
|
201 | 2 | $result = $this->resolveArgumentConfiguration($argumentsConfiguration[$index]); |
|
202 | |||
203 | 2 | } elseif ($parameterTypeName === Request::class) { |
|
204 | 1 | $result = $request; |
|
205 | |||
206 | 1 | } elseif (is_object($request) && $request->get($parameterName)) { |
|
207 | $result = $request->get($parameterName); |
||
208 | |||
209 | } else { |
||
210 | 1 | $result = $this->getDefaultValueFromParameterReflectionSafely($parameterReflection); |
|
211 | } |
||
212 | |||
213 | 3 | if (!empty($parameterTypeName) && (is_string($result) || is_int($result))) { |
|
214 | if (is_subclass_of($parameterTypeName, ValueObjectInterface::class)) { |
||
215 | $result = call_user_func("{$parameterTypeName}::fromNative", $result); |
||
216 | |||
217 | } elseif (class_exists($parameterTypeName)) { |
||
218 | $result = $this->controllerHelper->findEntity($parameterTypeName, (string)$result); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | 3 | return $result; |
|
223 | |||
224 | 1 | } catch (Exception $exception) { |
|
225 | 1 | throw new Exception(sprintf( |
|
226 | 1 | "While resolving parameter-argument '%s' on call to '%s': %s", |
|
227 | 1 | $parameterReflection->getName(), |
|
228 | 1 | $parameterReflection->getDeclaringFunction()->getName(), |
|
229 | 1 | $exception->getMessage() |
|
230 | 1 | ), 0, $exception); |
|
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 1 | private function getDefaultValueFromParameterReflectionSafely(ReflectionParameter $parameterReflection) |
|
260 | |||
261 | 4 | private function getTypeNameFromReflectionParameter(ReflectionParameter $parameterReflection): ?string |
|
262 | { |
||
263 | /** @var string|null $parameterTypeName */ |
||
264 | 4 | $parameterTypeName = null; |
|
265 | |||
266 | 4 | if ($parameterReflection->hasType()) { |
|
280 | |||
281 | } |
||
282 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.