| Total Complexity | 64 |
| Total Lines | 529 |
| 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 |
||
| 14 | class IndexesController extends BaseController |
||
| 15 | { |
||
| 16 | public $controller_name = 'IndexesController'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default method to render the controller according to the action parameter. |
||
| 20 | */ |
||
| 21 | public function render() |
||
| 86 | } |
||
| 87 | |||
| 88 | public function doDefault($msg = '') |
||
|
1 ignored issue
–
show
|
|||
| 89 | { |
||
| 90 | $lang = $this->lang; |
||
| 91 | $data = $this->misc->getDatabaseAccessor(); |
||
| 92 | |||
| 93 | $indPre = function (&$rowdata, $actions) use ($data, $lang) { |
||
| 94 | if ($data->phpBool($rowdata->fields['indisprimary'])) { |
||
| 95 | $rowdata->fields['+constraints'] = $lang['strprimarykey']; |
||
| 96 | $actions['drop']['disable'] = true; |
||
| 97 | } elseif ($data->phpBool($rowdata->fields['indisunique'])) { |
||
| 98 | $rowdata->fields['+constraints'] = $lang['struniquekey']; |
||
| 99 | $actions['drop']['disable'] = true; |
||
| 100 | } else { |
||
| 101 | $rowdata->fields['+constraints'] = ''; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $actions; |
||
| 105 | }; |
||
| 106 | if (!isset($_REQUEST['subject'])) { |
||
| 107 | $_REQUEST['subject'] = 'table'; |
||
| 108 | } |
||
| 109 | |||
| 110 | $subject = urlencode($_REQUEST['subject']); |
||
| 111 | $object = urlencode($_REQUEST[$_REQUEST['subject']]); |
||
| 112 | |||
| 113 | $this->printTrail($subject); |
||
| 114 | $this->printTabs($subject, 'indexes'); |
||
| 115 | $this->printMsg($msg); |
||
| 116 | |||
| 117 | $indexes = $data->getIndexes($_REQUEST[$_REQUEST['subject']]); |
||
| 118 | |||
| 119 | $columns = [ |
||
| 120 | 'index' => [ |
||
| 121 | 'title' => $lang['strname'], |
||
| 122 | 'field' => Decorator::field('indname'), |
||
| 123 | ], |
||
| 124 | 'definition' => [ |
||
| 125 | 'title' => $lang['strdefinition'], |
||
| 126 | 'field' => Decorator::field('inddef'), |
||
| 127 | ], |
||
| 128 | 'constraints' => [ |
||
| 129 | 'title' => $lang['strconstraints'], |
||
| 130 | 'field' => Decorator::field('+constraints'), |
||
| 131 | 'type' => 'verbatim', |
||
| 132 | 'params' => ['align' => 'center'], |
||
| 133 | ], |
||
| 134 | 'clustered' => [ |
||
| 135 | 'title' => $lang['strclustered'], |
||
| 136 | 'field' => Decorator::field('indisclustered'), |
||
| 137 | 'type' => 'yesno', |
||
| 138 | ], |
||
| 139 | 'actions' => [ |
||
| 140 | 'title' => $lang['stractions'], |
||
| 141 | ], |
||
| 142 | 'comment' => [ |
||
| 143 | 'title' => $lang['strcomment'], |
||
| 144 | 'field' => Decorator::field('idxcomment'), |
||
| 145 | ], |
||
| 146 | ]; |
||
| 147 | |||
| 148 | $url = (\SUBFOLDER ? '/' . \SUBFOLDER : '') . '/src/views/indexes'; |
||
| 149 | |||
| 150 | $actions = [ |
||
| 151 | 'cluster' => [ |
||
| 152 | 'content' => $lang['strclusterindex'], |
||
| 153 | 'attr' => [ |
||
| 154 | 'href' => [ |
||
| 155 | 'url' => $url, |
||
| 156 | 'urlvars' => [ |
||
| 157 | 'action' => 'confirm_cluster_index', |
||
| 158 | 'subject' => $subject, |
||
| 159 | $subject => $object, |
||
| 160 | 'index' => Decorator::field('indname'), |
||
| 161 | ], |
||
| 162 | ], |
||
| 163 | ], |
||
| 164 | ], |
||
| 165 | 'reindex' => [ |
||
| 166 | 'content' => $lang['strreindex'], |
||
| 167 | 'attr' => [ |
||
| 168 | 'href' => [ |
||
| 169 | 'url' => $url, |
||
| 170 | 'urlvars' => [ |
||
| 171 | 'action' => 'reindex', |
||
| 172 | 'subject' => $subject, |
||
| 173 | $subject => $object, |
||
| 174 | 'index' => Decorator::field('indname'), |
||
| 175 | ], |
||
| 176 | ], |
||
| 177 | ], |
||
| 178 | ], |
||
| 179 | 'drop' => [ |
||
| 180 | 'content' => $lang['strdrop'], |
||
| 181 | 'attr' => [ |
||
| 182 | 'href' => [ |
||
| 183 | 'url' => $url, |
||
| 184 | 'urlvars' => [ |
||
| 185 | 'action' => 'confirm_drop_index', |
||
| 186 | 'subject' => $subject, |
||
| 187 | $subject => $object, |
||
| 188 | 'index' => Decorator::field('indname'), |
||
| 189 | ], |
||
| 190 | ], |
||
| 191 | ], |
||
| 192 | ], |
||
| 193 | ]; |
||
| 194 | |||
| 195 | echo $this->printTable($indexes, $columns, $actions, 'indexes-indexes', $lang['strnoindexes'], $indPre); |
||
| 196 | |||
| 197 | $this->printNavLinks([ |
||
|
1 ignored issue
–
show
|
|||
| 198 | 'create' => [ |
||
| 199 | 'attr' => [ |
||
| 200 | 'href' => [ |
||
| 201 | 'url' => 'indexes.php', |
||
| 202 | 'urlvars' => [ |
||
| 203 | 'action' => 'create_index', |
||
| 204 | 'server' => $_REQUEST['server'], |
||
| 205 | 'database' => $_REQUEST['database'], |
||
| 206 | 'schema' => $_REQUEST['schema'], |
||
| 207 | $subject => $object, |
||
| 208 | 'subject' => $subject, |
||
| 209 | ], |
||
| 210 | ], |
||
| 211 | ], |
||
| 212 | 'content' => $lang['strcreateindex'], |
||
| 213 | ], |
||
| 214 | ], 'indexes-indexes', get_defined_vars()); |
||
|
1 ignored issue
–
show
|
|||
| 215 | } |
||
| 216 | |||
| 217 | public function doTree() |
||
|
1 ignored issue
–
show
|
|||
| 218 | { |
||
| 219 | $lang = $this->lang; |
||
| 220 | $data = $this->misc->getDatabaseAccessor(); |
||
| 221 | if (!isset($_REQUEST['subject'])) { |
||
| 222 | $_REQUEST['subject'] = 'table'; |
||
| 223 | } |
||
| 224 | |||
| 225 | $subject = urlencode($_REQUEST['subject']); |
||
| 226 | $object = urlencode($_REQUEST[$_REQUEST['subject']]); |
||
| 227 | |||
| 228 | $indexes = $data->getIndexes($object); |
||
| 229 | |||
| 230 | $reqvars = $this->misc->getRequestVars($subject); |
||
| 231 | |||
| 232 | $getIcon = function ($f) { |
||
| 233 | if ('t' == $f['indisprimary']) { |
||
| 234 | return 'PrimaryKey'; |
||
| 235 | } |
||
| 236 | |||
| 237 | if ('t' == $f['indisunique']) { |
||
| 238 | return 'UniqueConstraint'; |
||
| 239 | } |
||
| 240 | |||
| 241 | return 'Index'; |
||
| 242 | }; |
||
| 243 | |||
| 244 | $attrs = [ |
||
| 245 | 'text' => Decorator::field('indname'), |
||
| 246 | 'icon' => Decorator::callback($getIcon), |
||
| 247 | ]; |
||
| 248 | |||
| 249 | return $this->printTree($indexes, $attrs, 'indexes'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Show confirmation of cluster index and perform actual cluster. |
||
| 254 | * |
||
| 255 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 256 | */ |
||
| 257 | public function doClusterIndex($confirm) |
||
| 258 | { |
||
| 259 | $lang = $this->lang; |
||
| 260 | $data = $this->misc->getDatabaseAccessor(); |
||
| 261 | |||
| 262 | if (!isset($_REQUEST['subject'])) { |
||
| 263 | $_REQUEST['subject'] = 'table'; |
||
| 264 | } |
||
| 265 | $subject = urlencode($_REQUEST['subject']); |
||
| 266 | $object = urlencode($_REQUEST[$subject]); |
||
| 267 | |||
| 268 | if ($confirm) { |
||
| 269 | // Default analyze to on |
||
| 270 | $_REQUEST['analyze'] = true; |
||
| 271 | |||
| 272 | $this->printTrail('index'); |
||
| 273 | $this->printTitle($lang['strclusterindex'], 'pg.index.cluster'); |
||
| 274 | |||
| 275 | echo '<p>', sprintf($lang['strconfcluster'], $this->misc->printVal($_REQUEST['index'])), '</p>' . "\n"; |
||
| 276 | |||
| 277 | echo '<form action="' . \SUBFOLDER . '/src/views/indexes.php" method="post">' . "\n"; |
||
| 278 | echo '<p><input type="checkbox" id="analyze" name="analyze"', (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), ' />'; |
||
| 279 | echo "<label for=\"analyze\">{$lang['stranalyze']}</label></p>" . "\n"; |
||
| 280 | echo '<input type="hidden" name="action" value="cluster_index" />' . "\n"; |
||
| 281 | echo '<input type="hidden" name="table" value="', htmlspecialchars($object), '" />' . "\n"; |
||
| 282 | echo '<input type="hidden" name="index" value="', htmlspecialchars($_REQUEST['index']), '" />' . "\n"; |
||
| 283 | echo $this->misc->form; |
||
| 284 | echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />" . "\n"; |
||
| 285 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />" . "\n"; |
||
| 286 | echo '</form>' . "\n"; |
||
| 287 | } else { |
||
| 288 | set_time_limit(0); |
||
| 289 | list($status, $sql) = $data->clusterIndex($object, $_POST['index']); |
||
| 290 | if (0 == $status) { |
||
| 291 | if (isset($_POST['analyze'])) { |
||
| 292 | $status = $data->analyzeDB($object); |
||
| 293 | if (0 == $status) { |
||
| 294 | $this->doDefault($sql . '<br>' . $lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']); |
||
| 295 | } else { |
||
| 296 | $this->doDefault($sql . '<br>' . $lang['stranalyzebad']); |
||
| 297 | } |
||
| 298 | } else { |
||
| 299 | $this->doDefault($sql . '<br>' . $lang['strclusteredgood']); |
||
| 300 | } |
||
| 301 | } else { |
||
| 302 | $this->doDefault($sql . '<br>' . $lang['strclusteredbad']); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | public function doReindex() |
||
|
1 ignored issue
–
show
|
|||
| 308 | { |
||
| 309 | $lang = $this->lang; |
||
| 310 | $data = $this->misc->getDatabaseAccessor(); |
||
| 311 | set_time_limit(0); |
||
| 312 | $status = $data->reindex('INDEX', $_REQUEST['index']); |
||
| 313 | if (0 == $status) { |
||
| 314 | $this->doDefault($lang['strreindexgood']); |
||
| 315 | } else { |
||
| 316 | $this->doDefault($lang['strreindexbad']); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Displays a screen where they can enter a new index. |
||
| 322 | * |
||
| 323 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 324 | */ |
||
| 325 | public function doCreateIndex($msg = '') |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Actually creates the new index in the database. |
||
| 458 | * |
||
| 459 | * @@ Note: this function can't handle columns with commas in them |
||
| 460 | */ |
||
| 461 | public function doSaveCreateIndex() |
||
| 462 | { |
||
| 463 | $lang = $this->lang; |
||
| 464 | $data = $this->misc->getDatabaseAccessor(); |
||
| 465 | |||
| 466 | if (!isset($_POST['subject'])) { |
||
| 467 | $_POST['subject'] = 'table'; |
||
| 468 | } |
||
| 469 | $subject = urlencode($_POST['subject']); |
||
| 470 | $object = urlencode($_POST[$subject]); |
||
| 471 | |||
| 472 | // Handle databases that don't have partial indexes |
||
| 473 | if (!isset($_POST['formWhere'])) { |
||
| 474 | $_POST['formWhere'] = ''; |
||
| 475 | } |
||
| 476 | |||
| 477 | // Default tablespace to null if it isn't set |
||
| 478 | if (!isset($_POST['formSpc'])) { |
||
| 479 | $_POST['formSpc'] = null; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Check that they've given a name and at least one column |
||
| 483 | if ('' == $_POST['formIndexName']) { |
||
| 484 | $this->doCreateIndex($lang['strindexneedsname']); |
||
| 485 | } elseif (!isset($_POST['IndexColumnList']) || '' == $_POST['IndexColumnList']) { |
||
| 486 | $this->doCreateIndex($lang['strindexneedscols']); |
||
| 487 | } else { |
||
| 488 | $status = $data->createIndex( |
||
| 489 | $_POST['formIndexName'], |
||
| 490 | $object, |
||
| 491 | $_POST['IndexColumnList'], |
||
| 492 | $_POST['formIndexType'], |
||
| 493 | isset($_POST['formUnique']), |
||
| 494 | $_POST['formWhere'], |
||
| 495 | $_POST['formSpc'], |
||
| 496 | isset($_POST['formConcur']) |
||
| 497 | ); |
||
| 498 | if (0 == $status) { |
||
| 499 | $this->doDefault($lang['strindexcreated']); |
||
| 500 | } else { |
||
| 501 | $this->doCreateIndex($lang['strindexcreatedbad']); |
||
| 502 | } |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Show confirmation of drop index and perform actual drop. |
||
| 508 | * |
||
| 509 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 510 | */ |
||
| 511 | public function doDropIndex($confirm) |
||
| 543 | } |
||
| 544 | } |
||
| 545 | } |
||
| 546 | } |
||
| 547 |