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 |
||
11 | View Code Duplication | class GenerateKeyEvent extends ContractsEvent |
|
|
|||
12 | { |
||
13 | |||
14 | /** @var Request */ |
||
15 | protected $request; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $key; |
||
19 | |||
20 | /** @var mixed */ |
||
21 | protected $payload; |
||
22 | |||
23 | public function __construct(Request $request, $key = '', $payload = null) |
||
29 | |||
30 | /** |
||
31 | * @return string |
||
32 | */ |
||
33 | public function getKey() |
||
37 | |||
38 | /** |
||
39 | * @param string $key |
||
40 | */ |
||
41 | public function setKey($key) |
||
45 | |||
46 | /** |
||
47 | * @return Request |
||
48 | */ |
||
49 | public function getRequest() |
||
53 | |||
54 | /** |
||
55 | * @param $part |
||
56 | */ |
||
57 | public function addToKey($part) |
||
65 | |||
66 | /** |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function getPayload() |
||
73 | } |
||
74 | } else { |
||
139 |
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.