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 Capabilities extends Base { |
|
|
|||
11 | |||
12 | /** |
||
13 | * The repository instance |
||
14 | * |
||
15 | * @var Usman\Guardian\Repositories\CapabilityRepository |
||
16 | */ |
||
17 | protected $capability; |
||
18 | |||
19 | /** |
||
20 | * The validator instance |
||
21 | * |
||
22 | * @var Usman\Guardian\Validators\CapabilityValidator |
||
23 | */ |
||
24 | protected $validator; |
||
25 | |||
26 | /** |
||
27 | * Creates a new instance of Capabilities controller. |
||
28 | * |
||
29 | * @param CapabilityRepositoryInterface $capability |
||
30 | * @param CapabilityValidator $validator |
||
31 | */ |
||
32 | public function __construct(CapabilityRepositoryInterface $capability, CapabilityValidator $validator) |
||
37 | |||
38 | /** |
||
39 | * Shows the index of capabilities. |
||
40 | * |
||
41 | * @return Response |
||
42 | */ |
||
43 | public function listCapability() |
||
48 | |||
49 | /** |
||
50 | * Shows the new capability form. |
||
51 | * |
||
52 | * @return Response |
||
53 | */ |
||
54 | public function addCapability() |
||
59 | |||
60 | /** |
||
61 | * Saves the newly created capability. |
||
62 | * |
||
63 | * @return Response |
||
64 | */ |
||
65 | public function createCapability() |
||
83 | |||
84 | /** |
||
85 | * Shows the edit capability form. |
||
86 | * |
||
87 | * @param int $id |
||
88 | * @return Response |
||
89 | */ |
||
90 | public function editCapability($id) |
||
95 | |||
96 | /** |
||
97 | * Updates the capability in database. |
||
98 | * |
||
99 | * @param int $id |
||
100 | * @return Response |
||
101 | */ |
||
102 | public function updateCapability($id) |
||
118 | |||
119 | /** |
||
120 | * Deletes a capability from database. |
||
121 | * |
||
122 | * @param int $id |
||
123 | * @return Response |
||
124 | */ |
||
125 | public function deleteCapability($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.