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 |
||
7 | View Code Duplication | class PayloadButton extends Button |
|
|
|||
8 | { |
||
9 | private $payload; |
||
10 | |||
11 | 1 | public function __construct(string $label, $payload, array $parameters = []) |
|
17 | |||
18 | /** |
||
19 | * Make a new payload button instance. |
||
20 | * |
||
21 | * @param string $label |
||
22 | * @param mixed $payload |
||
23 | * @param array $parameters |
||
24 | * |
||
25 | * @return static |
||
26 | */ |
||
27 | 1 | public static function make(string $label, $payload, array $parameters = []) |
|
28 | { |
||
29 | 1 | return new static($label, $payload, $parameters); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Get payload. |
||
34 | * |
||
35 | * @return mixed |
||
36 | */ |
||
37 | 1 | public function getPayload() |
|
41 | |||
42 | /** |
||
43 | * Set payload. |
||
44 | * |
||
45 | * @param mixed $payload |
||
46 | * |
||
47 | * @return PayloadButton |
||
48 | */ |
||
49 | public function setPayload($payload): PayloadButton |
||
55 | } |
||
56 |
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.