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 |
||
29 | class ProjectPolicy |
||
30 | { |
||
31 | use HandlesAuthorization, ProjectAccess; |
||
32 | |||
33 | View Code Duplication | public function before(UserInterface $user) |
|
40 | |||
41 | /** |
||
42 | * Determine whether the user can view the project. |
||
43 | * |
||
44 | * @param User $user |
||
45 | * @param Project $project |
||
46 | * @return mixed |
||
47 | */ |
||
48 | View Code Duplication | public function view(User $user, Project $project) |
|
56 | |||
57 | |||
58 | /** |
||
59 | * Determine whether the user can create projects. |
||
60 | * |
||
61 | * @param User $user |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function create(User $user) |
||
69 | |||
70 | /** |
||
71 | * Determine whether the user can update the project. |
||
72 | * |
||
73 | * @param User $user |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function update(User $user) |
||
81 | |||
82 | /** |
||
83 | * Determine whether the user can delete the project. |
||
84 | * |
||
85 | * @param User $user |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function delete(User $user) |
||
93 | |||
94 | public function export(User $user) |
||
99 | |||
100 | public function viewInactiveUsers(User $user, Project $project = null) |
||
112 | //inactiveUsers |
||
113 | } |
||
114 |
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.