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 |
||
45 | class Section extends AbstractElement implements HasClassCode |
||
46 | { |
||
47 | /** |
||
48 | * |
||
49 | * @var InstanceIdentifier |
||
50 | */ |
||
51 | private $id; |
||
52 | |||
53 | /** |
||
54 | * |
||
55 | * @var CodedWithEquivalents |
||
56 | */ |
||
57 | private $code; |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | * @var CharacterString |
||
62 | */ |
||
63 | private $text; |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | * @var InstanceIdentifier[] |
||
68 | */ |
||
69 | private $templateIds = array(); |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @var CharacterString |
||
74 | */ |
||
75 | private $title; |
||
76 | |||
77 | /** |
||
78 | * |
||
79 | * @var Entry[] |
||
80 | */ |
||
81 | private $entries = array(); |
||
82 | |||
83 | protected function getElementTag(): string |
||
87 | |||
88 | public function getClassCode(): string |
||
92 | |||
93 | /** |
||
94 | * |
||
95 | * @return InstanceIdentifier |
||
96 | */ |
||
97 | public function getId() |
||
101 | |||
102 | public function setId(InstanceIdentifier $id) |
||
108 | |||
109 | public function getCode(): CodedWithEquivalents |
||
113 | |||
114 | public function setCode(CodedWithEquivalents $code) |
||
119 | |||
120 | public function getText(): CharacterString |
||
124 | |||
125 | public function setText(CharacterString $text) |
||
130 | |||
131 | View Code Duplication | public function setTemplateIds(array $templateIds) |
|
151 | |||
152 | public function addTemplateId(InstanceIdentifier $templateId) |
||
158 | |||
159 | |||
160 | /** |
||
161 | * the code for the current section |
||
162 | * |
||
163 | * @return InstanceIdentifier[] |
||
164 | */ |
||
165 | public function getTemplateIds() |
||
169 | |||
170 | /** |
||
171 | * The title for the section |
||
172 | * |
||
173 | * @return CharacterString |
||
174 | */ |
||
175 | public function getTitle() |
||
179 | |||
180 | public function setTitle(CharacterString $title) |
||
186 | |||
187 | /** |
||
188 | * create an entry, which is already bound to the current section |
||
189 | * |
||
190 | * @return Entry |
||
191 | */ |
||
192 | public function createEntry(): Entry |
||
200 | |||
201 | function getEntries(): array |
||
205 | |||
206 | public function addEntry(Entry $entry) |
||
210 | |||
211 | |||
212 | /** |
||
213 | * |
||
214 | * @param \DOMDocument $doc |
||
215 | */ |
||
216 | public function toDOMElement(\DOMDocument $doc): \DOMElement |
||
258 | } |
||
259 |
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.