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 |
||
21 | View Code Duplication | class UserProfileModel extends AbstractModel implements ResourceModelInterface |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * Account ID. |
||
25 | * |
||
26 | * @SA\Type("string") |
||
27 | * @SA\SerializedName("accountId") |
||
28 | * |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $accountId; |
||
32 | |||
33 | /** |
||
34 | * Email address associated with account. |
||
35 | * |
||
36 | * @SA\Type("string") |
||
37 | * @SA\SerializedName("email") |
||
38 | * |
||
39 | * @var string|null |
||
40 | */ |
||
41 | protected $email; |
||
42 | |||
43 | /** |
||
44 | * Role in account. |
||
45 | * |
||
46 | * Enum: User, Collaborator |
||
47 | * |
||
48 | * @see \Zibios\WrikePhpLibrary\Enum\UserRoleEnum |
||
49 | * |
||
50 | * @SA\Type("string") |
||
51 | * @SA\SerializedName("role") |
||
52 | * |
||
53 | * @var string|null |
||
54 | */ |
||
55 | protected $role; |
||
56 | |||
57 | /** |
||
58 | * Is user external. |
||
59 | * |
||
60 | * @SA\Type("boolean") |
||
61 | * @SA\SerializedName("external") |
||
62 | * |
||
63 | * @var bool|null |
||
64 | */ |
||
65 | protected $external; |
||
66 | |||
67 | /** |
||
68 | * Is user account admin. |
||
69 | * |
||
70 | * @SA\Type("boolean") |
||
71 | * @SA\SerializedName("admin") |
||
72 | * |
||
73 | * @var bool|null |
||
74 | */ |
||
75 | protected $admin; |
||
76 | |||
77 | /** |
||
78 | * Is user account owner. |
||
79 | * |
||
80 | * @SA\Type("boolean") |
||
81 | * @SA\SerializedName("owner") |
||
82 | * |
||
83 | * @var bool|null |
||
84 | */ |
||
85 | protected $owner; |
||
86 | |||
87 | /** |
||
88 | * @return null|string |
||
89 | */ |
||
90 | 1 | public function getAccountId() |
|
94 | |||
95 | /** |
||
96 | * @param null|string $accountId |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | 1 | public function setAccountId($accountId) |
|
106 | |||
107 | /** |
||
108 | * @return null|string |
||
109 | */ |
||
110 | 1 | public function getEmail() |
|
114 | |||
115 | /** |
||
116 | * @param null|string $email |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | 1 | public function setEmail($email) |
|
126 | |||
127 | /** |
||
128 | * @return null|string |
||
129 | */ |
||
130 | 1 | public function getRole() |
|
134 | |||
135 | /** |
||
136 | * @param null|string $role |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | 1 | public function setRole($role) |
|
146 | |||
147 | /** |
||
148 | * @return bool|null |
||
149 | */ |
||
150 | 1 | public function getExternal() |
|
154 | |||
155 | /** |
||
156 | * @param bool|null $external |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | 1 | public function setExternal($external) |
|
166 | |||
167 | /** |
||
168 | * @return bool|null |
||
169 | */ |
||
170 | 1 | public function getAdmin() |
|
174 | |||
175 | /** |
||
176 | * @param bool|null $admin |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | 1 | public function setAdmin($admin) |
|
186 | |||
187 | /** |
||
188 | * @return bool|null |
||
189 | */ |
||
190 | 1 | public function getOwner() |
|
194 | |||
195 | /** |
||
196 | * @param bool|null $owner |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 1 | public function setOwner($owner) |
|
206 | } |
||
207 |
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.