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 namespace App\Modules\Acl\Repositories\V1; |
||
5 | class UserRepository extends AbstractRepository |
||
6 | { |
||
7 | /** |
||
8 | * Return the model full namespace. |
||
9 | * |
||
10 | * @return string |
||
11 | */ |
||
12 | protected function getModel() |
||
16 | |||
17 | /** |
||
18 | * Check if the logged in user or the given user |
||
19 | * has the given permissions on the given model. |
||
20 | * |
||
21 | * @param string $nameOfPermission |
||
22 | * @param string $model |
||
23 | * @param boolean $user |
||
24 | * @return boolean |
||
25 | */ |
||
26 | public function can($nameOfPermission, $model, $user = false ) |
||
36 | |||
37 | /** |
||
38 | * Check if the logged in user has the given group. |
||
39 | * |
||
40 | * @param string $groupName |
||
41 | * @return boolean |
||
42 | */ |
||
43 | public function hasGroup($groupName) |
||
48 | |||
49 | /** |
||
50 | * Assign the given group ids to the given user. |
||
51 | * |
||
52 | * @param integer $user_id |
||
53 | * @param array $group_ids |
||
54 | * @return object |
||
55 | */ |
||
56 | View Code Duplication | public function assignGroups($user_id, $group_ids) |
|
66 | } |
||
67 |
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.