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 |
||
26 | final class Backup implements BackupInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $name; |
||
32 | |||
33 | /** |
||
34 | * @var FileInterface[] |
||
35 | */ |
||
36 | private $files; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | private $size; |
||
42 | |||
43 | /** |
||
44 | * @var \DateTimeInterface |
||
45 | */ |
||
46 | private $createdAt; |
||
47 | |||
48 | /** |
||
49 | * @var \DateTimeInterface |
||
50 | */ |
||
51 | private $modifiedAt; |
||
52 | |||
53 | 54 | public function __construct($name, array $files = array(), $size = 0, $createdAt = null, $modifiedAt = null) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 44 | public function getName() |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 2 | public function setName($name) |
|
84 | |||
85 | 6 | public function addFiles(array $files) |
|
93 | |||
94 | 2 | public function addFile(FileInterface $file) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 28 | public function getFiles() |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 20 | public function setFiles(array $files) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 8 | public function getSize() |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function setSize($size) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 10 | public function getCreatedAt() |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | View Code Duplication | public function setCreatedAt($createdAt) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function getModifiedAt() |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | View Code Duplication | public function setModifiedAt($modifiedAt) |
|
183 | } |
||
184 |
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.