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 DataParser 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 DataParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class DataParser implements DataParserInterface |
||
28 | { |
||
29 | const ERROR_CODE = 'ee2c1d49-ba40-4077-a6bb-b06baceb3e97'; |
||
30 | |||
31 | /** |
||
32 | * Current path |
||
33 | * |
||
34 | * @var \SplStack |
||
35 | */ |
||
36 | protected $path; |
||
37 | |||
38 | /** |
||
39 | * Resource decoders factory |
||
40 | * |
||
41 | * @var DecodersFactoryInterface |
||
42 | */ |
||
43 | protected $factory; |
||
44 | |||
45 | /** |
||
46 | * @var PropertyAccessor |
||
47 | */ |
||
48 | protected $accessor; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param DecodersFactoryInterface $factory |
||
54 | */ |
||
55 | 14 | public function __construct(DecodersFactoryInterface $factory) |
|
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 13 | public function setPath($path) |
|
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | 12 | public function restorePath() |
|
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | 1 | public function getPath() |
|
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | 12 | public function hasValue($data, $path) |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 12 | public function getValue($data, $path) |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 7 | public function parseString($data, $path) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 2 | public function parseInt($data, $path) |
|
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 2 | public function parseFloat($data, $path) |
|
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 2 | public function parseBool($data, $path) |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 2 | public function parseDateTime($data, $path, $format = 'Y-m-d') |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 1 | public function parseArray($data, $path, $itemsParser) |
|
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | 1 | public function parseResource($data, $path, $resType) |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 2 | public function parseDocument($data, $docType) |
|
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | */ |
||
303 | public function parseQueryParams($data, $paramsType) |
||
307 | |||
308 | /** |
||
309 | * Prepare path segment |
||
310 | * |
||
311 | * @param string $path |
||
312 | * @return string |
||
313 | */ |
||
314 | 12 | protected function preparePathSegment($path) |
|
318 | |||
319 | /** |
||
320 | * Initialize stack that store current path |
||
321 | */ |
||
322 | 14 | protected function initPathStack() |
|
326 | |||
327 | /** |
||
328 | * Parse single array item |
||
329 | * |
||
330 | * @param object|array $value |
||
331 | * @param string $key |
||
332 | * @param string|\Closure $itemsParser |
||
333 | * @return mixed |
||
334 | */ |
||
335 | 1 | protected function parseArrayItem($value, $key, $itemsParser) |
|
383 | |||
384 | /** |
||
385 | * Parse numeric value |
||
386 | * |
||
387 | * @param mixed $data |
||
388 | * @param string $path |
||
389 | * @param string $type |
||
390 | * @return float|int|null |
||
391 | */ |
||
392 | 3 | protected function parseNumeric($data, $path, $type) |
|
416 | } |
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.