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 declare(strict_types=1); |
||
10 | final class FileDeleteCommand |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $repository; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $commitMessage; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $url; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $sha; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $branch; |
||
36 | |||
37 | /** |
||
38 | * @param string $repository |
||
39 | * @param string $commitMessage |
||
40 | * @param string $url |
||
41 | * @param string $sha |
||
42 | * @param string $branch |
||
43 | */ |
||
44 | View Code Duplication | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getRepository(): string |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | 3 | public function getCommitMessage(): string |
|
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | 3 | public function getUrl(): string |
|
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | 3 | public function getSha(): string |
|
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | 3 | public function getBranch(): string |
|
97 | } |
||
98 |
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.