1 | <?php |
||
30 | final class ArgumentCompiler implements ArgumentCompilerInterface |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * @var ArgumentFactory |
||
35 | */ |
||
36 | private $argumentFactory; |
||
37 | |||
38 | /** |
||
39 | * @var RequestStack |
||
40 | */ |
||
41 | private $requestStack; |
||
42 | |||
43 | /** |
||
44 | * @var ArgumentContextInterface |
||
45 | */ |
||
46 | private $argumentContext; |
||
47 | |||
48 | /** |
||
49 | * @var ControllerHelperInterface |
||
50 | */ |
||
51 | private $controllerHelper; |
||
52 | |||
53 | 11 | public function __construct( |
|
64 | |||
65 | 6 | public function buildArguments( |
|
85 | |||
86 | 5 | public function buildCallArguments( |
|
117 | |||
118 | /** |
||
119 | * @param array|string|bool|null $argumentConfiguration |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | 8 | private function resolveArgumentConfiguration($argumentConfiguration) |
|
124 | { |
||
125 | 8 | Assert::oneOf( |
|
126 | 8 | gettype($argumentConfiguration), |
|
127 | 8 | ['string', 'array', 'NULL', 'boolean', 'object'], |
|
128 | 8 | "Arguments must be defined as string, array, bool, object or null!" |
|
129 | ); |
||
130 | |||
131 | /** @var Argument|null $argument */ |
||
132 | 7 | $argument = null; |
|
|
|||
133 | |||
134 | 7 | if (is_bool($argumentConfiguration) || is_null($argumentConfiguration) || is_object($argumentConfiguration)) { |
|
135 | return $argumentConfiguration; |
||
136 | |||
137 | 7 | } else if ($argumentConfiguration === '') { |
|
138 | return ''; |
||
139 | |||
140 | 7 | } elseif (is_array($argumentConfiguration)) { |
|
141 | 2 | Assert::true($this->argumentFactory->understandsArray($argumentConfiguration), sprintf( |
|
142 | 2 | "Argument '%s' could not be understood!", |
|
143 | 2 | preg_replace("/\s+/is", "", var_export($argumentConfiguration, true)) |
|
144 | )); |
||
145 | |||
146 | 1 | $argument = $this->argumentFactory->createArgumentFromArray($argumentConfiguration); |
|
147 | |||
148 | } else { |
||
149 | 5 | Assert::true($this->argumentFactory->understandsString($argumentConfiguration), sprintf( |
|
150 | 5 | "Argument '%s' could not be understood!", |
|
151 | 5 | $argumentConfiguration |
|
152 | )); |
||
153 | |||
154 | 4 | $argument = $this->argumentFactory->createArgumentFromString(trim($argumentConfiguration)); |
|
155 | } |
||
156 | |||
157 | 5 | return $argument->resolve(); |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return mixed |
||
162 | */ |
||
163 | 4 | private function resolveParameterReflection( |
|
164 | ReflectionParameter $parameterReflection, |
||
165 | array $argumentsConfiguration, |
||
166 | int $index |
||
167 | ) { |
||
168 | /** @var string $parameterName */ |
||
169 | 4 | $parameterName = $parameterReflection->getName(); |
|
170 | |||
171 | /** @var string|null $parameterTypeName */ |
||
172 | 4 | $parameterTypeName = $this->getTypeNameFromReflectionParameter($parameterReflection); |
|
173 | |||
174 | 4 | if (isset($argumentsConfiguration[$parameterName])) { |
|
175 | /** @var mixed $value */ |
||
176 | 1 | $value = $this->resolveArgumentConfiguration($argumentsConfiguration[$parameterName]); |
|
177 | |||
178 | 1 | if (!empty($parameterTypeName)) { |
|
179 | if (class_exists($parameterTypeName) && is_scalar($value)) { |
||
180 | $value = $this->controllerHelper->findEntity($parameterTypeName, (string)$value); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | 1 | return $value; |
|
185 | |||
186 | 3 | } elseif (array_key_exists($index, $argumentsConfiguration)) { |
|
187 | 2 | return $this->resolveArgumentConfiguration($argumentsConfiguration[$index]); |
|
188 | |||
189 | 2 | } elseif ($parameterTypeName === Request::class) { |
|
190 | 1 | return $this->requestStack->getCurrentRequest(); |
|
191 | |||
192 | } else { |
||
193 | 1 | return $this->getDefaultValueFromParameterReflectionSafely($parameterReflection); |
|
194 | } |
||
195 | |||
196 | return null; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 1 | private function getDefaultValueFromParameterReflectionSafely(ReflectionParameter $parameterReflection) |
|
221 | |||
222 | 4 | private function getTypeNameFromReflectionParameter(ReflectionParameter $parameterReflection): ?string |
|
238 | |||
239 | } |
||
240 |
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.