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 |
||
12 | class XlsHeaderFooterWrapper extends AbstractWrapper |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $context; |
||
18 | /** |
||
19 | * @var \Twig_Environment |
||
20 | */ |
||
21 | protected $environment; |
||
22 | /** |
||
23 | * @var XlsSheetWrapper |
||
24 | */ |
||
25 | protected $sheetWrapper; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $alignmentAttributes; |
||
31 | |||
32 | /** |
||
33 | * @var null|HeaderFooter |
||
34 | */ |
||
35 | protected $object; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $attributes; |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $mappings; |
||
44 | |||
45 | /** |
||
46 | * XlsHeaderFooterWrapper constructor. |
||
47 | * |
||
48 | * @param array $context |
||
49 | * @param \Twig_Environment $environment |
||
50 | * @param XlsSheetWrapper $sheetWrapper |
||
51 | */ |
||
52 | View Code Duplication | public function __construct(array $context, \Twig_Environment $environment, XlsSheetWrapper $sheetWrapper) |
|
66 | |||
67 | protected function initializeMappings() |
||
76 | |||
77 | /** |
||
78 | * @param string $type |
||
79 | * @param array $properties |
||
80 | * @throws \LogicException |
||
81 | */ |
||
82 | public function start(string $type, array $properties = []) |
||
101 | |||
102 | public function end() |
||
148 | |||
149 | /** |
||
150 | * @param string $type |
||
151 | * @param array $properties |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | public function startAlignment(string $type, array $properties = []) |
||
173 | |||
174 | /** |
||
175 | * @param string $value |
||
176 | * @throws \InvalidArgumentException |
||
177 | */ |
||
178 | public function endAlignment($value) |
||
202 | |||
203 | // |
||
204 | // Getters/Setters |
||
205 | // |
||
206 | |||
207 | /** |
||
208 | * @return null|HeaderFooter |
||
209 | */ |
||
210 | public function getObject() |
||
214 | |||
215 | /** |
||
216 | * @param null|HeaderFooter $object |
||
217 | */ |
||
218 | public function setObject(HeaderFooter $object = null) |
||
222 | |||
223 | /** |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getAttributes(): array |
||
230 | |||
231 | /** |
||
232 | * @param array $attributes |
||
233 | */ |
||
234 | public function setAttributes(array $attributes) |
||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getMappings(): array |
||
246 | |||
247 | /** |
||
248 | * @param array $mappings |
||
249 | */ |
||
250 | public function setMappings(array $mappings) |
||
254 | |||
255 | /** |
||
256 | * @return array |
||
257 | */ |
||
258 | public function getAlignmentAttributes(): array |
||
262 | |||
263 | /** |
||
264 | * @param array $alignmentAttributes |
||
265 | */ |
||
266 | public function setAlignmentAttributes(array $alignmentAttributes) |
||
270 | } |
||
271 |
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.