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 |
||
20 | class Migration extends Component implements MigrationInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string|DbManager The auth manager component ID that this migration should work with |
||
24 | */ |
||
25 | public $authManager = 'authManager'; |
||
26 | |||
27 | /** |
||
28 | * Initializes the migration. |
||
29 | * This method will set [[authManager]] to be the 'authManager' application component, if it is `null`. |
||
30 | */ |
||
31 | public function init() |
||
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | View Code Duplication | public function up() |
|
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | View Code Duplication | public function down() |
|
89 | |||
90 | /** |
||
91 | * This method contains the logic to be executed when applying this migration. |
||
92 | * |
||
93 | * @return bool return a false value to indicate the migration fails |
||
94 | * and should not proceed further. All other return values mean the migration succeeds |
||
95 | */ |
||
96 | public function safeUp() |
||
99 | |||
100 | /** |
||
101 | * This method contains the logic to be executed when removing this migration. |
||
102 | * |
||
103 | * @return bool return a false value to indicate the migration fails |
||
104 | * and should not proceed further. All other return values mean the migration succeeds |
||
105 | */ |
||
106 | public function safeDown() |
||
109 | |||
110 | /** |
||
111 | * Creates new permission. |
||
112 | * |
||
113 | * @param string $name The name of the permission |
||
114 | * @param string $description The description of the permission |
||
115 | * @param string|null $ruleName The rule associated with the permission |
||
116 | * @param mixed|null $data The additional data associated with the permission |
||
117 | * |
||
118 | * @return Permission |
||
119 | */ |
||
120 | View Code Duplication | protected function createPermission($name, $description = '', $ruleName = null, $data = null) |
|
133 | |||
134 | /** |
||
135 | * Creates new role. |
||
136 | * |
||
137 | * @param string $name The name of the role |
||
138 | * @param string $description The description of the role |
||
139 | * @param string|null $ruleName The rule associated with the role |
||
140 | * @param mixed|null $data The additional data associated with the role |
||
141 | * |
||
142 | * @return Role |
||
143 | */ |
||
144 | View Code Duplication | protected function createRole($name, $description = '', $ruleName = null, $data = null) |
|
157 | |||
158 | /** |
||
159 | * Creates new rule. |
||
160 | * |
||
161 | * @param string $ruleName The name of the rule |
||
162 | * @param string|array $definition The class of the rule |
||
163 | * |
||
164 | * @return Rule |
||
165 | */ |
||
166 | protected function createRule($ruleName, $definition) |
||
185 | |||
186 | /** |
||
187 | * Finds either role or permission or throws an exception if it is not found. |
||
188 | * |
||
189 | * @param string $name |
||
190 | * |
||
191 | * @return Permission|Role|null |
||
192 | */ |
||
193 | protected function findItem($name) |
||
206 | |||
207 | /** |
||
208 | * Finds the role or throws an exception if it is not found. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * |
||
212 | * @return Role|null |
||
213 | */ |
||
214 | protected function findRole($name) |
||
223 | |||
224 | /** |
||
225 | * Finds the permission or throws an exception if it is not found. |
||
226 | * |
||
227 | * @param string $name |
||
228 | * |
||
229 | * @return Permission|null |
||
230 | */ |
||
231 | protected function findPermission($name) |
||
240 | |||
241 | /** |
||
242 | * Adds child. |
||
243 | * |
||
244 | * @param Item|string $parent Either name or Item instance which is parent |
||
245 | * @param Item|string $child Either name or Item instance which is child |
||
246 | */ |
||
247 | View Code Duplication | protected function addChild($parent, $child) |
|
260 | |||
261 | /** |
||
262 | * Removes child. |
||
263 | * |
||
264 | * @param Item|string $parent Either name or Item instance which is parent |
||
265 | * @param Item|string $child Either name or Item instance which is child |
||
266 | */ |
||
267 | View Code Duplication | protected function removeChild($parent, $child) |
|
280 | |||
281 | /** |
||
282 | * Assigns a role to a user. |
||
283 | * |
||
284 | * @param string|Role $role |
||
285 | * @param string|int $userId |
||
286 | */ |
||
287 | View Code Duplication | protected function assign($role, $userId) |
|
297 | |||
298 | /** |
||
299 | * Updates role. |
||
300 | * |
||
301 | * @param string|Role $role |
||
302 | * @param string $description |
||
303 | * @param string $ruleName |
||
304 | * @param mixed $data |
||
305 | * |
||
306 | * @return Role |
||
307 | */ |
||
308 | View Code Duplication | protected function updateRole($role, $description = '', $ruleName = null, $data = null) |
|
323 | |||
324 | /** |
||
325 | * Remove role. |
||
326 | * |
||
327 | * @param string $name |
||
328 | */ |
||
329 | View Code Duplication | protected function removeRole($name) |
|
340 | |||
341 | /** |
||
342 | * Updates permission. |
||
343 | * |
||
344 | * @param string|Permission $permission |
||
345 | * @param string $description |
||
346 | * @param string $ruleName |
||
347 | * @param mixed $data |
||
348 | * |
||
349 | * @return Permission |
||
350 | */ |
||
351 | View Code Duplication | protected function updatePermission($permission, $description = '', $ruleName = null, $data = null) |
|
366 | |||
367 | /** |
||
368 | * Remove permission. |
||
369 | * |
||
370 | * @param string $name |
||
371 | */ |
||
372 | View Code Duplication | protected function removePermission($name) |
|
383 | |||
384 | /** |
||
385 | * Updates rule. |
||
386 | * |
||
387 | * @param string $ruleName |
||
388 | * @param string $className |
||
389 | * |
||
390 | * @return Rule |
||
391 | */ |
||
392 | protected function updateRule($ruleName, $className) |
||
406 | |||
407 | /** |
||
408 | * Remove rule. |
||
409 | * |
||
410 | * @param string $ruleName |
||
411 | */ |
||
412 | View Code Duplication | protected function removeRule($ruleName) |
|
423 | } |
||
424 |
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.