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 Usman\Guardian\Http\Controllers; |
||
10 | View Code Duplication | class Roles extends Base { |
|
|
|||
11 | |||
12 | /** |
||
13 | * The role repository instance. |
||
14 | * |
||
15 | * @var Usman\Guardian\Repositories\RoleRepository |
||
16 | */ |
||
17 | protected $role; |
||
18 | |||
19 | /** |
||
20 | * The validator instance. |
||
21 | * |
||
22 | * @var RoleValidator |
||
23 | */ |
||
24 | protected $validator; |
||
25 | |||
26 | /** |
||
27 | * Creates a new instance of the Roles controller. |
||
28 | * |
||
29 | * @param RoleRepositoryInterface $role |
||
30 | * @param RoleValidator $validator |
||
31 | */ |
||
32 | public function __construct(RoleRepositoryInterface $role, RoleValidator $validator) |
||
37 | |||
38 | /** |
||
39 | * Shows an index of the roles. |
||
40 | * |
||
41 | * @return Response |
||
42 | */ |
||
43 | public function listRole() |
||
48 | |||
49 | /** |
||
50 | * Shows the add role form. |
||
51 | * |
||
52 | * @return Response |
||
53 | */ |
||
54 | public function addRole() |
||
58 | |||
59 | /** |
||
60 | * Saves the new role in the database. |
||
61 | * |
||
62 | * @return Response |
||
63 | */ |
||
64 | public function createRole() |
||
82 | |||
83 | /** |
||
84 | * Shows the edit role form. |
||
85 | * |
||
86 | * @param int $id |
||
87 | * @return Response |
||
88 | */ |
||
89 | public function editRole($id) |
||
94 | |||
95 | /** |
||
96 | * Updates the role record in the database. |
||
97 | * |
||
98 | * @param int $id |
||
99 | * @return Response |
||
100 | */ |
||
101 | public function updateRole($id) |
||
117 | |||
118 | /** |
||
119 | * Deletes a role from the database. |
||
120 | * |
||
121 | * @param int $id |
||
122 | * @return Response |
||
123 | */ |
||
124 | public function deleteRole($id) |
||
130 | |||
131 | } |
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.