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-magerun2'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const APP_VERSION = '1.3.0'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 47 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private static $logo = " |
||
| 53 | ___ ___ ___ |
||
| 54 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |_ ) |
||
| 55 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ / / |
||
| 56 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_/___| |
||
| 57 | |___/ |
||
| 58 | "; |
||
| 59 | /** |
||
| 60 | * @var ClassLoader |
||
| 61 | */ |
||
| 62 | protected $autoloader; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Config |
||
| 66 | */ |
||
| 67 | protected $config; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 71 | * @var ConfigurationLoader |
||
| 72 | */ |
||
| 73 | private $configurationLoaderInjected; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string [optional] root folder not detected, but set via public setter |
||
| 77 | * @see setMagentoRootFolder() |
||
| 78 | */ |
||
| 79 | private $magentoRootFolderInjected; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var int Magento Major Version to operate on by this Magerun application |
||
| 83 | */ |
||
| 84 | private $magerunMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $_isPharMode = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected $_isInitialized = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var EventDispatcher |
||
| 98 | */ |
||
| 99 | protected $dispatcher; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * If root dir is set by root-dir option this flag is true |
||
| 103 | * |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $_directRootDir = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var DetectionResult of the Magento application (e.g. v1/v2, Enterprise/Community, root-path) |
||
| 110 | */ |
||
| 111 | private $detectionResult; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var ObjectManager |
||
| 115 | */ |
||
| 116 | protected $_objectManager = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var boolean |
||
| 120 | */ |
||
| 121 | private $autoExit = true; |
||
|
|
|||
| 122 | |||
| 123 | /** |
||
| 124 | * @param ClassLoader $autoloader |
||
| 125 | */ |
||
| 126 | public function __construct($autoloader = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return InputDefinition |
||
| 134 | */ |
||
| 135 | protected function getDefaultInputDefinition() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets whether to automatically exit after a command execution or not. |
||
| 188 | * |
||
| 189 | * Implemented on this level to allow early exit on configuration exceptions |
||
| 190 | * |
||
| 191 | * @see run() |
||
| 192 | * |
||
| 193 | * @param bool $boolean Whether to automatically exit after a command execution or not |
||
| 194 | */ |
||
| 195 | public function setAutoExit($boolean) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Search for magento root folder |
||
| 203 | * |
||
| 204 | * @param InputInterface $input [optional] |
||
| 205 | * @param OutputInterface $output [optional] |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add own helpers to helperset. |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function registerHelpers() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Try to bootstrap magento 2 and load cli application |
||
| 252 | * |
||
| 253 | * @param OutputInterface $output |
||
| 254 | */ |
||
| 255 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Override standard command registration. We want alias support. |
||
| 284 | * |
||
| 285 | * @param Command $command |
||
| 286 | * |
||
| 287 | * @return Command |
||
| 288 | */ |
||
| 289 | public function add(Command $command) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param bool $mode |
||
| 300 | */ |
||
| 301 | public function setPharMode($mode) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function isPharMode() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @TODO Move logic into "EventSubscriber" |
||
| 316 | * |
||
| 317 | * @param OutputInterface $output |
||
| 318 | * @return null|false |
||
| 319 | */ |
||
| 320 | public function checkVarDir(OutputInterface $output) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Loads and initializes the Magento application |
||
| 379 | * |
||
| 380 | * @param bool $soft |
||
| 381 | * |
||
| 382 | * @return bool false if magento root folder is not set, true otherwise |
||
| 383 | */ |
||
| 384 | public function initMagento($soft = false) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getHelp() |
||
| 407 | |||
| 408 | public function getLongVersion() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return boolean |
||
| 415 | */ |
||
| 416 | public function isMagentoEnterprise() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param bool $preventException [optional] on uninitialized magento root folder (returns null then, caution!) |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function getMagentoRootFolder($preventException = false) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param string $magentoRootFolder |
||
| 444 | */ |
||
| 445 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @return int |
||
| 452 | */ |
||
| 453 | public function getMagentoMajorVersion() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return ClassLoader |
||
| 460 | */ |
||
| 461 | public function getAutoloader() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param ClassLoader $autoloader |
||
| 468 | */ |
||
| 469 | public function setAutoloader(ClassLoader $autoloader) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | public function getConfig() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param array $config |
||
| 485 | */ |
||
| 486 | public function setConfig($config) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @deprecated 1.3.0 |
||
| 493 | * @return boolean |
||
| 494 | */ |
||
| 495 | public function isMagerunStopFileFound() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Runs the current application with possible command aliases |
||
| 504 | * |
||
| 505 | * @param InputInterface $input An Input instance |
||
| 506 | * @param OutputInterface $output An Output instance |
||
| 507 | * |
||
| 508 | * @return integer 0 if everything went fine, or an error code |
||
| 509 | */ |
||
| 510 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param InputInterface $input [optional] |
||
| 531 | * @param OutputInterface $output [optional] |
||
| 532 | * |
||
| 533 | * @return int |
||
| 534 | */ |
||
| 535 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param array $initConfig [optional] |
||
| 576 | * @param InputInterface $input [optional] |
||
| 577 | * @param OutputInterface $output [optional] |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function init(array $initConfig = array(), 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 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @return void |
||
| 650 | */ |
||
| 651 | protected function registerEventSubscribers() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param InputInterface $input |
||
| 663 | * @return bool |
||
| 664 | */ |
||
| 665 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | protected function _checkSkipMagento2CoreCommandsOption(InputInterface $input) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param InputInterface $input |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | protected function _checkRootDirOption(InputInterface $input) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Set root dir (chdir()) of magento directory |
||
| 692 | * |
||
| 693 | * @param string $path to Magento directory |
||
| 694 | */ |
||
| 695 | private function setRootDir($path) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 710 | * and $this unbound. |
||
| 711 | * |
||
| 712 | * @param string $path |
||
| 713 | */ |
||
| 714 | private function requireOnce($path) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @param bool $soft |
||
| 728 | * |
||
| 729 | * @return void |
||
| 730 | */ |
||
| 731 | protected function _initMagento1($soft = false) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @return void |
||
| 738 | */ |
||
| 739 | protected function _initMagento2() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 759 | * |
||
| 760 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 761 | */ |
||
| 762 | private function outputMagerunCompatibilityNotice($version) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @return EventDispatcher |
||
| 800 | */ |
||
| 801 | public function getDispatcher() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @param ConfigurationLoader $configurationLoader |
||
| 808 | */ |
||
| 809 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @param OutputInterface $output |
||
| 822 | */ |
||
| 823 | protected function _addOutputStyles(OutputInterface $output) |
||
| 828 | |||
| 829 | /** |
||
| 830 | * @return ObjectManager |
||
| 831 | */ |
||
| 832 | public function getObjectManager() |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @param InputInterface $input [optional] |
||
| 839 | * @param OutputInterface $output [optional] |
||
| 840 | * @return DetectionResult |
||
| 841 | */ |
||
| 842 | private function getDetectionResult(InputInterface $input = null, OutputInterface $output = null) |
||
| 866 | } |
||
| 867 |