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); |
||
18 | View Code Duplication | abstract class Commit extends AbstractResource implements CommitInterface |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $sha; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $url; |
||
29 | |||
30 | /** |
||
31 | * @var User |
||
32 | */ |
||
33 | protected $author; |
||
34 | |||
35 | /** |
||
36 | * @var User |
||
37 | */ |
||
38 | protected $comitter; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $message; |
||
44 | |||
45 | /** |
||
46 | * @var Tree |
||
47 | */ |
||
48 | protected $tree; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $comment_count; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $protection_url; |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | 4 | public function sha(): string |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 4 | public function url(): string |
|
75 | |||
76 | /** |
||
77 | * @return User |
||
78 | */ |
||
79 | public function author(): User |
||
83 | |||
84 | /** |
||
85 | * @return User |
||
86 | */ |
||
87 | public function comitter(): User |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | 4 | public function message(): string |
|
99 | |||
100 | /** |
||
101 | * @return TreeInterface |
||
102 | */ |
||
103 | public function tree(): TreeInterface |
||
107 | |||
108 | /** |
||
109 | * @return int |
||
110 | */ |
||
111 | 4 | public function commentCount(): int |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 4 | public function protectionUrl(): string |
|
123 | } |
||
124 |
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.