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 |
||
| 29 | class Application extends BaseApplication |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const APP_NAME = 'n98-magerun'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | const APP_VERSION = '1.97.29'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private static $logo = " |
||
| 55 | ___ ___ |
||
| 56 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |
||
| 57 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ |
||
| 58 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_| |
||
| 59 | |___/ |
||
| 60 | "; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Shadow copy of the Application parent when using this concrete setAutoExit() implementation |
||
| 64 | * |
||
| 65 | * @see \Symfony\Component\Console\Application::$autoExit |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | private $autoExitShadow = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ClassLoader |
||
| 72 | */ |
||
| 73 | protected $autoloader; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Config |
||
| 77 | */ |
||
| 78 | protected $config; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 82 | * @var ConfigurationLoader |
||
| 83 | */ |
||
| 84 | private $configurationLoaderInjected; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $_magentoRootFolder = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected $_magentoEnterprise = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var EntryPoint |
||
| 103 | */ |
||
| 104 | protected $_magento2EntryPoint = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $_isPharMode = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $_magerunStopFileFound = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | protected $_magerunStopFileFolder = null; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected $_isInitialized = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var EventDispatcher |
||
| 128 | */ |
||
| 129 | protected $dispatcher; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * If root dir is set by root-dir option this flag is true |
||
| 133 | * |
||
| 134 | * @var bool |
||
| 135 | */ |
||
| 136 | protected $_directRootDir = false; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $_magentoDetected = false; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param ClassLoader $autoloader |
||
| 145 | */ |
||
| 146 | public function __construct($autoloader = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param bool $boolean |
||
| 154 | * @return bool previous auto-exit state |
||
| 155 | */ |
||
| 156 | public function setAutoExit($boolean) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return InputDefinition |
||
| 167 | */ |
||
| 168 | protected function getDefaultInputDefinition() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Search for magento root folder |
||
| 210 | * |
||
| 211 | * @param InputInterface $input [optional] |
||
| 212 | * @param OutputInterface $output [optional] |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Add own helpers to helperset. |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | protected function registerHelpers() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param InputInterface $input |
||
| 282 | * |
||
| 283 | * @return ArgvInput|InputInterface |
||
| 284 | */ |
||
| 285 | protected function checkConfigCommandAlias(InputInterface $input) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Command $command |
||
| 294 | */ |
||
| 295 | protected function registerConfigCommandAlias(Command $command) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Adds autoloader prefixes from user's config |
||
| 304 | */ |
||
| 305 | protected function registerCustomAutoloaders() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function hasCustomCommands() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | protected function registerCustomCommands() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $class |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | protected function isCommandDisabled($class) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Override standard command registration. We want alias support. |
||
| 347 | * |
||
| 348 | * @param Command $command |
||
| 349 | * |
||
| 350 | * @return Command |
||
| 351 | */ |
||
| 352 | public function add(Command $command) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param bool $mode |
||
| 363 | */ |
||
| 364 | public function setPharMode($mode) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function isPharMode() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @TODO Move logic into "EventSubscriber" |
||
| 379 | * |
||
| 380 | * @param OutputInterface $output |
||
| 381 | * @return null|false |
||
| 382 | */ |
||
| 383 | public function checkVarDir(OutputInterface $output) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Loads and initializes the Magento application |
||
| 443 | * |
||
| 444 | * @param bool $soft |
||
| 445 | * |
||
| 446 | * @return bool false if magento root folder is not set, true otherwise |
||
| 447 | */ |
||
| 448 | public function initMagento($soft = false) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public function getHelp() |
||
| 471 | |||
| 472 | public function getLongVersion() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return boolean |
||
| 479 | */ |
||
| 480 | public function isMagentoEnterprise() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public function getMagentoRootFolder() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param string $magentoRootFolder |
||
| 495 | */ |
||
| 496 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return int |
||
| 503 | */ |
||
| 504 | public function getMagentoMajorVersion() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return ClassLoader |
||
| 511 | */ |
||
| 512 | public function getAutoloader() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param ClassLoader $autoloader |
||
| 519 | */ |
||
| 520 | public function setAutoloader(ClassLoader $autoloader) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get config array |
||
| 527 | * |
||
| 528 | * Specify one key per parameter to traverse the config. Then returns null |
||
| 529 | * if the path of the key(s) can not be obtained. |
||
| 530 | * |
||
| 531 | * @param string|int $key ... (optional) |
||
| 532 | * |
||
| 533 | * @return array|null |
||
| 534 | */ |
||
| 535 | public function getConfig($key = null) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param array $config |
||
| 555 | */ |
||
| 556 | public function setConfig($config) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @return boolean |
||
| 563 | */ |
||
| 564 | public function isMagerunStopFileFound() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Runs the current application with possible command aliases |
||
| 571 | * |
||
| 572 | * @param InputInterface $input An Input instance |
||
| 573 | * @param OutputInterface $output An Output instance |
||
| 574 | * |
||
| 575 | * @return integer 0 if everything went fine, or an error code |
||
| 576 | */ |
||
| 577 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @param InputInterface $input [optional] |
||
| 598 | * @param OutputInterface $output [optional] |
||
| 599 | * |
||
| 600 | * @return int |
||
| 601 | */ |
||
| 602 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param array $initConfig [optional] |
||
| 637 | * @param InputInterface $input [optional] |
||
| 638 | * @param OutputInterface $output [optional] |
||
| 639 | * |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param array $initConfig [optional] |
||
| 687 | * @param InputInterface $input [optional] |
||
| 688 | * @param OutputInterface $output [optional] |
||
| 689 | */ |
||
| 690 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @return void |
||
| 701 | */ |
||
| 702 | protected function registerEventSubscribers() |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @param InputInterface $input |
||
| 714 | * @return bool |
||
| 715 | * @deprecated 1.97.27 |
||
| 716 | */ |
||
| 717 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @param InputInterface $input |
||
| 729 | * @return string |
||
| 730 | */ |
||
| 731 | protected function _checkRootDirOption(InputInterface $input) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Set root dir (chdir()) of magento directory |
||
| 741 | * |
||
| 742 | * @param string $path to Magento directory |
||
| 743 | */ |
||
| 744 | private function setRootDir($path) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * @param bool $soft |
||
| 759 | * |
||
| 760 | * @return void |
||
| 761 | */ |
||
| 762 | protected function _initMagento1($soft = false) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @return void |
||
| 779 | */ |
||
| 780 | protected function _initMagento2() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 787 | * |
||
| 788 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 789 | */ |
||
| 790 | private function outputMagerunCompatibilityNotice($version) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @return EventDispatcher |
||
| 828 | */ |
||
| 829 | public function getDispatcher() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @param array $initConfig |
||
| 836 | * @param OutputInterface $output |
||
| 837 | * @return ConfigurationLoader |
||
| 838 | */ |
||
| 839 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
| 853 | |||
| 854 | /** |
||
| 855 | * @param ConfigurationLoader $configurationLoader |
||
| 856 | * |
||
| 857 | * @return $this |
||
| 858 | */ |
||
| 859 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * @param OutputInterface $output |
||
| 874 | */ |
||
| 875 | protected function _addOutputStyles(OutputInterface $output) |
||
| 880 | } |
||
| 881 |