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 | 82 | public function __construct(RequestStack $requestStack, PropertyAccessorInterface $propertyAccessor) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 4 | public function resolveApi() |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 6 | public function resolveCriteria($mandatory = false) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 3 | public function resolveCurrentPage() |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 3 | public function resolveForm(ResourceInterface $resource) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 2 | public function resolveGrid(ResourceInterface $resource) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 3 | public function resolveHateoas() |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 3 | View Code Duplication | public function resolveLocationRoute() |
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 3 | public function resolveLocationRouteParameters($object) |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 6 | public function resolveMaxPerPage() |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 3 | View Code Duplication | public function resolveRedirectRoute() |
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 6 | public function resolveRedirectRouteParameters($object = null, $forwardParameters = false) |
|
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | 3 | public function resolveRedirectRouteParametersForward() |
|
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | 3 | public function resolveRepositoryMethod($action) |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 3 | public function resolveSerializerGroups() |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | 3 | public function resolveSerializerNull() |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 3 | public function resolveStatusCode($statusCode) |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | 3 | public function resolveSorting() |
|
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | 3 | View Code Duplication | public function resolveTemplate() |
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | 3 | public function resolveThemes() |
|
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | 5 | public function resolveTranslationDomain() |
|
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | */ |
||
320 | 3 | public function resolveValidationGroups() |
|
324 | |||
325 | /** |
||
326 | * {@inheritdoc} |
||
327 | */ |
||
328 | 3 | public function resolveVoter() |
|
332 | |||
333 | /** |
||
334 | * @param string $parameter |
||
335 | * @param mixed $default |
||
336 | * |
||
337 | * @return mixed |
||
338 | */ |
||
339 | 67 | private function resolveParameter($parameter, $default = null) |
|
347 | |||
348 | /** |
||
349 | * @return Request|null |
||
350 | */ |
||
351 | 77 | private function resolveRequest() |
|
355 | |||
356 | /** |
||
357 | * @param string $parameter |
||
358 | * @param object|null $object |
||
359 | * |
||
360 | * @return mixed[] |
||
361 | */ |
||
362 | 9 | private function resolveRouteParameter($parameter, $object = null) |
|
384 | } |
||
385 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.