Total Complexity | 42 |
Total Lines | 548 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AggregatesController 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.
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 AggregatesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class AggregatesController extends BaseController |
||
15 | { |
||
16 | public $controller_name = 'AggregatesController'; |
||
17 | |||
18 | public function render() |
||
1 ignored issue
–
show
|
|||
19 | { |
||
20 | $conf = $this->conf; |
||
21 | |||
22 | $lang = $this->lang; |
||
23 | |||
24 | $action = $this->action; |
||
25 | if ('tree' == $action) { |
||
26 | return $this->doTree(); |
||
27 | } |
||
28 | |||
29 | $this->printHeader($lang['straggregates']); |
||
30 | $this->printBody(); |
||
31 | |||
32 | switch ($action) { |
||
33 | case 'create': |
||
34 | $this->doCreate(); |
||
35 | |||
36 | break; |
||
37 | case 'save_create': |
||
38 | if (isset($_POST['cancel'])) { |
||
39 | $this->doDefault(); |
||
40 | } else { |
||
41 | $this->doSaveCreate(); |
||
42 | } |
||
43 | |||
44 | break; |
||
45 | case 'alter': |
||
46 | $this->doAlter(); |
||
47 | |||
48 | break; |
||
49 | case 'save_alter': |
||
50 | if (isset($_POST['alter'])) { |
||
51 | $this->doSaveAlter(); |
||
52 | } else { |
||
53 | $this->doProperties(); |
||
54 | } |
||
55 | |||
56 | break; |
||
57 | case 'drop': |
||
58 | if (isset($_POST['drop'])) { |
||
59 | $this->doDrop(false); |
||
60 | } else { |
||
61 | $this->doDefault(); |
||
62 | } |
||
63 | |||
64 | break; |
||
65 | case 'confirm_drop': |
||
66 | $this->doDrop(true); |
||
67 | |||
68 | break; |
||
69 | default: |
||
70 | $this->doDefault(); |
||
71 | |||
72 | break; |
||
73 | case 'properties': |
||
74 | $this->doProperties(); |
||
75 | |||
76 | break; |
||
77 | } |
||
78 | |||
79 | return $this->printFooter(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Show default list of aggregate functions in the database |
||
84 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
85 | */ |
||
86 | public function doDefault($msg = '') |
||
87 | { |
||
88 | $conf = $this->conf; |
||
89 | |||
90 | $lang = $this->lang; |
||
91 | $data = $this->misc->getDatabaseAccessor(); |
||
92 | $this->printTrail('schema'); |
||
93 | $this->printTabs('schema', 'aggregates'); |
||
94 | $this->printMsg($msg); |
||
95 | |||
96 | $aggregates = $data->getAggregates(); |
||
97 | |||
98 | $columns = [ |
||
99 | 'aggrname' => [ |
||
100 | 'title' => $lang['strname'], |
||
101 | 'field' => Decorator::field('proname'), |
||
102 | 'url' => SUBFOLDER . "/redirect/aggregate?action=properties&{$this->misc->href}&", |
||
103 | 'vars' => ['aggrname' => 'proname', 'aggrtype' => 'proargtypes'], |
||
104 | ], |
||
105 | 'aggrtype' => [ |
||
106 | 'title' => $lang['strtype'], |
||
107 | 'field' => Decorator::field('proargtypes'), |
||
108 | ], |
||
109 | 'aggrtransfn' => [ |
||
110 | 'title' => $lang['straggrsfunc'], |
||
111 | 'field' => Decorator::field('aggtransfn'), |
||
112 | ], |
||
113 | 'owner' => [ |
||
114 | 'title' => $lang['strowner'], |
||
115 | 'field' => Decorator::field('usename'), |
||
116 | ], |
||
117 | 'actions' => [ |
||
118 | 'title' => $lang['stractions'], |
||
119 | ], |
||
120 | 'comment' => [ |
||
121 | 'title' => $lang['strcomment'], |
||
122 | 'field' => Decorator::field('aggrcomment'), |
||
123 | ], |
||
124 | ]; |
||
125 | |||
126 | $actions = [ |
||
127 | 'alter' => [ |
||
128 | 'content' => $lang['stralter'], |
||
129 | 'attr' => [ |
||
130 | 'href' => [ |
||
131 | 'url' => 'aggregates.php', |
||
132 | 'urlvars' => [ |
||
133 | 'action' => 'alter', |
||
134 | 'aggrname' => Decorator::field('proname'), |
||
135 | 'aggrtype' => Decorator::field('proargtypes'), |
||
136 | ], |
||
137 | ], |
||
138 | ], |
||
139 | ], |
||
140 | 'drop' => [ |
||
141 | 'content' => $lang['strdrop'], |
||
142 | 'attr' => [ |
||
143 | 'href' => [ |
||
144 | 'url' => 'aggregates.php', |
||
145 | 'urlvars' => [ |
||
146 | 'action' => 'confirm_drop', |
||
147 | 'aggrname' => Decorator::field('proname'), |
||
148 | 'aggrtype' => Decorator::field('proargtypes'), |
||
149 | ], |
||
150 | ], |
||
151 | ], |
||
152 | ], |
||
153 | ]; |
||
154 | |||
155 | if (!$data->hasAlterAggregate()) { |
||
156 | unset($actions['alter']); |
||
157 | } |
||
158 | |||
159 | echo $this->printTable($aggregates, $columns, $actions, 'aggregates-aggregates', $lang['strnoaggregates']); |
||
160 | |||
161 | $navlinks = [ |
||
162 | 'create' => [ |
||
163 | 'attr' => [ |
||
164 | 'href' => [ |
||
165 | 'url' => 'aggregates.php', |
||
166 | 'urlvars' => [ |
||
167 | 'action' => 'create', |
||
168 | 'server' => $_REQUEST['server'], |
||
169 | 'database' => $_REQUEST['database'], |
||
170 | 'schema' => $_REQUEST['schema'], |
||
171 | ], |
||
172 | ], |
||
173 | ], |
||
174 | 'content' => $lang['strcreateaggregate'], |
||
175 | ], |
||
176 | ]; |
||
177 | $this->printNavLinks($navlinks, 'aggregates-aggregates', get_defined_vars()); |
||
178 | } |
||
179 | |||
180 | public function doTree() |
||
1 ignored issue
–
show
|
|||
181 | { |
||
182 | $conf = $this->conf; |
||
183 | |||
184 | $lang = $this->lang; |
||
185 | $data = $this->misc->getDatabaseAccessor(); |
||
186 | |||
187 | $aggregates = $data->getAggregates(); |
||
188 | |||
189 | $proto = Decorator::concat(Decorator::field('proname'), ' (', Decorator::field('proargtypes'), ')'); |
||
190 | $reqvars = $this->misc->getRequestVars('aggregate'); |
||
191 | |||
192 | $attrs = [ |
||
193 | 'text' => $proto, |
||
194 | 'icon' => 'Aggregate', |
||
195 | 'toolTip' => Decorator::field('aggcomment'), |
||
196 | 'action' => Decorator::redirecturl( |
||
197 | 'redirect.php', |
||
198 | $reqvars, |
||
199 | [ |
||
200 | 'action' => 'properties', |
||
201 | 'aggrname' => Decorator::field('proname'), |
||
202 | 'aggrtype' => Decorator::field('proargtypes'), |
||
203 | ] |
||
204 | ), |
||
205 | ]; |
||
206 | |||
207 | return $this->printTree($aggregates, $attrs, 'aggregates'); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Actually creates the new aggregate in the database |
||
212 | */ |
||
213 | public function doSaveCreate() |
||
214 | { |
||
215 | $conf = $this->conf; |
||
216 | |||
217 | $lang = $this->lang; |
||
218 | $data = $this->misc->getDatabaseAccessor(); |
||
219 | // Check inputs |
||
220 | if ('' == trim($_REQUEST['name'])) { |
||
221 | $this->doCreate($lang['straggrneedsname']); |
||
222 | |||
223 | return; |
||
224 | } |
||
225 | if ('' == trim($_REQUEST['basetype'])) { |
||
226 | $this->doCreate($lang['straggrneedsbasetype']); |
||
227 | |||
228 | return; |
||
229 | } |
||
230 | if ('' == trim($_REQUEST['sfunc'])) { |
||
231 | $this->doCreate($lang['straggrneedssfunc']); |
||
232 | |||
233 | return; |
||
234 | } |
||
235 | if ('' == trim($_REQUEST['stype'])) { |
||
236 | $this->doCreate($lang['straggrneedsstype']); |
||
237 | |||
238 | return; |
||
239 | } |
||
240 | |||
241 | $status = $data->createAggregate( |
||
242 | $_REQUEST['name'], |
||
243 | $_REQUEST['basetype'], |
||
244 | $_REQUEST['sfunc'], |
||
245 | $_REQUEST['stype'], |
||
246 | $_REQUEST['ffunc'], |
||
247 | $_REQUEST['initcond'], |
||
248 | $_REQUEST['sortop'], |
||
249 | $_REQUEST['aggrcomment'] |
||
250 | ); |
||
251 | |||
252 | if (0 == $status) { |
||
253 | $this->misc->setReloadBrowser(true); |
||
254 | $this->doDefault($lang['straggrcreated']); |
||
255 | } else { |
||
256 | $this->doCreate($lang['straggrcreatedbad']); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Displays a screen for create a new aggregate function |
||
262 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
263 | */ |
||
264 | public function doCreate($msg = '') |
||
265 | { |
||
266 | $conf = $this->conf; |
||
267 | |||
268 | $lang = $this->lang; |
||
269 | $data = $this->misc->getDatabaseAccessor(); |
||
270 | |||
271 | if (!isset($_REQUEST['name'])) { |
||
272 | $_REQUEST['name'] = ''; |
||
273 | } |
||
274 | |||
275 | if (!isset($_REQUEST['basetype'])) { |
||
276 | $_REQUEST['basetype'] = ''; |
||
277 | } |
||
278 | |||
279 | if (!isset($_REQUEST['sfunc'])) { |
||
280 | $_REQUEST['sfunc'] = ''; |
||
281 | } |
||
282 | |||
283 | if (!isset($_REQUEST['stype'])) { |
||
284 | $_REQUEST['stype'] = ''; |
||
285 | } |
||
286 | |||
287 | if (!isset($_REQUEST['ffunc'])) { |
||
288 | $_REQUEST['ffunc'] = ''; |
||
289 | } |
||
290 | |||
291 | if (!isset($_REQUEST['initcond'])) { |
||
292 | $_REQUEST['initcond'] = ''; |
||
293 | } |
||
294 | |||
295 | if (!isset($_REQUEST['sortop'])) { |
||
296 | $_REQUEST['sortop'] = ''; |
||
297 | } |
||
298 | |||
299 | if (!isset($_REQUEST['aggrcomment'])) { |
||
300 | $_REQUEST['aggrcomment'] = ''; |
||
301 | } |
||
302 | |||
303 | $this->printTrail('schema'); |
||
304 | $this->printTitle($lang['strcreateaggregate'], 'pg.aggregate.create'); |
||
305 | $this->printMsg($msg); |
||
306 | |||
307 | echo '<form action="' . SUBFOLDER . "/src/views/aggregates.php\" method=\"post\">\n"; |
||
308 | echo "<table>\n"; |
||
309 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
310 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
311 | htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n"; |
||
312 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrbasetype']}</th>\n"; |
||
313 | echo "\t\t<td class=\"data\"><input name=\"basetype\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
314 | htmlspecialchars($_REQUEST['basetype']), "\" /></td>\n\t</tr>\n"; |
||
315 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrsfunc']}</th>\n"; |
||
316 | echo "\t\t<td class=\"data\"><input name=\"sfunc\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
317 | htmlspecialchars($_REQUEST['sfunc']), "\" /></td>\n\t</tr>\n"; |
||
318 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrstype']}</th>\n"; |
||
319 | echo "\t\t<td class=\"data\"><input name=\"stype\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
320 | htmlspecialchars($_REQUEST['stype']), "\" /></td>\n\t</tr>\n"; |
||
321 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrffunc']}</th>\n"; |
||
322 | echo "\t\t<td class=\"data\"><input name=\"ffunc\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
323 | htmlspecialchars($_REQUEST['ffunc']), "\" /></td>\n\t</tr>\n"; |
||
324 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrinitcond']}</th>\n"; |
||
325 | echo "\t\t<td class=\"data\"><input name=\"initcond\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
326 | htmlspecialchars($_REQUEST['initcond']), "\" /></td>\n\t</tr>\n"; |
||
327 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrsortop']}</th>\n"; |
||
328 | echo "\t\t<td class=\"data\"><input name=\"sortop\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
329 | htmlspecialchars($_REQUEST['sortop']), "\" /></td>\n\t</tr>\n"; |
||
330 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
331 | echo "\t\t<td><textarea name=\"aggrcomment\" rows=\"3\" cols=\"32\">", |
||
332 | htmlspecialchars($_REQUEST['aggrcomment']), "</textarea></td>\n\t</tr>\n"; |
||
333 | |||
334 | echo "</table>\n"; |
||
335 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
336 | echo $this->misc->form; |
||
337 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
338 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
339 | echo "</form>\n"; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Function to save after altering an aggregate |
||
344 | */ |
||
345 | public function doSaveAlter() |
||
346 | { |
||
347 | $conf = $this->conf; |
||
348 | |||
349 | $lang = $this->lang; |
||
350 | $data = $this->misc->getDatabaseAccessor(); |
||
351 | |||
352 | // Check inputs |
||
353 | if ('' == trim($_REQUEST['aggrname'])) { |
||
354 | $this->doAlter($lang['straggrneedsname']); |
||
355 | |||
356 | return; |
||
357 | } |
||
358 | |||
359 | $status = $data->alterAggregate( |
||
360 | $_REQUEST['aggrname'], |
||
361 | $_REQUEST['aggrtype'], |
||
362 | $_REQUEST['aggrowner'], |
||
363 | $_REQUEST['aggrschema'], |
||
364 | $_REQUEST['aggrcomment'], |
||
365 | $_REQUEST['newaggrname'], |
||
366 | $_REQUEST['newaggrowner'], |
||
367 | $_REQUEST['newaggrschema'], |
||
368 | $_REQUEST['newaggrcomment'] |
||
369 | ); |
||
370 | if (0 == $status) { |
||
371 | $this->doDefault($lang['straggraltered']); |
||
372 | } else { |
||
373 | $this->doAlter($lang['straggralteredbad']); |
||
374 | |||
375 | return; |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Function to allow editing an aggregate function |
||
381 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
382 | */ |
||
383 | public function doAlter($msg = '') |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Show confirmation of drop and perform actual drop of the aggregate function selected |
||
429 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
430 | */ |
||
431 | public function doDrop($confirm) |
||
460 | } |
||
461 | } |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Show the properties of an aggregate |
||
466 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
467 | */ |
||
468 | public function doProperties($msg = '') |
||
562 | } |
||
563 | } |
||
564 |