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 |
||
12 | View Code Duplication | class GetRoleRequest extends Request |
|
|
|||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Define which Roles and/or Permissions has access to this request. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $access = [ |
||
21 | 'roles' => 'admin', |
||
22 | 'permissions' => '', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * Id's that needs decoding before applying the validation rules. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $decode = [ |
||
31 | 'id', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Defining the URL parameters (`/stores/999/items`) allows applying |
||
36 | * validation rules on them and allows accessing them like request data. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $urlParameters = [ |
||
41 | 'id', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | public function rules() |
||
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function authorize() |
||
63 | } |
||
64 |
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.