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:
Complex classes like WorkspaceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WorkspaceController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class WorkspaceController implements ControllerProviderInterface |
||
13 | { |
||
14 | |||
15 | public $POINT_FOR_USING_A_CONQUERED_BADGE = 200; |
||
16 | public $POINT_FOR_USING_A_BADGE = 100; |
||
17 | public $POINT_DEFAULT = 50; |
||
18 | public $DATE_FORMAT = 'Y-m-d\TH:i:s\Z'; |
||
19 | private $app; |
||
20 | |||
21 | public function connect(Application $app) |
||
47 | public function getWorkspaceList(Request $request) |
||
48 | { |
||
49 | //print_r("sono qui"); |
||
50 | $user_id = $this->getSessionId(); |
||
51 | $workspaces = R::getAll("SELECT ws.id, |
||
52 | ws.title, |
||
53 | ws.description, |
||
54 | ws.environment, |
||
55 | ws.completed |
||
56 | FROM userworkspace AS uws |
||
57 | LEFT JOIN workspace AS ws |
||
58 | ON uws.workspace = ws.id |
||
59 | WHERE uws.user = ?",[$user_id]); |
||
60 | $list = []; |
||
61 | View Code Duplication | foreach ($workspaces as $ws) { |
|
|
|||
62 | array_push($list, [ |
||
63 | "id"=>intval($ws['id']), |
||
64 | "title"=>$ws['title'], |
||
65 | "description"=>$ws['description'], |
||
66 | "environment"=>intval($ws['environment']), |
||
67 | "point"=>0, //TODO fare una view con i point già calcolati per il ws |
||
68 | "completed"=>boolval($ws['completed']), |
||
69 | ]); |
||
70 | } |
||
71 | $headers = []; |
||
72 | return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
||
73 | |||
74 | } |
||
75 | public function createWorkspace(Request $request) |
||
76 | { |
||
77 | $user_id = $this->getSessionId(); |
||
78 | $data = json_decode($request->getContent(), true); |
||
79 | //TODO validate json_decode |
||
80 | $title = $data['title']; |
||
81 | $description = $data['description']; |
||
82 | $environment = $data['environment']; |
||
83 | |||
84 | $patrol = $data['team']['patrol']; |
||
85 | $unit = $data['team']['unit']; |
||
86 | $group = $data['team']['group']; |
||
87 | |||
88 | //save the workspace get id |
||
89 | $ws = R::dispense("workspace"); |
||
90 | $ws->title = $title; |
||
91 | $ws->description = $description; |
||
92 | $ws->environment = $environment; |
||
93 | $ws->completed = false; |
||
94 | $ws->inserttime = date($this->DATE_FORMAT); |
||
95 | $ws->lastupdatetime = date($this->DATE_FORMAT); |
||
96 | $id = R::store($ws); |
||
97 | |||
98 | //save the team |
||
99 | $team = R::dispense("team"); |
||
100 | $team->workspace = $id; |
||
101 | $team->patrol = $patrol; |
||
102 | $team->unit = $unit; |
||
103 | $team->group = $group; |
||
104 | $team_id = R::store($team); |
||
105 | |||
106 | //create a phantom part to add badge |
||
107 | $part = R::dispense("part"); |
||
108 | $part->workspace = $id; |
||
109 | $part->user = $user_id; |
||
110 | $part->inserttime = date($this->DATE_FORMAT); |
||
111 | $part->lastupdatetime = date($this->DATE_FORMAT); |
||
112 | $part->totalpoint = 0; |
||
113 | $part->deleted = false; |
||
114 | $part_id = R::store($part); |
||
115 | |||
116 | //add the badge to the project |
||
117 | View Code Duplication | foreach ($data['badges'] as $badge_id) { |
|
118 | $pb = R::dispense("partbadge"); |
||
119 | $pb->badge = $badge_id; |
||
120 | $pb->part = $part_id; |
||
121 | $pb->inserttime = date($this->DATE_FORMAT); |
||
122 | $tmp = R::store($pb); |
||
123 | } |
||
124 | |||
125 | //add the workspace created to the user as owner |
||
126 | $usw = R::dispense("userworkspace"); |
||
127 | $usw->user = $user_id; |
||
128 | $usw->workspace = $id; |
||
129 | $usw->inserttime = date($this->DATE_FORMAT); |
||
130 | R::store($usw); |
||
131 | |||
132 | $res = ["id" => $id]; |
||
133 | $headers = []; |
||
134 | return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
||
135 | } |
||
136 | |||
137 | public function putWorkspace($id, Request $request) |
||
138 | { |
||
139 | $user_id = $this->getSessionId(); |
||
140 | $data = json_decode($request->getContent(), true); |
||
141 | //TODO validate json_decode |
||
142 | |||
143 | $title = $data['title']; |
||
144 | $description = $data['description']; |
||
145 | $environment = $data['environment']; |
||
146 | |||
147 | $patrol = $data['team']['patrol']; |
||
148 | $unit = $data['team']['unit']; |
||
149 | $group = $data['team']['group']; |
||
150 | |||
151 | $ws = R::load("workspace", intval($id)); |
||
152 | $ws->title = $title; |
||
153 | $ws->description = $description; |
||
154 | $ws->environment = $environment; |
||
155 | $ws->completed = false; |
||
156 | $ws->lastupdatetime = date($this->DATE_FORMAT); |
||
157 | $id = R::store($ws); |
||
158 | |||
159 | //save the team |
||
160 | $team = R::findOne("team", "workspace = ?", [$id]); |
||
161 | $team->patrol = $patrol; |
||
162 | $team->unit = $unit; |
||
163 | $team->group = $group; |
||
164 | $team_id = R::store($team); |
||
165 | |||
166 | //TODO WE DELIBERATLY IGNORE ANY CHANGE IN BADGES AND PARTS, THEY MUST NOT BE EDITED HERE!!!! AND IF YOU DID WE DONT CARE! |
||
167 | |||
168 | |||
169 | $response = new Response(); |
||
170 | $response->headers->set('Content-Type', 'text/html'); |
||
171 | $response->setStatusCode(Response::HTTP_NO_CONTENT); |
||
172 | $response->setSharedMaxAge(300); |
||
173 | } |
||
174 | |||
175 | public function getWorkspace($id, Request $request) { |
||
176 | $user_id = $this->getSessionId(); |
||
177 | //TODO controllare che l'utente abbia diritto a vedere questo workspace |
||
178 | |||
179 | $workspace = R::findOne("workspace", "id = ?", [$id]); |
||
180 | $team = R::findOne("team", "workspace = ?", [$id]); |
||
181 | $part = R::findAll("part", "workspace = ? AND DELETED = 0", [$id]); |
||
182 | |||
183 | $badges = R::findAll("workspacebadge", "workspace = ?", [$id]); //TODO controllare i deleted |
||
184 | |||
185 | $l_part = []; |
||
186 | foreach ($part as $p) { |
||
187 | array_push($l_part, intval($p['id'])); |
||
188 | } |
||
189 | $l_badges = []; |
||
190 | foreach ($badges as $b) { |
||
191 | array_push($l_badges, intval($b['badge'])); |
||
192 | } |
||
193 | |||
194 | $res = [ |
||
195 | 'id'=> intval($workspace['id']), |
||
196 | 'title'=> $workspace['title'], |
||
197 | 'description'=> $workspace['description'], |
||
198 | 'environment'=> intval($workspace['environment']), |
||
199 | 'completed'=> $workspace['completed'], |
||
200 | 'parts'=>$l_part, |
||
201 | 'badges'=>$l_badges, |
||
202 | 'team'=>[ |
||
203 | 'patrol'=>$team['patrol'], |
||
204 | 'unit'=>$team['unit'], |
||
205 | 'group'=>$team['group'] |
||
206 | ] |
||
207 | ]; |
||
208 | $headers = []; |
||
209 | return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
||
210 | } |
||
211 | |||
212 | public function deleteWorkspace($id, Request $request) |
||
213 | { |
||
214 | //Disassocia un utente da un workspace |
||
215 | $user_id = $this->getSessionId(); |
||
216 | |||
217 | $wp = R::findOne("userworkspace", "workspace = ? AND user = ?", [$id, $user_id]); |
||
218 | R::trash($wp); |
||
219 | |||
220 | $response = new Response(); |
||
221 | $response->headers->set('Content-Type', 'text/html'); |
||
222 | $response->setStatusCode(Response::HTTP_NO_CONTENT); |
||
223 | $response->setSharedMaxAge(300); |
||
224 | } |
||
225 | |||
226 | public function share($id, Request $request) { |
||
249 | |||
250 | public function join(Request $request) { |
||
288 | |||
289 | public function postPart($id, Request $request) {//TODO quando uno crea una parte bisognerebbe dire che lui c'era in quella parte |
||
290 | $user_id = $this->getSessionId(); |
||
291 | |||
292 | $data = json_decode($request->getContent(), true); |
||
329 | |||
330 | public function getPart($id, $part_id, Request $request) { |
||
375 | |||
376 | private function getPositionInArray($array, $id) { |
||
386 | |||
387 | public function putPart($id, $part_id, Request $request) { |
||
456 | |||
457 | public function deletePart($id, $part_id, Request $request) { |
||
478 | |||
479 | private function getPoint($badge_id, $badges) { |
||
538 | |||
539 | public function deleteCheckin($id, $part_id, Request $request) { |
||
555 | } |
||
556 |
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.