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 Model |
||
21 | { |
||
22 | use SoftDeletes; |
||
23 | |||
24 | protected $fillable = ['name', 'display_name', 'description']; |
||
25 | |||
26 | protected $searchableFields = ['name', 'display_name', 'description']; |
||
27 | |||
28 | protected $sortableFields = ['name', 'display_name', 'description', 'created_at', 'updated_at']; |
||
29 | |||
30 | protected $dates = ['deleted_at']; |
||
31 | |||
32 | View Code Duplication | public function cachedPermissions() |
|
39 | |||
40 | /** |
||
41 | * Checks if the role has a permission by its name. |
||
42 | * |
||
43 | * @param string|array $name Permission name or array of permission names. |
||
44 | * @param bool $requireAll All permissions in the array are required. |
||
45 | * @return bool |
||
46 | */ |
||
47 | View Code Duplication | public function hasPermission($name, $requireAll = false) |
|
74 | |||
75 | View Code Duplication | public function users() |
|
83 | |||
84 | public function permissions() |
||
92 | |||
93 | } |
||
94 |
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.