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 |
||
31 | class NotePolicy |
||
32 | { |
||
33 | use HandlesAuthorization, ProjectAccess; |
||
34 | |||
35 | View Code Duplication | public function before(UserInterface $user) |
|
42 | |||
43 | /** |
||
44 | * Determine whether the user can view the note. |
||
45 | * |
||
46 | * @param User $user |
||
47 | * @param Note $note |
||
48 | * @return mixed |
||
49 | */ |
||
50 | // public function view(User $user, Note $note, Project $project) |
||
51 | // { |
||
52 | // if ($this->isPublicProject($project) || $project->isMember($user->id)) { |
||
53 | // return true; |
||
54 | // } |
||
55 | // |
||
56 | // return false; |
||
57 | // } |
||
58 | View Code Duplication | public function view(User $user, Project $project) |
|
67 | |||
68 | /** |
||
69 | * Determine whether the user can create notes. |
||
70 | * |
||
71 | * @param User $user |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function create(User $user) |
||
79 | |||
80 | /** |
||
81 | * Determine whether the user can update the note. |
||
82 | * |
||
83 | * @param User $user |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function update(User $user) |
||
91 | |||
92 | /** |
||
93 | * Determine whether the user can delete the note. |
||
94 | * |
||
95 | * @param User $user |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function delete(User $user) |
||
103 | |||
104 | } |
||
105 |
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.