Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ParameterResolver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ParameterResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ParameterResolver implements ParameterResolverInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var RequestStack |
||
28 | */ |
||
29 | private $requestStack; |
||
30 | |||
31 | /** |
||
32 | * @var PropertyAccessorInterface |
||
33 | */ |
||
34 | private $propertyAccessor; |
||
35 | |||
36 | /** |
||
37 | * @param RequestStack $requestStack |
||
38 | * @param PropertyAccessorInterface $propertyAccessor |
||
39 | */ |
||
40 | 80 | public function __construct(RequestStack $requestStack, PropertyAccessorInterface $propertyAccessor) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 4 | View Code Duplication | public function resolveApi() |
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 6 | public function resolveCriteria($mandatory = false) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 4 | View Code Duplication | public function resolveCurrentPage() |
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 3 | public function resolveForm(ResourceInterface $resource) |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 2 | public function resolveGrid(ResourceInterface $resource) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 3 | public function resolveHateoas() |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 3 | View Code Duplication | public function resolveLocationRoute() |
147 | { |
||
148 | 3 | if (($request = $this->resolveRequest()) === null) { |
|
149 | 1 | throw new RequestNotFoundException(); |
|
150 | } |
||
151 | |||
152 | 2 | $locationRoute = $this->resolveParameter('location_route'); |
|
153 | |||
154 | 2 | if (empty($locationRoute)) { |
|
155 | 1 | throw new RuntimeException(sprintf( |
|
156 | 1 | 'The location route could not be found for the route "%s".', |
|
157 | 1 | $request->attributes->get('_route') |
|
158 | 1 | )); |
|
159 | } |
||
160 | |||
161 | 1 | return $locationRoute; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 3 | public function resolveLocationRouteParameters($object) |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 3 | public function resolveMaxPerPage() |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 3 | View Code Duplication | public function resolveRedirectRoute() |
184 | { |
||
185 | 3 | if (($request = $this->resolveRequest()) === null) { |
|
186 | 1 | throw new RequestNotFoundException(); |
|
187 | } |
||
188 | |||
189 | 2 | $redirectRoute = $this->resolveParameter('redirect_route'); |
|
190 | |||
191 | 2 | if (empty($redirectRoute)) { |
|
192 | 1 | throw new RuntimeException(sprintf( |
|
193 | 1 | 'The redirect route could not be found for the route "%s".', |
|
194 | 1 | $request->attributes->get('_route') |
|
195 | 1 | )); |
|
196 | } |
||
197 | |||
198 | 1 | return $redirectRoute; |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 6 | public function resolveRedirectRouteParameters($object = null, $forwardParameters = false) |
|
205 | { |
||
206 | 6 | $routeParameters = $this->resolveRouteParameter('redirect_route_parameters', $object); |
|
207 | |||
208 | 5 | if ($forwardParameters && ($request = $this->resolveRequest()) !== null) { |
|
209 | 1 | return array_replace_recursive($request->query->all(), $routeParameters); |
|
210 | } |
||
211 | |||
212 | 4 | return $routeParameters; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | 3 | public function resolveRedirectRouteParametersForward() |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 3 | public function resolveRepositoryMethod($action) |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 3 | public function resolveSerializerGroups() |
|
235 | { |
||
236 | 3 | return $this->resolveParameter('serializer_groups', []); |
|
237 | } |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 3 | public function resolveSerializerNull() |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 3 | public function resolveStatusCode($statusCode) |
|
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 3 | public function resolveSorting() |
|
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | 3 | View Code Duplication | public function resolveTemplate() |
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | 3 | public function resolveThemes() |
|
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 5 | public function resolveTranslationDomain() |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | 3 | public function resolveValidationGroups() |
|
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | 3 | public function resolveVoter() |
|
318 | |||
319 | /** |
||
320 | * @param string $parameter |
||
321 | * @param mixed $default |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | 67 | private function resolveParameter($parameter, $default = null) |
|
333 | |||
334 | /** |
||
335 | * @return Request|null |
||
336 | */ |
||
337 | 75 | private function resolveRequest() |
|
341 | |||
342 | /** |
||
343 | * @param string $parameter |
||
344 | * @param object|null $object |
||
345 | * |
||
346 | * @return mixed[] |
||
347 | */ |
||
348 | 9 | private function resolveRouteParameter($parameter, $object = null) |
|
370 | } |
||
371 |
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.