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 Transformer 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 Transformer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class Transformer implements StrategyInterface |
||
12 | { |
||
13 | const SELF_LINK = 'self'; |
||
14 | const FIRST_LINK = 'first'; |
||
15 | const LAST_LINK = 'last'; |
||
16 | const PREV_LINK = 'prev'; |
||
17 | const NEXT_LINK = 'next'; |
||
18 | const LINKS_HREF = 'href'; |
||
19 | const LINKS_KEY = 'links'; |
||
20 | |||
21 | /** |
||
22 | * @var Mapping[] |
||
23 | */ |
||
24 | protected $mappings = []; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $firstUrl = ''; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $lastUrl = ''; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $prevUrl = ''; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $nextUrl = ''; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $selfUrl = ''; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $meta = []; |
||
51 | |||
52 | /** |
||
53 | * @param Mapper $mapper |
||
54 | */ |
||
55 | public function __construct(Mapper $mapper) |
||
59 | |||
60 | /** |
||
61 | * Represents the provided $value as a serialized value in string format. |
||
62 | * |
||
63 | * @param mixed $value |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | abstract public function serialize($value); |
||
68 | |||
69 | /** |
||
70 | * Unserialization will fail. This is a transformer. |
||
71 | * |
||
72 | * @param string $value |
||
73 | * |
||
74 | * @throws TransformerException |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function unserialize($value) |
||
82 | |||
83 | /** |
||
84 | * @param string $key |
||
85 | * @param array|string $value |
||
86 | */ |
||
87 | public function addMeta($key, $value) |
||
91 | |||
92 | /** |
||
93 | * @param array $meta |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function setMeta(array $meta) |
||
103 | |||
104 | /** |
||
105 | * @param array $links |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function addHrefToLinks(array $links) |
||
119 | |||
120 | /** |
||
121 | * @throws TransformerException |
||
122 | */ |
||
123 | protected function noMappingGuard() |
||
131 | |||
132 | /** |
||
133 | * Changes all array keys to under_score format using recursion. |
||
134 | * |
||
135 | * @param array $array |
||
136 | */ |
||
137 | protected function recursiveSetKeysToUnderScore(array &$array) |
||
150 | |||
151 | /** |
||
152 | * @return array |
||
153 | */ |
||
154 | protected function buildLinks() |
||
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getSelfUrl() |
||
176 | |||
177 | /** |
||
178 | * @param string $selfUrl |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setSelfUrl($selfUrl) |
||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getFirstUrl() |
||
196 | |||
197 | /** |
||
198 | * @param string $firstUrl |
||
199 | * |
||
200 | * @throws \InvalidArgumentException |
||
201 | */ |
||
202 | public function setFirstUrl($firstUrl) |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getLastUrl() |
||
214 | |||
215 | /** |
||
216 | * @param string $lastUrl |
||
217 | * |
||
218 | * @throws \InvalidArgumentException |
||
219 | */ |
||
220 | public function setLastUrl($lastUrl) |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getPrevUrl() |
||
232 | |||
233 | /** |
||
234 | * @param string $prevUrl |
||
235 | * |
||
236 | * @throws \InvalidArgumentException |
||
237 | */ |
||
238 | public function setPrevUrl($prevUrl) |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getNextUrl() |
||
250 | |||
251 | /** |
||
252 | * @param string $nextUrl |
||
253 | * |
||
254 | * @throws \InvalidArgumentException |
||
255 | */ |
||
256 | public function setNextUrl($nextUrl) |
||
260 | |||
261 | /** |
||
262 | * @param array $copy |
||
263 | * @param string $type |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | protected function getResponseAdditionalLinks(array $copy, $type) |
||
289 | |||
290 | /** |
||
291 | * Replaces the Serializer array structure representing scalar values to the actual scalar value using recursion. |
||
292 | * |
||
293 | * @param array $array |
||
294 | */ |
||
295 | View Code Duplication | protected static function formatScalarValues(array &$array) |
|
303 | |||
304 | /** |
||
305 | * @param array $array |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | protected static function arrayToScalarValue(array &$array) |
||
317 | |||
318 | /** |
||
319 | * @param array $array |
||
320 | * @param string $method |
||
321 | */ |
||
322 | protected static function loopScalarValues(array &$array, $method) |
||
330 | |||
331 | /** |
||
332 | * Simplifies the data structure by removing an array level if data is scalar and has one element in array. |
||
333 | * |
||
334 | * @param array $array |
||
335 | */ |
||
336 | View Code Duplication | protected static function flattenObjectsWithSingleKeyScalars(array &$array) |
|
346 | |||
347 | /** |
||
348 | * @return \NilPortugues\Api\Mapping\Mapping[] |
||
349 | */ |
||
350 | public function getMappings() |
||
354 | |||
355 | /** |
||
356 | * @param string $alias |
||
357 | * |
||
358 | * @return Mapping|null |
||
359 | */ |
||
360 | public function getMappingByAlias($alias) |
||
370 | |||
371 | /** |
||
372 | * @param string $className |
||
373 | * |
||
374 | * @return Mapping|null |
||
375 | */ |
||
376 | public function getMappingByClassName($className) |
||
382 | } |
||
383 |
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.