Total Complexity | 90 |
Total Lines | 786 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TypesController 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 TypesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TypesController extends BaseController |
||
17 | { |
||
18 | public $controller_title = 'strtypes'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Show default list of types in the database. |
||
88 | * |
||
89 | * @param mixed $msg |
||
90 | */ |
||
91 | public function doDefault($msg = '') |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Generate XML for the browser tree. |
||
211 | */ |
||
212 | public function doTree() |
||
213 | { |
||
214 | $data = $this->misc->getDatabaseAccessor(); |
||
215 | |||
216 | $types = $data->getTypes(); |
||
217 | |||
218 | $reqvars = $this->misc->getRequestVars('type'); |
||
219 | |||
220 | $attrs = [ |
||
221 | 'text' => Decorator::field('typname'), |
||
222 | 'icon' => 'Type', |
||
223 | 'toolTip' => Decorator::field('typcomment'), |
||
224 | 'action' => Decorator::actionurl( |
||
225 | 'types', |
||
226 | $reqvars, |
||
227 | [ |
||
228 | 'action' => 'properties', |
||
229 | 'type' => Decorator::field('basename'), |
||
230 | ] |
||
231 | ), |
||
232 | ]; |
||
233 | |||
234 | return $this->printTree($types, $attrs, 'types'); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Show read only properties for a type. |
||
239 | * |
||
240 | * @param mixed $msg |
||
241 | */ |
||
242 | public function doProperties($msg = '') |
||
327 | } |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Show confirmation of drop and perform actual drop. |
||
332 | * |
||
333 | * @param mixed $confirm |
||
334 | */ |
||
335 | public function doDrop($confirm) |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Displays a screen where they can enter a new composite type. |
||
365 | * |
||
366 | * @param mixed $msg |
||
367 | */ |
||
368 | public function doCreateComposite($msg = '') |
||
369 | { |
||
370 | $data = $this->misc->getDatabaseAccessor(); |
||
371 | |||
372 | $this->coalesceArr($_REQUEST, 'stage', 1); |
||
373 | |||
374 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
375 | |||
376 | $this->coalesceArr($_REQUEST, 'fields', ''); |
||
377 | |||
378 | $this->coalesceArr($_REQUEST, 'typcomment', ''); |
||
379 | |||
380 | switch ($_REQUEST['stage']) { |
||
381 | case 1: |
||
382 | $this->printTrail('type'); |
||
383 | $this->printTitle($this->lang['strcreatecomptype'], 'pg.type.create'); |
||
384 | $this->printMsg($msg); |
||
385 | |||
386 | echo '<form action="'.\SUBFOLDER.'/src/views/types" method="post">'.PHP_EOL; |
||
387 | echo '<table>'.PHP_EOL; |
||
388 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>".PHP_EOL; |
||
389 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
390 | htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>".PHP_EOL; |
||
391 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumfields']}</th>".PHP_EOL; |
||
392 | echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
393 | htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>".PHP_EOL; |
||
394 | |||
395 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>".PHP_EOL; |
||
396 | echo "\t\t<td><textarea name=\"typcomment\" rows=\"3\" cols=\"32\">", |
||
397 | htmlspecialchars($_REQUEST['typcomment']), "</textarea></td>\n\t</tr>".PHP_EOL; |
||
398 | |||
399 | echo '</table>'.PHP_EOL; |
||
400 | echo '<p><input type="hidden" name="action" value="create_comp" />'.PHP_EOL; |
||
401 | echo '<input type="hidden" name="stage" value="2" />'.PHP_EOL; |
||
402 | echo $this->misc->form; |
||
403 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />".PHP_EOL; |
||
404 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
405 | echo '</form>'.PHP_EOL; |
||
406 | |||
407 | break; |
||
408 | case 2: |
||
409 | |||
410 | // Check inputs |
||
411 | $fields = trim($_REQUEST['fields']); |
||
412 | if ('' == trim($_REQUEST['name'])) { |
||
413 | $_REQUEST['stage'] = 1; |
||
414 | $this->doCreateComposite($this->lang['strtypeneedsname']); |
||
415 | |||
416 | return; |
||
417 | } |
||
418 | if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields < 1) { |
||
419 | $_REQUEST['stage'] = 1; |
||
420 | $this->doCreateComposite($this->lang['strtypeneedscols']); |
||
421 | |||
422 | return; |
||
423 | } |
||
424 | |||
425 | $types = $data->getTypes(true, false, true); |
||
426 | |||
427 | $this->printTrail('schema'); |
||
428 | $this->printTitle($this->lang['strcreatecomptype'], 'pg.type.create'); |
||
429 | $this->printMsg($msg); |
||
430 | |||
431 | echo '<form action="'.\SUBFOLDER.'/src/views/types" method="post">'.PHP_EOL; |
||
432 | |||
433 | // Output table header |
||
434 | echo '<table>'.PHP_EOL; |
||
435 | echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strfield']}</th><th colspan=\"2\" class=\"data required\">{$this->lang['strtype']}</th>"; |
||
436 | echo "<th class=\"data\">{$this->lang['strlength']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>".PHP_EOL; |
||
437 | |||
438 | for ($i = 0; $i < $_REQUEST['fields']; ++$i) { |
||
439 | if (!isset($_REQUEST['field'][$i])) { |
||
440 | $_REQUEST['field'][$i] = ''; |
||
441 | } |
||
442 | |||
443 | if (!isset($_REQUEST['length'][$i])) { |
||
444 | $_REQUEST['length'][$i] = ''; |
||
445 | } |
||
446 | |||
447 | if (!isset($_REQUEST['colcomment'][$i])) { |
||
448 | $_REQUEST['colcomment'][$i] = ''; |
||
449 | } |
||
450 | |||
451 | echo "\t<tr>\n\t\t<td>", $i + 1, '. </td>'.PHP_EOL; |
||
452 | echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
453 | htmlspecialchars($_REQUEST['field'][$i]), '" /></td>'.PHP_EOL; |
||
454 | echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\">".PHP_EOL; |
||
455 | $types->moveFirst(); |
||
456 | while (!$types->EOF) { |
||
457 | $typname = $types->fields['typname']; |
||
458 | echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), '"', |
||
459 | (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $typname) ? ' selected="selected"' : '', '>', |
||
460 | $this->misc->printVal($typname), '</option>'.PHP_EOL; |
||
461 | $types->moveNext(); |
||
462 | } |
||
463 | echo "\t\t\t</select>\n\t\t</td>".PHP_EOL; |
||
464 | |||
465 | // Output array type selector |
||
466 | echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">".PHP_EOL; |
||
467 | echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', '></option>'.PHP_EOL; |
||
468 | echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', '>[ ]</option>'.PHP_EOL; |
||
469 | echo "\t\t\t</select>\n\t\t</td>".PHP_EOL; |
||
470 | |||
471 | echo "\t\t<td><input name=\"length[{$i}]\" size=\"10\" value=\"", |
||
472 | htmlspecialchars($_REQUEST['length'][$i]), '" /></td>'.PHP_EOL; |
||
473 | echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"", |
||
474 | htmlspecialchars($_REQUEST['colcomment'][$i]), "\" /></td>\n\t</tr>".PHP_EOL; |
||
475 | } |
||
476 | echo '</table>'.PHP_EOL; |
||
477 | echo '<p><input type="hidden" name="action" value="create_comp" />'.PHP_EOL; |
||
478 | echo '<input type="hidden" name="stage" value="3" />'.PHP_EOL; |
||
479 | echo $this->misc->form; |
||
480 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), '" />'.PHP_EOL; |
||
481 | echo '<input type="hidden" name="fields" value="', htmlspecialchars($_REQUEST['fields']), '" />'.PHP_EOL; |
||
482 | echo '<input type="hidden" name="typcomment" value="', htmlspecialchars($_REQUEST['typcomment']), '" />'.PHP_EOL; |
||
483 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />".PHP_EOL; |
||
484 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
485 | echo '</form>'.PHP_EOL; |
||
486 | |||
487 | break; |
||
488 | case 3: |
||
489 | |||
490 | // Check inputs |
||
491 | $fields = trim($_REQUEST['fields']); |
||
492 | if ('' == trim($_REQUEST['name'])) { |
||
493 | $_REQUEST['stage'] = 1; |
||
494 | $this->doCreateComposite($this->lang['strtypeneedsname']); |
||
495 | |||
496 | return; |
||
497 | } |
||
498 | if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields <= 0) { |
||
499 | $_REQUEST['stage'] = 1; |
||
500 | $this->doCreateComposite($this->lang['strtypeneedscols']); |
||
501 | |||
502 | return; |
||
503 | } |
||
504 | |||
505 | $status = $data->createCompositeType( |
||
506 | $_REQUEST['name'], |
||
507 | $_REQUEST['fields'], |
||
508 | $_REQUEST['field'], |
||
509 | $_REQUEST['type'], |
||
510 | $_REQUEST['array'], |
||
511 | $_REQUEST['length'], |
||
512 | $_REQUEST['colcomment'], |
||
513 | $_REQUEST['typcomment'] |
||
514 | ); |
||
515 | |||
516 | if (0 == $status) { |
||
517 | $this->doDefault($this->lang['strtypecreated']); |
||
518 | } elseif ($status == -1) { |
||
519 | $_REQUEST['stage'] = 2; |
||
520 | $this->doCreateComposite($this->lang['strtypeneedsfield']); |
||
521 | |||
522 | return; |
||
523 | } else { |
||
524 | $_REQUEST['stage'] = 2; |
||
525 | $this->doCreateComposite($this->lang['strtypecreatedbad']); |
||
526 | |||
527 | return; |
||
528 | } |
||
529 | |||
530 | break; |
||
531 | default: |
||
532 | echo "<p>{$this->lang['strinvalidparam']}</p>".PHP_EOL; |
||
533 | } |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Displays a screen where they can enter a new enum type. |
||
538 | * |
||
539 | * @param mixed $msg |
||
540 | */ |
||
541 | public function doCreateEnum($msg = '') |
||
542 | { |
||
543 | $data = $this->misc->getDatabaseAccessor(); |
||
544 | |||
545 | $this->coalesceArr($_REQUEST, 'stage', 1); |
||
546 | |||
547 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
548 | |||
549 | $this->coalesceArr($_REQUEST, 'values', ''); |
||
550 | |||
551 | $this->coalesceArr($_REQUEST, 'typcomment', ''); |
||
552 | |||
553 | switch ($_REQUEST['stage']) { |
||
554 | case 1: |
||
555 | $this->printTrail('type'); |
||
556 | $this->printTitle($this->lang['strcreateenumtype'], 'pg.type.create'); |
||
557 | $this->printMsg($msg); |
||
558 | |||
559 | echo '<form action="'.\SUBFOLDER.'/src/views/types" method="post">'.PHP_EOL; |
||
560 | echo '<table>'.PHP_EOL; |
||
561 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>".PHP_EOL; |
||
562 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
563 | htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>".PHP_EOL; |
||
564 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumvalues']}</th>".PHP_EOL; |
||
565 | echo "\t\t<td class=\"data\"><input name=\"values\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
566 | htmlspecialchars($_REQUEST['values']), "\" /></td>\n\t</tr>".PHP_EOL; |
||
567 | |||
568 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>".PHP_EOL; |
||
569 | echo "\t\t<td><textarea name=\"typcomment\" rows=\"3\" cols=\"32\">", |
||
570 | htmlspecialchars($_REQUEST['typcomment']), "</textarea></td>\n\t</tr>".PHP_EOL; |
||
571 | |||
572 | echo '</table>'.PHP_EOL; |
||
573 | echo '<p><input type="hidden" name="action" value="create_enum" />'.PHP_EOL; |
||
574 | echo '<input type="hidden" name="stage" value="2" />'.PHP_EOL; |
||
575 | echo $this->misc->form; |
||
576 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />".PHP_EOL; |
||
577 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
578 | echo '</form>'.PHP_EOL; |
||
579 | |||
580 | break; |
||
581 | case 2: |
||
582 | |||
583 | // Check inputs |
||
584 | $values = trim($_REQUEST['values']); |
||
585 | if ('' == trim($_REQUEST['name'])) { |
||
586 | $_REQUEST['stage'] = 1; |
||
587 | $this->doCreateEnum($this->lang['strtypeneedsname']); |
||
588 | |||
589 | return; |
||
590 | } |
||
591 | if ('' == $values || !is_numeric($values) || $values != (int) $values || $values < 1) { |
||
592 | $_REQUEST['stage'] = 1; |
||
593 | $this->doCreateEnum($this->lang['strtypeneedsvals']); |
||
594 | |||
595 | return; |
||
596 | } |
||
597 | |||
598 | $this->printTrail('schema'); |
||
599 | $this->printTitle($this->lang['strcreateenumtype'], 'pg.type.create'); |
||
600 | $this->printMsg($msg); |
||
601 | |||
602 | echo '<form action="'.\SUBFOLDER.'/src/views/types" method="post">'.PHP_EOL; |
||
603 | |||
604 | // Output table header |
||
605 | echo '<table>'.PHP_EOL; |
||
606 | echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strvalue']}</th></tr>".PHP_EOL; |
||
607 | |||
608 | for ($i = 0; $i < $_REQUEST['values']; ++$i) { |
||
609 | if (!isset($_REQUEST['value'][$i])) { |
||
610 | $_REQUEST['value'][$i] = ''; |
||
611 | } |
||
612 | |||
613 | echo "\t<tr>\n\t\t<td>", $i + 1, '. </td>'.PHP_EOL; |
||
614 | echo "\t\t<td><input name=\"value[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
615 | htmlspecialchars($_REQUEST['value'][$i]), "\" /></td>\n\t</tr>".PHP_EOL; |
||
616 | } |
||
617 | echo '</table>'.PHP_EOL; |
||
618 | echo '<p><input type="hidden" name="action" value="create_enum" />'.PHP_EOL; |
||
619 | echo '<input type="hidden" name="stage" value="3" />'.PHP_EOL; |
||
620 | echo $this->misc->form; |
||
621 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), '" />'.PHP_EOL; |
||
622 | echo '<input type="hidden" name="values" value="', htmlspecialchars($_REQUEST['values']), '" />'.PHP_EOL; |
||
623 | echo '<input type="hidden" name="typcomment" value="', htmlspecialchars($_REQUEST['typcomment']), '" />'.PHP_EOL; |
||
624 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />".PHP_EOL; |
||
625 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
626 | echo '</form>'.PHP_EOL; |
||
627 | |||
628 | break; |
||
629 | case 3: |
||
630 | |||
631 | // Check inputs |
||
632 | $values = trim($_REQUEST['values']); |
||
633 | if ('' == trim($_REQUEST['name'])) { |
||
634 | $_REQUEST['stage'] = 1; |
||
635 | $this->doCreateEnum($this->lang['strtypeneedsname']); |
||
636 | |||
637 | return; |
||
638 | } |
||
639 | if ('' == $values || !is_numeric($values) || $values != (int) $values || $values <= 0) { |
||
640 | $_REQUEST['stage'] = 1; |
||
641 | $this->doCreateEnum($this->lang['strtypeneedsvals']); |
||
642 | |||
643 | return; |
||
644 | } |
||
645 | |||
646 | $status = $data->createEnumType($_REQUEST['name'], $_REQUEST['value'], $_REQUEST['typcomment']); |
||
647 | |||
648 | if (0 == $status) { |
||
649 | $this->doDefault($this->lang['strtypecreated']); |
||
650 | } elseif ($status == -1) { |
||
651 | $_REQUEST['stage'] = 2; |
||
652 | $this->doCreateEnum($this->lang['strtypeneedsvalue']); |
||
653 | |||
654 | return; |
||
655 | } else { |
||
656 | $_REQUEST['stage'] = 2; |
||
657 | $this->doCreateEnum($this->lang['strtypecreatedbad']); |
||
658 | |||
659 | return; |
||
660 | } |
||
661 | |||
662 | break; |
||
663 | default: |
||
664 | echo "<p>{$this->lang['strinvalidparam']}</p>".PHP_EOL; |
||
665 | } |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * Displays a screen where they can enter a new type. |
||
670 | * |
||
671 | * @param mixed $msg |
||
672 | */ |
||
673 | public function doCreate($msg = '') |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Actually creates the new type in the database. |
||
773 | */ |
||
774 | public function doSaveCreate() |
||
806 |