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 | const ALIGNMENT_CENTER = 'center'; |
||
13 | const ALIGNMENT_LEFT = 'left'; |
||
14 | const ALIGNMENT_RIGHT = 'right'; |
||
15 | |||
16 | const BASETYPE_FOOTER = 'footer'; |
||
17 | const BASETYPE_HEADER = 'header'; |
||
18 | |||
19 | const TYPE_EVEN = 'even'; |
||
20 | const TYPE_FIRST = 'first'; |
||
21 | const TYPE_ODD = 'odd'; |
||
22 | |||
23 | /** |
||
24 | * @var SheetWrapper |
||
25 | */ |
||
26 | protected $sheetWrapper; |
||
27 | |||
28 | /** |
||
29 | * @var HeaderFooter|null |
||
30 | */ |
||
31 | protected $object; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $alignmentParameters; |
||
36 | |||
37 | /** |
||
38 | * HeaderFooterWrapper constructor. |
||
39 | * |
||
40 | * @param array $context |
||
41 | * @param \Twig_Environment $environment |
||
42 | * @param SheetWrapper $sheetWrapper |
||
43 | */ |
||
44 | 112 | public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper) |
|
53 | |||
54 | /** |
||
55 | * @param string $alignment |
||
56 | * |
||
57 | * @throws \InvalidArgumentException |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 13 | View Code Duplication | public static function validateAlignment(string $alignment): string |
69 | |||
70 | /** |
||
71 | * @param string $baseType |
||
72 | * |
||
73 | * @throws \InvalidArgumentException |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 13 | View Code Duplication | public static function validateBaseType(string $baseType): string |
85 | |||
86 | /** |
||
87 | * @param string $baseType |
||
88 | * @param string|null $type |
||
89 | * @param array $properties |
||
90 | * |
||
91 | * @throws \InvalidArgumentException |
||
92 | * @throws \LogicException |
||
93 | * @throws \RuntimeException |
||
94 | */ |
||
95 | 5 | public function start(string $baseType, string $type = null, array $properties = []) |
|
117 | |||
118 | /** |
||
119 | * @throws \InvalidArgumentException |
||
120 | * @throws \LogicException |
||
121 | */ |
||
122 | 5 | public function end() |
|
171 | |||
172 | /** |
||
173 | * @param string $alignment |
||
174 | * @param array $properties |
||
175 | * |
||
176 | * @throws \InvalidArgumentException |
||
177 | * @throws \LogicException |
||
178 | */ |
||
179 | 5 | public function startAlignment(string $alignment, array $properties = []) |
|
202 | |||
203 | /** |
||
204 | * @param string $value |
||
205 | * |
||
206 | * @throws \InvalidArgumentException |
||
207 | * @throws \LogicException |
||
208 | */ |
||
209 | 5 | public function endAlignment($value) |
|
221 | |||
222 | /** |
||
223 | * @return null|HeaderFooter |
||
224 | */ |
||
225 | 6 | public function getObject() |
|
229 | |||
230 | /** |
||
231 | * @param null|HeaderFooter $object |
||
232 | */ |
||
233 | public function setObject(HeaderFooter $object = null) |
||
237 | |||
238 | /** |
||
239 | * @return array |
||
240 | */ |
||
241 | 1 | public function getAlignmentParameters(): array |
|
245 | |||
246 | /** |
||
247 | * @param array $alignmentParameters |
||
248 | */ |
||
249 | public function setAlignmentParameters(array $alignmentParameters) |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | protected function configureMappings(): array |
||
264 | } |
||
265 |
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.