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); |
||
11 | final class FileUploadCommand |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $repository; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $commitMessage; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $url; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $sha; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $branch; |
||
37 | |||
38 | /** |
||
39 | * @var ReadableStreamInterface |
||
40 | */ |
||
41 | private $stream; |
||
42 | |||
43 | /** |
||
44 | * @param string $repository |
||
45 | * @param string $commitMessage |
||
46 | * @param string $url |
||
47 | * @param string $sha |
||
48 | * @param string $branch |
||
49 | * @param ReadableStreamInterface $stream |
||
50 | */ |
||
51 | 3 | View Code Duplication | public function __construct( |
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getRepository(): string |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 3 | public function getCommitMessage(): string |
|
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | 3 | public function getUrl(): string |
|
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 3 | public function getSha(): string |
|
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | 3 | public function getBranch(): string |
|
106 | |||
107 | /** |
||
108 | * @return ReadableStreamInterface |
||
109 | */ |
||
110 | 3 | public function getStream(): ReadableStreamInterface |
|
114 | } |
||
115 |
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.