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 IndexDocument 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 IndexDocument, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class IndexDocument implements \JsonSerializable { |
||
30 | |||
31 | const NOT_ENCODED = 0; |
||
32 | const ENCODED_BASE64 = 1; |
||
33 | |||
34 | /** @var string|int */ |
||
35 | protected $id; |
||
36 | |||
37 | /** @var string */ |
||
38 | protected $providerId; |
||
39 | |||
40 | /** @var DocumentAccess */ |
||
41 | protected $access; |
||
42 | |||
43 | /** @var Index */ |
||
44 | protected $index; |
||
45 | |||
46 | /** @var int */ |
||
47 | protected $modifiedTime = 0; |
||
48 | |||
49 | /** @var string */ |
||
50 | protected $source = ''; |
||
51 | |||
52 | /** @var array */ |
||
53 | protected $tags = []; |
||
54 | |||
55 | /** @var array */ |
||
56 | protected $metaTags = []; |
||
57 | |||
58 | /** @var array */ |
||
59 | protected $subTags = []; |
||
60 | |||
61 | /** @var string */ |
||
62 | protected $title = ''; |
||
63 | |||
64 | /** @var string */ |
||
65 | protected $content = null; |
||
66 | |||
67 | /** @var string */ |
||
68 | protected $hash = ''; |
||
69 | |||
70 | /** @var array */ |
||
71 | protected $parts = []; |
||
72 | |||
73 | /** @var string */ |
||
74 | protected $link = ''; |
||
75 | |||
76 | /** @var array */ |
||
77 | protected $more = []; |
||
78 | |||
79 | /** @var array */ |
||
80 | protected $excerpts = []; |
||
81 | |||
82 | /** @var string */ |
||
83 | protected $score; |
||
84 | |||
85 | /** @var array */ |
||
86 | protected $info = []; |
||
87 | |||
88 | /** @var int */ |
||
89 | protected $contentEncoded; |
||
90 | |||
91 | |||
92 | public function __construct($providerId, $id) { |
||
96 | |||
97 | |||
98 | // /** |
||
99 | // * @param string|integer $id |
||
100 | // * |
||
101 | // * @return $this |
||
102 | // */ |
||
103 | // public function setId($id) { |
||
104 | // $this->id = $id; |
||
105 | // |
||
106 | // return $this; |
||
107 | // } |
||
108 | |||
109 | /** |
||
110 | * @return string|integer |
||
111 | */ |
||
112 | public function getId() { |
||
115 | |||
116 | |||
117 | // /** |
||
118 | // * @param string $providerId |
||
119 | // * |
||
120 | // * @return $this |
||
121 | // */ |
||
122 | // public function setProviderId($providerId) { |
||
123 | // $this->providerId = $providerId; |
||
124 | // |
||
125 | // return $this; |
||
126 | // } |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getProviderId() { |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @param Index $index |
||
138 | */ |
||
139 | public function setIndex(Index $index) { |
||
142 | |||
143 | /** |
||
144 | * @return Index |
||
145 | */ |
||
146 | public function getIndex() { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @param int $modifiedTime |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setModifiedTime($modifiedTime) { |
||
161 | |||
162 | /** |
||
163 | * @return int |
||
164 | */ |
||
165 | public function getModifiedTime() { |
||
168 | |||
169 | /** |
||
170 | * @param int $time |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isOlderThan($time) { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param DocumentAccess $access |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setAccess(DocumentAccess $access) { |
||
189 | |||
190 | /** |
||
191 | * @return DocumentAccess |
||
192 | */ |
||
193 | public function getAccess() { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @param array $tags |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function setTags($tags) { |
||
208 | |||
209 | /** |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getTags() { |
||
215 | |||
216 | /** |
||
217 | * @param $tag |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function addTag($tag) { |
||
226 | |||
227 | |||
228 | /** |
||
229 | * @param array $tags |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function setMetaTags($tags) { |
||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getMetaTags() { |
||
245 | |||
246 | /** |
||
247 | * @param $tags |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function addMetaTag($tags) { |
||
256 | |||
257 | |||
258 | /** |
||
259 | * @param array $tags |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function setSubTags($tags) { |
||
268 | |||
269 | /** |
||
270 | * @param bool $formatted |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | View Code Duplication | public function getSubTags($formatted = false) { |
|
290 | |||
291 | /** |
||
292 | * @param string $k |
||
293 | * @param string $tag |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function addSubTag($k, $tag) { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getSource() { |
||
310 | |||
311 | /** |
||
312 | * @param string $source |
||
313 | * |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setSource($source) { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @param string $title |
||
325 | * |
||
326 | * @return $this |
||
327 | */ |
||
328 | public function setTitle($title) { |
||
333 | |||
334 | /** |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getTitle() { |
||
340 | |||
341 | |||
342 | /** |
||
343 | * @param string $content |
||
344 | * @param int $encoded |
||
345 | * |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function setContent($content, $encoded = 0) { |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getContent() { |
||
361 | |||
362 | /** |
||
363 | * @return int |
||
364 | */ |
||
365 | public function getContentSize() { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function initHash() { |
||
382 | |||
383 | /** |
||
384 | * @param $hash |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setHash($hash) { |
||
393 | |||
394 | /** |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getHash() { |
||
400 | |||
401 | |||
402 | /** |
||
403 | * @param string $part |
||
404 | * @param string $content |
||
405 | * |
||
406 | * @return $this |
||
407 | */ |
||
408 | public function addPart($part, $content) { |
||
413 | |||
414 | /** |
||
415 | * @param array $parts |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function setParts($parts) { |
||
424 | |||
425 | /** |
||
426 | * @return array |
||
427 | */ |
||
428 | public function getParts() { |
||
431 | |||
432 | |||
433 | /** |
||
434 | * @return int |
||
435 | */ |
||
436 | public function isContentEncoded() { |
||
439 | |||
440 | |||
441 | /** |
||
442 | * @param string $link |
||
443 | * |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function setLink($link) { |
||
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | public function getLink() { |
||
458 | |||
459 | |||
460 | /** |
||
461 | * @param array $more |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function setMore($more) { |
||
470 | |||
471 | /** |
||
472 | * @return array |
||
473 | */ |
||
474 | public function getMore() { |
||
477 | |||
478 | |||
479 | /** |
||
480 | * @param array $excerpts |
||
481 | * |
||
482 | * @return $this |
||
483 | */ |
||
484 | public function setExcerpts($excerpts) { |
||
491 | |||
492 | /** |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getExcerpts() { |
||
498 | |||
499 | /** |
||
500 | * @param string $excerpt |
||
501 | */ |
||
502 | public function addExcerpt($excerpt) { |
||
507 | |||
508 | /** |
||
509 | * @param $excerpt |
||
510 | * |
||
511 | * @return mixed |
||
512 | */ |
||
513 | public function cleanExcerpt($excerpt) { |
||
523 | |||
524 | /** |
||
525 | * @param string $score |
||
526 | * |
||
527 | * @return $this |
||
528 | */ |
||
529 | public function setScore($score) { |
||
534 | |||
535 | /** |
||
536 | * @return string |
||
537 | */ |
||
538 | public function getScore() { |
||
541 | |||
542 | |||
543 | /** |
||
544 | * @param string $info |
||
545 | * @param mixed $value |
||
546 | * |
||
547 | * @return $this |
||
548 | */ |
||
549 | public function setInfo($info, $value) { |
||
554 | |||
555 | /** |
||
556 | * @param string $info |
||
557 | * @param mixed $default |
||
558 | * |
||
559 | * @return mixed |
||
560 | */ |
||
561 | public function getInfo($info, $default = '') { |
||
568 | |||
569 | |||
570 | /** |
||
571 | * @return array |
||
572 | */ |
||
573 | public function getInfoAll() { |
||
586 | |||
587 | |||
588 | public function __destruct() { |
||
607 | |||
608 | /** |
||
609 | * @return array<string,string|integer|DocumentAccess|array> |
||
610 | */ |
||
611 | public function jsonSerialize() { |
||
632 | |||
633 | } |
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.