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 |
||
10 | class HeaderFooterWrapper extends BaseWrapper |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $context; |
||
16 | /** |
||
17 | * @var \Twig_Environment |
||
18 | */ |
||
19 | protected $environment; |
||
20 | /** |
||
21 | * @var SheetWrapper |
||
22 | */ |
||
23 | protected $sheetWrapper; |
||
24 | |||
25 | /** |
||
26 | * @var null|HeaderFooter |
||
27 | */ |
||
28 | protected $object; |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $attributes; |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $mappings; |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $alignmentAttributes; |
||
41 | |||
42 | /** |
||
43 | * HeaderFooterWrapper constructor. |
||
44 | * |
||
45 | * @param array $context |
||
46 | * @param \Twig_Environment $environment |
||
47 | * @param SheetWrapper $sheetWrapper |
||
48 | */ |
||
49 | View Code Duplication | public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper) |
|
62 | |||
63 | /** |
||
64 | * @param string $type |
||
65 | * @param array $properties |
||
66 | * |
||
67 | * @throws \InvalidArgumentException |
||
68 | * @throws \LogicException |
||
69 | * @throws \RuntimeException |
||
70 | */ |
||
71 | public function start(string $type, array $properties = []) |
||
90 | |||
91 | public function end() |
||
137 | |||
138 | /** |
||
139 | * @param string $type |
||
140 | * @param array $properties |
||
141 | * |
||
142 | * @throws \InvalidArgumentException |
||
143 | */ |
||
144 | public function startAlignment(string $type, array $properties = []) |
||
163 | |||
164 | /** |
||
165 | * @param string $value |
||
166 | * |
||
167 | * @throws \InvalidArgumentException |
||
168 | */ |
||
169 | public function endAlignment($value) |
||
193 | |||
194 | /** |
||
195 | * @return null|HeaderFooter |
||
196 | */ |
||
197 | public function getObject() |
||
201 | |||
202 | /** |
||
203 | * @param null|HeaderFooter $object |
||
204 | */ |
||
205 | public function setObject(HeaderFooter $object = null) |
||
209 | |||
210 | /** |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getAttributes(): array |
||
217 | |||
218 | /** |
||
219 | * @param array $attributes |
||
220 | */ |
||
221 | public function setAttributes(array $attributes) |
||
225 | |||
226 | /** |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getMappings(): array |
||
233 | |||
234 | /** |
||
235 | * @param array $mappings |
||
236 | */ |
||
237 | public function setMappings(array $mappings) |
||
241 | |||
242 | /** |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getAlignmentAttributes(): array |
||
249 | |||
250 | /** |
||
251 | * @param array $alignmentAttributes |
||
252 | */ |
||
253 | public function setAlignmentAttributes(array $alignmentAttributes) |
||
257 | |||
258 | protected function initializeMappings() |
||
267 | } |
||
268 |
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.