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:
1 | <?php |
||
4 | View Code Duplication | class Attachment extends AbstractProcessor |
|
|
|||
5 | { |
||
6 | /** |
||
7 | * Attachment constructor. |
||
8 | * |
||
9 | * @param string $field |
||
10 | */ |
||
11 | public function __construct(string $field) |
||
15 | |||
16 | /** |
||
17 | * Set field. |
||
18 | * |
||
19 | * @param string $field |
||
20 | * |
||
21 | * @return $this |
||
22 | */ |
||
23 | public function setField(string $field) |
||
27 | |||
28 | /** |
||
29 | * Set target_field. Default attachment. |
||
30 | * |
||
31 | * @param string $targetField |
||
32 | * |
||
33 | * @return $this |
||
34 | */ |
||
35 | public function setTargetField(string $targetField) |
||
39 | |||
40 | /** |
||
41 | * Set indexed_chars. Default 100000. |
||
42 | * |
||
43 | * @param int $indexedChars |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function setIndexedChars(int $indexedChars) |
||
51 | |||
52 | /** |
||
53 | * Set properties. Default all properties. Can be content, title, name, author, keywords, date, content_type, content_length, language |
||
54 | * |
||
55 | * @param array $properties |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function setProperties(array $properties) |
||
63 | |||
64 | /** |
||
65 | * Set ignore_missing. Default false. |
||
66 | * |
||
67 | * @param bool $ignoreMissing |
||
68 | * |
||
69 | * @return $this |
||
70 | */ |
||
71 | public function setIgnoreMissing(bool $ignoreMissing) |
||
75 | } |
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.