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 |
||
15 | View Code Duplication | class ResourceAggregate extends ObjectAggregate implements ResourceAggregateInterface |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * Add resource. |
||
19 | * |
||
20 | * @param \SimpleAcl\Resource $resource |
||
21 | 8 | */ |
|
22 | public function addResource(\SimpleAcl\Resource $resource) |
||
26 | |||
27 | /** |
||
28 | * Remove all resources. |
||
29 | * |
||
30 | 1 | */ |
|
31 | public function removeResources() |
||
35 | |||
36 | /** |
||
37 | * Remove resource by name. |
||
38 | * |
||
39 | * @param Object|string $resourceName |
||
40 | 1 | */ |
|
41 | public function removeResource($resourceName) |
||
45 | |||
46 | /** |
||
47 | * Add resources from array. |
||
48 | * |
||
49 | * @param array $resources |
||
50 | 1 | */ |
|
51 | public function setResources($resources) |
||
55 | |||
56 | /** |
||
57 | * Return all resources. |
||
58 | * |
||
59 | * @return array|Resource[] |
||
60 | 6 | */ |
|
61 | public function getResources() |
||
65 | |||
66 | /** |
||
67 | * Return array of names for registered resources. |
||
68 | * |
||
69 | * @return array |
||
70 | 5 | */ |
|
71 | public function getResourcesNames(): array |
||
75 | |||
76 | /** |
||
77 | * Return resource by name. |
||
78 | * |
||
79 | * @param Resource|string $resourceName |
||
80 | * |
||
81 | * @return null|Resource |
||
82 | 3 | */ |
|
83 | public function getResource($resourceName) |
||
87 | } |
||
88 |
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.