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 |
||
14 | class StoreRole extends FormRequest |
||
15 | { |
||
16 | /** |
||
17 | * Determine if the user is authorized to make this request. |
||
18 | * |
||
19 | * @return bool |
||
20 | */ |
||
21 | public function authorize() |
||
22 | { |
||
23 | return !empty(config('rbac.routesMainPermission')) ? $this->user()->can(config('rbac.routesMainPermission')) : true; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Get the validation rules that apply to the request. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | public function rules() |
||
39 | |||
40 | /** |
||
41 | * Get the error messages for the defined validation rules. |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | View Code Duplication | public function messages() |
|
56 | |||
57 | /** |
||
58 | * Get custom attributes for validator errors. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | View Code Duplication | public function attributes() |
|
70 | } |
||
71 |
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.