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.23'; |
||
| 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 | * @param ClassLoader $autoloader |
||
| 138 | */ |
||
| 139 | public function __construct($autoloader = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return InputDefinition |
||
| 147 | */ |
||
| 148 | protected function getDefaultInputDefinition() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Search for magento root folder |
||
| 190 | * |
||
| 191 | * @param InputInterface $input [optional] |
||
| 192 | * @param OutputInterface $output [optional] |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add own helpers to helperset. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected function registerHelpers() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param InputInterface $input |
||
| 262 | * |
||
| 263 | * @return ArgvInput|InputInterface |
||
| 264 | */ |
||
| 265 | protected function checkConfigCommandAlias(InputInterface $input) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param Command $command |
||
| 274 | */ |
||
| 275 | protected function registerConfigCommandAlias(Command $command) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Adds autoloader prefixes from user's config |
||
| 284 | */ |
||
| 285 | protected function registerCustomAutoloaders() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | protected function hasCustomCommands() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | protected function registerCustomCommands() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $class |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | protected function isCommandDisabled($class) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Override standard command registration. We want alias support. |
||
| 327 | * |
||
| 328 | * @param Command $command |
||
| 329 | * |
||
| 330 | * @return Command |
||
| 331 | */ |
||
| 332 | public function add(Command $command) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param bool $mode |
||
| 343 | */ |
||
| 344 | public function setPharMode($mode) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function isPharMode() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @TODO Move logic into "EventSubscriber" |
||
| 359 | * |
||
| 360 | * @param OutputInterface $output |
||
| 361 | * @return null|false |
||
| 362 | */ |
||
| 363 | public function checkVarDir(OutputInterface $output) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Loads and initializes the Magento application |
||
| 423 | * |
||
| 424 | * @param bool $soft |
||
| 425 | * |
||
| 426 | * @return bool false if magento root folder is not set, true otherwise |
||
| 427 | */ |
||
| 428 | public function initMagento($soft = false) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getHelp() |
||
| 451 | |||
| 452 | public function getLongVersion() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return boolean |
||
| 459 | */ |
||
| 460 | public function isMagentoEnterprise() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function getMagentoRootFolder() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param string $magentoRootFolder |
||
| 475 | */ |
||
| 476 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return int |
||
| 483 | */ |
||
| 484 | public function getMagentoMajorVersion() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return ClassLoader |
||
| 491 | */ |
||
| 492 | public function getAutoloader() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param ClassLoader $autoloader |
||
| 499 | */ |
||
| 500 | public function setAutoloader(ClassLoader $autoloader) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return array |
||
| 507 | */ |
||
| 508 | public function getConfig() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param array $config |
||
| 515 | */ |
||
| 516 | public function setConfig($config) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return boolean |
||
| 523 | */ |
||
| 524 | public function isMagerunStopFileFound() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Runs the current application with possible command aliases |
||
| 531 | * |
||
| 532 | * @param InputInterface $input An Input instance |
||
| 533 | * @param OutputInterface $output An Output instance |
||
| 534 | * |
||
| 535 | * @return integer 0 if everything went fine, or an error code |
||
| 536 | */ |
||
| 537 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param InputInterface $input [optional] |
||
| 558 | * @param OutputInterface $output [optional] |
||
| 559 | * |
||
| 560 | * @return int |
||
| 561 | */ |
||
| 562 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param array $initConfig [optional] |
||
| 597 | * @param InputInterface $input [optional] |
||
| 598 | * @param OutputInterface $output [optional] |
||
| 599 | * |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param array $initConfig [optional] |
||
| 652 | * @param InputInterface $input [optional] |
||
| 653 | * @param OutputInterface $output [optional] |
||
| 654 | */ |
||
| 655 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @return void |
||
| 666 | */ |
||
| 667 | protected function registerEventSubscribers() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param InputInterface $input |
||
| 679 | * @return bool |
||
| 680 | */ |
||
| 681 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @param InputInterface $input |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | protected function _checkRootDirOption(InputInterface $input) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Set root dir (chdir()) of magento directory |
||
| 700 | * |
||
| 701 | * @param string $path to Magento directory |
||
| 702 | */ |
||
| 703 | private function setRootDir($path) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 718 | * and $this unbound. |
||
| 719 | * |
||
| 720 | * @param string $path |
||
| 721 | */ |
||
| 722 | private function requireOnce($path) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param bool $soft |
||
| 736 | * |
||
| 737 | * @return void |
||
| 738 | */ |
||
| 739 | protected function _initMagento1($soft = false) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @return void |
||
| 763 | */ |
||
| 764 | protected function _initMagento2() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 771 | * |
||
| 772 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 773 | */ |
||
| 774 | private function outputMagerunCompatibilityNotice($version) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * @return EventDispatcher |
||
| 812 | */ |
||
| 813 | public function getDispatcher() |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @param array $initConfig |
||
| 820 | * @param OutputInterface $output |
||
| 821 | * @return ConfigurationLoader |
||
| 822 | */ |
||
| 823 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @param ConfigurationLoader $configurationLoader |
||
| 836 | * |
||
| 837 | * @return $this |
||
| 838 | */ |
||
| 839 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * @param OutputInterface $output |
||
| 854 | */ |
||
| 855 | protected function _addOutputStyles(OutputInterface $output) |
||
| 860 | } |
||
| 861 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.