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 |
||
40 | class NodeController extends BaseController |
||
41 | { |
||
42 | public function __construct() |
||
46 | |||
47 | /** |
||
48 | * List All Nodes. |
||
49 | * |
||
50 | * Lists all nodes currently on the system. |
||
51 | * |
||
52 | * @Get("/nodes/{?page}") |
||
53 | * @Versions({"v1"}) |
||
54 | * @Parameters({ |
||
55 | * @Parameter("page", type="integer", description="The page of results to view.", default=1) |
||
56 | * }) |
||
57 | * @Response(200) |
||
58 | */ |
||
59 | public function lists(Request $request) |
||
|
|||
60 | { |
||
61 | return Models\Node::all()->toArray(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create a New Node. |
||
66 | * |
||
67 | * @Post("/nodes") |
||
68 | * @Versions({"v1"}) |
||
69 | * @Transaction({ |
||
70 | * @Request({ |
||
71 | * 'name' => 'My API Node', |
||
72 | * 'location' => 1, |
||
73 | * 'public' => 1, |
||
74 | * 'fqdn' => 'daemon.wuzzle.woo', |
||
75 | * 'scheme' => 'https', |
||
76 | * 'memory' => 10240, |
||
77 | * 'memory_overallocate' => 100, |
||
78 | * 'disk' => 204800, |
||
79 | * 'disk_overallocate' => -1, |
||
80 | * 'daemonBase' => '/srv/daemon-data', |
||
81 | * 'daemonSFTP' => 2022, |
||
82 | * 'daemonListen' => 8080 |
||
83 | * }, headers={"Authorization": "Bearer <jwt-token>"}), |
||
84 | * @Response(200), |
||
85 | * @Response(422, body={ |
||
86 | * "message": "A validation error occured.", |
||
87 | * "errors": {}, |
||
88 | * "status_code": 422 |
||
89 | * }), |
||
90 | * @Response(503, body={ |
||
91 | * "message": "There was an error while attempting to add this node to the system.", |
||
92 | * "status_code": 503 |
||
93 | * }) |
||
94 | * }) |
||
95 | */ |
||
96 | View Code Duplication | public function create(Request $request) |
|
111 | |||
112 | /** |
||
113 | * List Specific Node. |
||
114 | * |
||
115 | * Lists specific fields about a server or all fields pertaining to that node. |
||
116 | * |
||
117 | * @Get("/nodes/{id}/{?fields}") |
||
118 | * @Versions({"v1"}) |
||
119 | * @Parameters({ |
||
120 | * @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."), |
||
121 | * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
||
122 | * }) |
||
123 | * @Response(200) |
||
124 | */ |
||
125 | public function view(Request $request, $id, $fields = null) |
||
155 | |||
156 | public function config(Request $request, $id) |
||
210 | |||
211 | /** |
||
212 | * List all Node Allocations. |
||
213 | * |
||
214 | * Returns a listing of all allocations for every node. |
||
215 | * |
||
216 | * @Get("/nodes/allocations") |
||
217 | * @Versions({"v1"}) |
||
218 | * @Response(200) |
||
219 | */ |
||
220 | public function allocations(Request $request) |
||
229 | |||
230 | /** |
||
231 | * List Node Allocation based on assigned to ID. |
||
232 | * |
||
233 | * Returns a listing of the allocation for the specified server id. |
||
234 | * |
||
235 | * @Get("/nodes/allocations/{id}") |
||
236 | * @Versions({"v1"}) |
||
237 | * @Response(200) |
||
238 | */ |
||
239 | public function allocationsView(Request $request, $id) |
||
254 | |||
255 | /** |
||
256 | * Delete Node. |
||
257 | * |
||
258 | * @Delete("/nodes/{id}") |
||
259 | * @Versions({"v1"}) |
||
260 | * @Parameters({ |
||
261 | * @Parameter("id", type="integer", required=true, description="The ID of the node."), |
||
262 | * }) |
||
263 | * @Response(204) |
||
264 | */ |
||
265 | View Code Duplication | public function delete(Request $request, $id) |
|
278 | } |
||
279 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.