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 |
||
9 | class Element extends AbstractElement |
||
10 | { |
||
11 | /** |
||
12 | * @var null|Button |
||
13 | */ |
||
14 | private $button; |
||
15 | |||
16 | /** |
||
17 | * @var null|DefaultAction |
||
18 | */ |
||
19 | private $defaultAction; |
||
20 | |||
21 | /** |
||
22 | * @param string $title |
||
23 | * @param null|string $subtitle |
||
24 | * @param null|string $imageUrl |
||
25 | * @param Button|null $button |
||
26 | * @param DefaultAction|null $defaultAction |
||
27 | */ |
||
28 | 8 | public function __construct($title, $subtitle = null, $imageUrl = null, Button $button = null, DefaultAction $defaultAction = null) |
|
34 | |||
35 | 1 | public function getButton() |
|
39 | |||
40 | 1 | public function getDefaultAction() |
|
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | 1 | View Code Duplication | public function jsonSerialize() |
58 | } |
||
59 |
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.