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 Role extends ActiveModel |
||
21 | { |
||
22 | /** |
||
23 | * Get all table data as object |
||
24 | * @param array $columns |
||
25 | * @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
||
26 | */ |
||
27 | View Code Duplication | public static function all($columns = ['*']) |
|
38 | |||
39 | /** |
||
40 | * Get role object via id |
||
41 | * @param int $role_id |
||
42 | * @return object|null |
||
43 | */ |
||
44 | public static function get($role_id) |
||
55 | |||
56 | /** |
||
57 | * @deprecated |
||
58 | * @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
||
59 | */ |
||
60 | public static function getAll() |
||
64 | |||
65 | /** |
||
66 | * Get all roles as array [id=>name] |
||
67 | * @return null|array |
||
68 | */ |
||
69 | public static function getIdNameAll() |
||
79 | |||
80 | /** |
||
81 | * Check if user role contains permission |
||
82 | * @param string $permission |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function can($permission) |
||
112 | } |
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.