Total Complexity | 53 |
Total Lines | 522 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like IndexesController 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 IndexesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class IndexesController extends BaseController |
||
17 | { |
||
18 | public $controller_name = 'IndexesController'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
24 | { |
||
25 | $lang = $this->lang; |
||
26 | |||
27 | $action = $this->action; |
||
28 | if ('tree' == $action) { |
||
29 | return $this->doTree(); |
||
30 | } |
||
31 | |||
32 | $this->printHeader($lang['strindexes'], '<script src="' . \SUBFOLDER . '/js/indexes.js" type="text/javascript"></script>'); |
||
33 | |||
34 | $onloadInit = false; |
||
35 | if ('create_index' == $action || 'save_create_index' == $action) { |
||
36 | $onloadInit = true; |
||
37 | } |
||
38 | $this->printBody(true, 'detailbody', $onloadInit); |
||
39 | |||
40 | switch ($action) { |
||
41 | case 'cluster_index': |
||
42 | if (isset($_POST['cluster'])) { |
||
43 | $this->doClusterIndex(false); |
||
44 | } else { |
||
45 | $this->doDefault(); |
||
46 | } |
||
47 | |||
48 | break; |
||
49 | case 'confirm_cluster_index': |
||
50 | $this->doClusterIndex(true); |
||
51 | |||
52 | break; |
||
53 | case 'reindex': |
||
54 | $this->doReindex(); |
||
55 | |||
56 | break; |
||
57 | case 'save_create_index': |
||
58 | if (isset($_POST['cancel'])) { |
||
59 | $this->doDefault(); |
||
60 | } else { |
||
61 | $this->doSaveCreateIndex(); |
||
62 | } |
||
63 | |||
64 | break; |
||
65 | case 'create_index': |
||
66 | $this->doCreateIndex(); |
||
67 | |||
68 | break; |
||
69 | case 'drop_index': |
||
70 | if (isset($_POST['drop'])) { |
||
71 | $this->doDropIndex(false); |
||
72 | } else { |
||
73 | $this->doDefault(); |
||
74 | } |
||
75 | |||
76 | break; |
||
77 | case 'confirm_drop_index': |
||
78 | $this->doDropIndex(true); |
||
79 | |||
80 | break; |
||
81 | default: |
||
82 | $this->doDefault(); |
||
83 | |||
84 | break; |
||
85 | } |
||
86 | |||
87 | return $this->printFooter(); |
||
88 | } |
||
89 | |||
90 | public function doDefault($msg = '') |
||
91 | { |
||
92 | $lang = $this->lang; |
||
93 | $data = $this->misc->getDatabaseAccessor(); |
||
94 | |||
95 | $indPre = function (&$rowdata, $actions) use ($data, $lang) { |
||
96 | if ($data->phpBool($rowdata->fields['indisprimary'])) { |
||
97 | $rowdata->fields['+constraints'] = $lang['strprimarykey']; |
||
98 | $actions['drop']['disable'] = true; |
||
99 | } elseif ($data->phpBool($rowdata->fields['indisunique'])) { |
||
100 | $rowdata->fields['+constraints'] = $lang['struniquekey']; |
||
101 | $actions['drop']['disable'] = true; |
||
102 | } else { |
||
103 | $rowdata->fields['+constraints'] = ''; |
||
104 | } |
||
105 | |||
106 | return $actions; |
||
107 | }; |
||
108 | if (!isset($_REQUEST['subject'])) { |
||
109 | $_REQUEST['subject'] = 'table'; |
||
110 | } |
||
111 | |||
112 | $subject = urlencode($_REQUEST['subject']); |
||
113 | $object = urlencode($_REQUEST[$_REQUEST['subject']]); |
||
114 | |||
115 | $this->printTrail($subject); |
||
116 | $this->printTabs($subject, 'indexes'); |
||
117 | $this->printMsg($msg); |
||
118 | |||
119 | $indexes = $data->getIndexes($_REQUEST[$_REQUEST['subject']]); |
||
120 | |||
121 | $columns = [ |
||
122 | 'index' => [ |
||
123 | 'title' => $lang['strname'], |
||
124 | 'field' => Decorator::field('indname'), |
||
125 | ], |
||
126 | 'definition' => [ |
||
127 | 'title' => $lang['strdefinition'], |
||
128 | 'field' => Decorator::field('inddef'), |
||
129 | ], |
||
130 | 'constraints' => [ |
||
131 | 'title' => $lang['strconstraints'], |
||
132 | 'field' => Decorator::field('+constraints'), |
||
133 | 'type' => 'verbatim', |
||
134 | 'params' => ['align' => 'center'], |
||
135 | ], |
||
136 | 'clustered' => [ |
||
137 | 'title' => $lang['strclustered'], |
||
138 | 'field' => Decorator::field('indisclustered'), |
||
139 | 'type' => 'yesno', |
||
140 | ], |
||
141 | 'actions' => [ |
||
142 | 'title' => $lang['stractions'], |
||
143 | ], |
||
144 | 'comment' => [ |
||
145 | 'title' => $lang['strcomment'], |
||
146 | 'field' => Decorator::field('idxcomment'), |
||
147 | ], |
||
148 | ]; |
||
149 | |||
150 | $url = \SUBFOLDER . '/src/views/indexes'; |
||
151 | |||
152 | $actions = [ |
||
153 | 'cluster' => [ |
||
154 | 'content' => $lang['strclusterindex'], |
||
155 | 'attr' => [ |
||
156 | 'href' => [ |
||
157 | 'url' => $url, |
||
158 | 'urlvars' => [ |
||
159 | 'action' => 'confirm_cluster_index', |
||
160 | 'subject' => $subject, |
||
161 | $subject => $object, |
||
162 | 'index' => Decorator::field('indname'), |
||
163 | ], |
||
164 | ], |
||
165 | ], |
||
166 | ], |
||
167 | 'reindex' => [ |
||
168 | 'content' => $lang['strreindex'], |
||
169 | 'attr' => [ |
||
170 | 'href' => [ |
||
171 | 'url' => $url, |
||
172 | 'urlvars' => [ |
||
173 | 'action' => 'reindex', |
||
174 | 'subject' => $subject, |
||
175 | $subject => $object, |
||
176 | 'index' => Decorator::field('indname'), |
||
177 | ], |
||
178 | ], |
||
179 | ], |
||
180 | ], |
||
181 | 'drop' => [ |
||
182 | 'content' => $lang['strdrop'], |
||
183 | 'attr' => [ |
||
184 | 'href' => [ |
||
185 | 'url' => $url, |
||
186 | 'urlvars' => [ |
||
187 | 'action' => 'confirm_drop_index', |
||
188 | 'subject' => $subject, |
||
189 | $subject => $object, |
||
190 | 'index' => Decorator::field('indname'), |
||
191 | ], |
||
192 | ], |
||
193 | ], |
||
194 | ], |
||
195 | ]; |
||
196 | |||
197 | echo $this->printTable($indexes, $columns, $actions, 'indexes-indexes', $lang['strnoindexes'], $indPre); |
||
198 | |||
199 | $this->printNavLinks([ |
||
1 ignored issue
–
show
|
|||
200 | 'create' => [ |
||
201 | 'attr' => [ |
||
202 | 'href' => [ |
||
203 | 'url' => 'indexes', |
||
204 | 'urlvars' => [ |
||
205 | 'action' => 'create_index', |
||
206 | 'server' => $_REQUEST['server'], |
||
207 | 'database' => $_REQUEST['database'], |
||
208 | 'schema' => $_REQUEST['schema'], |
||
209 | $subject => $object, |
||
210 | 'subject' => $subject, |
||
211 | ], |
||
212 | ], |
||
213 | ], |
||
214 | 'content' => $lang['strcreateindex'], |
||
215 | ], |
||
216 | ], 'indexes-indexes', get_defined_vars()); |
||
1 ignored issue
–
show
|
|||
217 | } |
||
218 | |||
219 | public function doTree() |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Show confirmation of cluster index and perform actual cluster. |
||
256 | * |
||
257 | * @param mixed $confirm |
||
258 | */ |
||
259 | public function doClusterIndex($confirm) |
||
260 | { |
||
261 | $lang = $this->lang; |
||
262 | $data = $this->misc->getDatabaseAccessor(); |
||
263 | |||
264 | if (!isset($_REQUEST['subject'])) { |
||
265 | $_REQUEST['subject'] = 'table'; |
||
266 | } |
||
267 | $subject = urlencode($_REQUEST['subject']); |
||
268 | $object = urlencode($_REQUEST[$subject]); |
||
269 | |||
270 | //$this->printTrail($subject); |
||
271 | |||
272 | if ($confirm) { |
||
273 | // Default analyze to on |
||
274 | $_REQUEST['analyze'] = true; |
||
275 | |||
276 | $this->printTrail('index'); |
||
277 | $this->printTabs($subject, 'indexes'); |
||
278 | $this->printTitle($lang['strclusterindex'], 'pg.index.cluster'); |
||
279 | |||
280 | echo '<p>', sprintf($lang['strconfcluster'], $this->misc->printVal($_REQUEST['index'])), '</p>' . "\n"; |
||
281 | |||
282 | echo '<form action="' . \SUBFOLDER . '/src/views/indexes" method="post">' . "\n"; |
||
283 | echo '<p><input type="checkbox" id="analyze" name="analyze"', (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), ' />'; |
||
284 | echo "<label for=\"analyze\">{$lang['stranalyze']}</label></p>" . "\n"; |
||
285 | echo '<input type="hidden" name="action" value="cluster_index" />' . "\n"; |
||
286 | echo '<input type="hidden" name="table" value="', htmlspecialchars($object), '" />' . "\n"; |
||
287 | echo '<input type="hidden" name="index" value="', htmlspecialchars($_REQUEST['index']), '" />' . "\n"; |
||
288 | echo $this->misc->form; |
||
289 | echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />" . "\n"; |
||
290 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />" . "\n"; |
||
291 | echo '</form>' . "\n"; |
||
292 | } else { |
||
293 | set_time_limit(0); |
||
294 | list($status, $sql) = $data->clusterIndex($object, $_POST['index']); |
||
295 | if (0 == $status) { |
||
296 | if (isset($_POST['analyze'])) { |
||
297 | $status = $data->analyzeDB($object); |
||
298 | if (0 == $status) { |
||
299 | $this->doDefault($sql . '<br>' . $lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']); |
||
300 | } else { |
||
301 | $this->doDefault($sql . '<br>' . $lang['stranalyzebad']); |
||
302 | } |
||
303 | } else { |
||
304 | $this->doDefault($sql . '<br>' . $lang['strclusteredgood']); |
||
305 | } |
||
306 | } else { |
||
307 | $this->doDefault($sql . '<br>' . $lang['strclusteredbad']); |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | public function doReindex() |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Displays a screen where they can enter a new index. |
||
327 | * |
||
328 | * @param mixed $msg |
||
329 | */ |
||
330 | public function doCreateIndex($msg = '') |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Actually creates the new index in the database. |
||
455 | * |
||
456 | * @@ Note: this function can't handle columns with commas in them |
||
457 | */ |
||
458 | public function doSaveCreateIndex() |
||
459 | { |
||
460 | $lang = $this->lang; |
||
461 | $data = $this->misc->getDatabaseAccessor(); |
||
462 | |||
463 | if (!isset($_POST['subject'])) { |
||
464 | $_POST['subject'] = 'table'; |
||
465 | } |
||
466 | $subject = urlencode($_POST['subject']); |
||
467 | $object = urlencode($_POST[$subject]); |
||
468 | |||
469 | // Handle databases that don't have partial indexes |
||
470 | $formWhere = $this->getPostParam('formWhere', ''); |
||
471 | |||
472 | // Default tablespace to null if it isn't set |
||
473 | $formSpc = $this->getPostParam('formSpc'); |
||
474 | |||
475 | $IndexColumnList = $this->getPostParam('IndexColumnList', ''); |
||
476 | |||
477 | // Check that they've given a name and at least one column |
||
478 | if ('' == $IndexColumnList) { |
||
479 | $this->doCreateIndex($lang['strindexneedscols']); |
||
480 | } else { |
||
481 | list($status, $sql) = $data->createIndex( |
||
482 | $this->getPostParam('formIndexName', ''), |
||
483 | $object, |
||
484 | $IndexColumnList, |
||
485 | $this->getPostParam('formIndexType'), |
||
486 | $this->getPostParam('formUnique'), |
||
487 | $formWhere, |
||
488 | $formSpc, |
||
489 | $this->getPostParam('formConcur') |
||
490 | ); |
||
491 | |||
492 | if (0 == $status) { |
||
493 | $this->doDefault($sql . '<br>' . $lang['strindexcreated']); |
||
494 | } else { |
||
495 | $this->doCreateIndex($lang['strindexcreatedbad']); |
||
496 | } |
||
497 | } |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Show confirmation of drop index and perform actual drop. |
||
502 | * |
||
503 | * @param mixed $confirm |
||
504 | */ |
||
505 | public function doDropIndex($confirm) |
||
538 | } |
||
539 | } |
||
542 |