Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class Application |
||
| 53 | { |
||
| 54 | use Common\Helper; |
||
| 55 | use Common\Singleton; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string Application path |
||
| 59 | */ |
||
| 60 | protected $path; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string Environment name |
||
| 64 | */ |
||
| 65 | protected $environment = 'production'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \Exception |
||
| 69 | */ |
||
| 70 | protected $exception; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool Debug application flag |
||
| 74 | */ |
||
| 75 | protected $debugFlag = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool Layout flag |
||
| 79 | */ |
||
| 80 | protected $layoutFlag = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array Stack of widgets closures |
||
| 84 | */ |
||
| 85 | protected $widgets = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array Stack of API closures |
||
| 89 | */ |
||
| 90 | protected $api = array(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get application environment |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getEnvironment() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get path to Application |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 669 | public function getPath() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Check debug flag |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 4 | public function isDebug() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Check Layout flag |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | 5 | public function hasLayout() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Set Layout template and/or flag |
||
| 143 | * |
||
| 144 | * @param bool|string $flag |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | 669 | public function useLayout($flag = true) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Set JSON presentation |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function useJson() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Initialize process |
||
| 171 | * |
||
| 172 | * @param string $environment |
||
| 173 | * @throws ApplicationException |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function init($environment = 'production') |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Run application |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | public function run() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Initial Request instance |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected function initRequest() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Initial Response instance |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | protected function initResponse() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Initial Router instance |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | 669 | protected function initRouter() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Initial controller view |
||
| 273 | * |
||
| 274 | * @param string $module |
||
| 275 | * @param string $controller |
||
| 276 | * @return View |
||
| 277 | */ |
||
| 278 | 2 | protected function initView($module, $controller) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Process application |
||
| 300 | * |
||
| 301 | * Note: |
||
| 302 | * - Why you don't use "X-" prefix for custom headers? |
||
| 303 | * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | 2 | public function process() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Pre process |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | * @throws ApplicationException |
||
| 321 | */ |
||
| 322 | 2 | protected function preProcess() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Do process |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | 2 | protected function doProcess() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Post process |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | 2 | protected function postProcess() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Dispatch controller with params |
||
| 493 | * |
||
| 494 | * Call dispatch from any \Bluz\Package |
||
| 495 | * Application::getInstance()->dispatch($module, $controller, array $params); |
||
| 496 | * |
||
| 497 | * @param string $module |
||
| 498 | * @param string $controller |
||
| 499 | * @param array $params |
||
| 500 | * @return View|callable |
||
| 501 | * @throws ApplicationException |
||
| 502 | */ |
||
| 503 | 3 | public function dispatch($module, $controller, $params = array()) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Pre dispatch mount point |
||
| 516 | * |
||
| 517 | * @param string $module |
||
| 518 | * @param string $controller |
||
| 519 | * @param array $params |
||
| 520 | * @return void |
||
| 521 | */ |
||
| 522 | 3 | protected function preDispatch($module, $controller, $params = array()) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Do dispatch |
||
| 534 | * |
||
| 535 | * @param string $module |
||
| 536 | * @param string $controller |
||
| 537 | * @param array $params |
||
| 538 | * @return View|callable |
||
| 539 | * @throws ApplicationException |
||
| 540 | */ |
||
| 541 | 2 | protected function doDispatch($module, $controller, $params = array()) |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Post dispatch mount point |
||
| 617 | * |
||
| 618 | * @param string $module |
||
| 619 | * @param string $controller |
||
| 620 | * @param array $params |
||
| 621 | * @return void |
||
| 622 | */ |
||
| 623 | 2 | protected function postDispatch($module, $controller, $params = array()) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Render, is send Response |
||
| 630 | * |
||
| 631 | * @return void |
||
| 632 | */ |
||
| 633 | public function render() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Get Response instance |
||
| 642 | * |
||
| 643 | * @return \Bluz\Response\Response |
||
| 644 | */ |
||
| 645 | 1 | public function getResponse() |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get Request instance |
||
| 652 | * |
||
| 653 | * @return \Zend\Diactoros\ServerRequest |
||
| 654 | */ |
||
| 655 | 1 | public function getRequest() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * setException |
||
| 662 | * |
||
| 663 | * @param \Exception $exception |
||
| 664 | * @return void |
||
| 665 | */ |
||
| 666 | 1 | public function setException($exception) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * getException |
||
| 673 | * |
||
| 674 | * @return \Exception |
||
| 675 | */ |
||
| 676 | public function getException() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Widget call |
||
| 683 | * |
||
| 684 | * Call widget from any \Bluz\Package |
||
| 685 | * <code> |
||
| 686 | * Application::getInstance()->widget($module, $widget, array $params); |
||
| 687 | * </code> |
||
| 688 | * |
||
| 689 | * @param string $module |
||
| 690 | * @param string $widget |
||
| 691 | * @param array $params |
||
| 692 | * @return \Closure |
||
| 693 | * @throws ApplicationException |
||
| 694 | */ |
||
| 695 | 1 | public function widget($module, $widget, $params = array()) |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Api call |
||
| 730 | * |
||
| 731 | * Call API from any \Bluz\Package |
||
| 732 | * <code> |
||
| 733 | * Application::getInstance()->api($module, $widget, array $params); |
||
| 734 | * </code> |
||
| 735 | * |
||
| 736 | * @param string $module |
||
| 737 | * @param string $method |
||
| 738 | * @return \Closure |
||
| 739 | * @throws ApplicationException |
||
| 740 | */ |
||
| 741 | 1 | public function api($module, $method) |
|
| 768 | |||
| 769 | /** |
||
| 770 | * Retrieve reflection for anonymous function |
||
| 771 | * |
||
| 772 | * @param string $file |
||
| 773 | * @return Reflection |
||
| 774 | * @throws ApplicationException |
||
| 775 | */ |
||
| 776 | 670 | public function reflection($file) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Is allowed controller/widget/etc |
||
| 791 | * |
||
| 792 | * @param string $module |
||
| 793 | * @param string $controller |
||
| 794 | * @return bool |
||
| 795 | * @throws ApplicationException |
||
| 796 | */ |
||
| 797 | 4 | public function isAllowed($module, $controller) |
|
| 807 | |||
| 808 | /** |
||
| 809 | * Get controller file |
||
| 810 | * |
||
| 811 | * @param string $module |
||
| 812 | * @param string $controller |
||
| 813 | * @return string |
||
| 814 | * @throws ApplicationException |
||
| 815 | */ |
||
| 816 | 4 | protected function getControllerFile($module, $controller) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Get widget file |
||
| 830 | * |
||
| 831 | * @param string $module |
||
| 832 | * @param string $widget |
||
| 833 | * @return string |
||
| 834 | * @throws ApplicationException |
||
| 835 | */ |
||
| 836 | 1 | protected function getWidgetFile($module, $widget) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Get API file |
||
| 850 | * |
||
| 851 | * @param string $module |
||
| 852 | * @param string $method |
||
| 853 | * @return string |
||
| 854 | * @throws ApplicationException |
||
| 855 | */ |
||
| 856 | 1 | protected function getApiFile($module, $method) |
|
| 867 | |||
| 868 | /** |
||
| 869 | * Finally method |
||
| 870 | * |
||
| 871 | * @return void |
||
| 872 | */ |
||
| 873 | public function finish() |
||
| 877 | } |
||
| 878 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.