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.29'; |
||
| 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 | /** |
||
| 65 | * Shadow copy of the Application parent when using this concrete setAutoExit() implementation |
||
| 66 | * |
||
| 67 | * @see \Symfony\Component\Console\Application::$autoExit |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $autoExitShadow = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ClassLoader |
||
| 74 | */ |
||
| 75 | protected $autoloader; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Config |
||
| 79 | */ |
||
| 80 | protected $config; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 84 | * @var ConfigurationLoader |
||
| 85 | */ |
||
| 86 | private $configurationLoaderInjected; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $_magentoRootFolder = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | protected $_magentoEnterprise = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var EntryPoint |
||
| 105 | */ |
||
| 106 | protected $_magento2EntryPoint = null; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $_isPharMode = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $_magerunStopFileFound = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $_magerunStopFileFolder = null; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $_isInitialized = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var EventDispatcher |
||
| 130 | */ |
||
| 131 | protected $dispatcher; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * If root dir is set by root-dir option this flag is true |
||
| 135 | * |
||
| 136 | * @var bool |
||
| 137 | */ |
||
| 138 | protected $_directRootDir = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var bool |
||
| 142 | */ |
||
| 143 | protected $_magentoDetected = false; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param ClassLoader $autoloader |
||
|
|
|||
| 147 | */ |
||
| 148 | public function __construct($autoloader = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param bool $boolean |
||
| 156 | * @return bool previous auto-exit state |
||
| 157 | */ |
||
| 158 | public function setAutoExit($boolean) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return InputDefinition |
||
| 169 | */ |
||
| 170 | protected function getDefaultInputDefinition() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Search for magento root folder |
||
| 212 | * |
||
| 213 | * @param InputInterface $input [optional] |
||
| 214 | * @param OutputInterface $output [optional] |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Add own helpers to helperset. |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | protected function registerHelpers() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param InputInterface $input |
||
| 284 | * |
||
| 285 | * @return ArgvInput|InputInterface |
||
| 286 | */ |
||
| 287 | protected function checkConfigCommandAlias(InputInterface $input) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param Command $command |
||
| 296 | */ |
||
| 297 | protected function registerConfigCommandAlias(Command $command) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Adds autoloader prefixes from user's config |
||
| 306 | */ |
||
| 307 | protected function registerCustomAutoloaders() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | protected function hasCustomCommands() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return void |
||
| 328 | */ |
||
| 329 | protected function registerCustomCommands() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $class |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | protected function isCommandDisabled($class) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Override standard command registration. We want alias support. |
||
| 351 | * |
||
| 352 | * @param Command $command |
||
| 353 | * |
||
| 354 | * @return Command |
||
| 355 | */ |
||
| 356 | public function add(Command $command) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param bool $mode |
||
| 367 | */ |
||
| 368 | public function setPharMode($mode) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function isPharMode() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @TODO Move logic into "EventSubscriber" |
||
| 383 | * |
||
| 384 | * @param OutputInterface $output |
||
| 385 | * @return null|false |
||
| 386 | */ |
||
| 387 | public function checkVarDir(OutputInterface $output) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Loads and initializes the Magento application |
||
| 447 | * |
||
| 448 | * @param bool $soft |
||
| 449 | * |
||
| 450 | * @return bool false if magento root folder is not set, true otherwise |
||
| 451 | */ |
||
| 452 | public function initMagento($soft = false) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getHelp() |
||
| 475 | |||
| 476 | public function getLongVersion() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return boolean |
||
| 483 | */ |
||
| 484 | public function isMagentoEnterprise() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function getMagentoRootFolder() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param string $magentoRootFolder |
||
| 499 | */ |
||
| 500 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return int |
||
| 507 | */ |
||
| 508 | public function getMagentoMajorVersion() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return ClassLoader |
||
| 515 | */ |
||
| 516 | public function getAutoloader() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param ClassLoader $autoloader |
||
| 523 | */ |
||
| 524 | public function setAutoloader(ClassLoader $autoloader) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get config array |
||
| 531 | * |
||
| 532 | * Specify one key per parameter to traverse the config. Then returns null |
||
| 533 | * if the path of the key(s) can not be obtained. |
||
| 534 | * |
||
| 535 | * @param string|int $key ... (optional) |
||
| 536 | * |
||
| 537 | * @return array|null |
||
| 538 | */ |
||
| 539 | public function getConfig($key = null) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param array $config |
||
| 559 | */ |
||
| 560 | public function setConfig($config) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @return boolean |
||
| 567 | */ |
||
| 568 | public function isMagerunStopFileFound() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Runs the current application with possible command aliases |
||
| 575 | * |
||
| 576 | * @param InputInterface $input An Input instance |
||
| 577 | * @param OutputInterface $output An Output instance |
||
| 578 | * |
||
| 579 | * @return integer 0 if everything went fine, or an error code |
||
| 580 | */ |
||
| 581 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param InputInterface $input [optional] |
||
| 602 | * @param OutputInterface $output [optional] |
||
| 603 | * |
||
| 604 | * @return int |
||
| 605 | */ |
||
| 606 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @param array $initConfig [optional] |
||
| 641 | * @param InputInterface $input [optional] |
||
| 642 | * @param OutputInterface $output [optional] |
||
| 643 | * |
||
| 644 | * @return void |
||
| 645 | */ |
||
| 646 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @param array $initConfig [optional] |
||
| 691 | * @param InputInterface $input [optional] |
||
| 692 | * @param OutputInterface $output [optional] |
||
| 693 | */ |
||
| 694 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return void |
||
| 705 | */ |
||
| 706 | protected function registerEventSubscribers() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @param InputInterface $input |
||
| 718 | * @return bool |
||
| 719 | * @deprecated 1.97.27 |
||
| 720 | */ |
||
| 721 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @param InputInterface $input |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | protected function _checkRootDirOption(InputInterface $input) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Set root dir (chdir()) of magento directory |
||
| 745 | * |
||
| 746 | * @param string $path to Magento directory |
||
| 747 | */ |
||
| 748 | private function setRootDir($path) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 763 | * and $this unbound. |
||
| 764 | * |
||
| 765 | * @param string $path |
||
| 766 | */ |
||
| 767 | private function requireOnce($path) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @param bool $soft |
||
| 781 | * |
||
| 782 | * @return void |
||
| 783 | */ |
||
| 784 | protected function _initMagento1($soft = false) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @return void |
||
| 808 | */ |
||
| 809 | protected function _initMagento2() |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 816 | * |
||
| 817 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 818 | */ |
||
| 819 | private function outputMagerunCompatibilityNotice($version) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @return EventDispatcher |
||
| 857 | */ |
||
| 858 | public function getDispatcher() |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @param array $initConfig |
||
| 865 | * @param OutputInterface $output |
||
| 866 | * @return ConfigurationLoader |
||
| 867 | */ |
||
| 868 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param ConfigurationLoader $configurationLoader |
||
| 885 | * |
||
| 886 | * @return $this |
||
| 887 | */ |
||
| 888 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @param OutputInterface $output |
||
| 903 | */ |
||
| 904 | protected function _addOutputStyles(OutputInterface $output) |
||
| 909 | } |
||
| 910 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.