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 |
||
| 31 | class Application extends BaseApplication |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const APP_NAME = 'n98-magerun'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const APP_VERSION = '1.97.28'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $logo = " |
||
| 57 | ___ ___ |
||
| 58 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |
||
| 59 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ |
||
| 60 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_| |
||
| 61 | |___/ |
||
| 62 | "; |
||
| 63 | /** |
||
| 64 | * @var ClassLoader |
||
| 65 | */ |
||
| 66 | protected $autoloader; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Config |
||
| 70 | */ |
||
| 71 | protected $config; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 75 | * @var ConfigurationLoader |
||
| 76 | */ |
||
| 77 | private $configurationLoaderInjected; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $_magentoRootFolder = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $_magentoEnterprise = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var EntryPoint |
||
| 96 | */ |
||
| 97 | protected $_magento2EntryPoint = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $_isPharMode = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $_magerunStopFileFound = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $_magerunStopFileFolder = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $_isInitialized = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var EventDispatcher |
||
| 121 | */ |
||
| 122 | protected $dispatcher; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * If root dir is set by root-dir option this flag is true |
||
| 126 | * |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | protected $_directRootDir = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $_magentoDetected = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | private static $_isScriptEnvironment = false; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param ClassLoader $autoloader |
||
| 143 | */ |
||
| 144 | public function __construct($autoloader = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return InputDefinition |
||
| 152 | */ |
||
| 153 | protected function getDefaultInputDefinition() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Search for magento root folder |
||
| 195 | * |
||
| 196 | * @param InputInterface $input [optional] |
||
| 197 | * @param OutputInterface $output [optional] |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add own helpers to helperset. |
||
| 241 | * |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | protected function registerHelpers() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param InputInterface $input |
||
| 267 | * |
||
| 268 | * @return ArgvInput|InputInterface |
||
| 269 | */ |
||
| 270 | protected function checkConfigCommandAlias(InputInterface $input) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param Command $command |
||
| 279 | */ |
||
| 280 | protected function registerConfigCommandAlias(Command $command) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds autoloader prefixes from user's config |
||
| 289 | */ |
||
| 290 | protected function registerCustomAutoloaders() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | protected function hasCustomCommands() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | protected function registerCustomCommands() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $class |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | protected function isCommandDisabled($class) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Override standard command registration. We want alias support. |
||
| 334 | * |
||
| 335 | * @param Command $command |
||
| 336 | * |
||
| 337 | * @return Command |
||
| 338 | */ |
||
| 339 | public function add(Command $command) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param bool $mode |
||
| 350 | */ |
||
| 351 | public function setPharMode($mode) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function isPharMode() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @TODO Move logic into "EventSubscriber" |
||
| 366 | * |
||
| 367 | * @param OutputInterface $output |
||
| 368 | * @return null|false |
||
| 369 | */ |
||
| 370 | public function checkVarDir(OutputInterface $output) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Loads and initializes the Magento application |
||
| 430 | * |
||
| 431 | * @param bool $soft |
||
| 432 | * |
||
| 433 | * @return bool false if magento root folder is not set, true otherwise |
||
| 434 | */ |
||
| 435 | public function initMagento($soft = false) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getHelp() |
||
| 458 | |||
| 459 | public function getLongVersion() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return boolean |
||
| 466 | */ |
||
| 467 | public function isMagentoEnterprise() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function getMagentoRootFolder() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param string $magentoRootFolder |
||
| 482 | */ |
||
| 483 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return int |
||
| 490 | */ |
||
| 491 | public function getMagentoMajorVersion() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return ClassLoader |
||
| 498 | */ |
||
| 499 | public function getAutoloader() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param ClassLoader $autoloader |
||
| 506 | */ |
||
| 507 | public function setAutoloader(ClassLoader $autoloader) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get config array |
||
| 514 | * |
||
| 515 | * Specify one key per parameter to traverse the config. Then returns null |
||
| 516 | * if the path of the key(s) can not be obtained. |
||
| 517 | * |
||
| 518 | * @param string|int $key ... (optional) |
||
| 519 | * |
||
| 520 | * @return array|null |
||
| 521 | */ |
||
| 522 | public function getConfig($key = null) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param array $config |
||
| 542 | */ |
||
| 543 | public function setConfig($config) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return boolean |
||
| 550 | */ |
||
| 551 | public function isMagerunStopFileFound() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Runs the current application with possible command aliases |
||
| 558 | * |
||
| 559 | * @param InputInterface $input An Input instance |
||
| 560 | * @param OutputInterface $output An Output instance |
||
| 561 | * |
||
| 562 | * @return integer 0 if everything went fine, or an error code |
||
| 563 | */ |
||
| 564 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param InputInterface $input [optional] |
||
| 585 | * @param OutputInterface $output [optional] |
||
| 586 | * |
||
| 587 | * @return int |
||
| 588 | */ |
||
| 589 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @param array $initConfig [optional] |
||
| 624 | * @param InputInterface $input [optional] |
||
| 625 | * @param OutputInterface $output [optional] |
||
| 626 | * |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param array $initConfig [optional] |
||
| 674 | * @param InputInterface $input [optional] |
||
| 675 | * @param OutputInterface $output [optional] |
||
| 676 | */ |
||
| 677 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @return void |
||
| 688 | */ |
||
| 689 | protected function registerEventSubscribers() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param InputInterface $input |
||
| 701 | * @return bool |
||
| 702 | * @deprecated 1.97.27 |
||
| 703 | */ |
||
| 704 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @param InputInterface $input |
||
| 716 | * @return string |
||
| 717 | */ |
||
| 718 | protected function _checkRootDirOption(InputInterface $input) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Set root dir (chdir()) of magento directory |
||
| 728 | * |
||
| 729 | * @param string $path to Magento directory |
||
| 730 | */ |
||
| 731 | private function setRootDir($path) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 746 | * and $this unbound. |
||
| 747 | * |
||
| 748 | * @param string $path |
||
| 749 | */ |
||
| 750 | private function requireOnce($path) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @param bool $soft |
||
| 764 | * |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | protected function _initMagento1($soft = false) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * @return void |
||
| 791 | */ |
||
| 792 | protected function _initMagento2() |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 799 | * |
||
| 800 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 801 | */ |
||
| 802 | private function outputMagerunCompatibilityNotice($version) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @return EventDispatcher |
||
| 840 | */ |
||
| 841 | public function getDispatcher() |
||
| 845 | |||
| 846 | /** |
||
| 847 | * @param array $initConfig |
||
| 848 | * @param OutputInterface $output |
||
| 849 | * @return ConfigurationLoader |
||
| 850 | */ |
||
| 851 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @param ConfigurationLoader $configurationLoader |
||
| 868 | * |
||
| 869 | * @return $this |
||
| 870 | */ |
||
| 871 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 883 | |||
| 884 | /** |
||
| 885 | * @param OutputInterface $output |
||
| 886 | */ |
||
| 887 | protected function _addOutputStyles(OutputInterface $output) |
||
| 892 | /** |
||
| 893 | * @return bool |
||
| 894 | */ |
||
| 895 | public function isScriptEnvironment() |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param $state bool |
||
| 902 | */ |
||
| 903 | public function setScriptEnvironment($state) |
||
| 907 | } |
||
| 908 |