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 |
||
| 30 | class Application extends BaseApplication |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | const APP_NAME = 'n98-magerun2'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | const APP_VERSION = '1.1.17'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private static $logo = " |
||
| 56 | ___ ___ ___ |
||
| 57 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |_ ) |
||
| 58 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ / / |
||
| 59 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_/___| |
||
| 60 | |___/ |
||
| 61 | "; |
||
| 62 | /** |
||
| 63 | * @var ClassLoader |
||
| 64 | */ |
||
| 65 | protected $autoloader; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Config |
||
| 69 | */ |
||
| 70 | protected $config; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 74 | * @var ConfigurationLoader |
||
| 75 | */ |
||
| 76 | private $configurationLoaderInjected; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $_magentoRootFolder = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $_magentoEnterprise = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var EntryPoint |
||
| 95 | */ |
||
| 96 | protected $_magento2EntryPoint = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | protected $_isPharMode = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $_magerunStopFileFound = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $_magerunStopFileFolder = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $_isInitialized = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var EventDispatcher |
||
| 120 | */ |
||
| 121 | protected $dispatcher; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * If root dir is set by root-dir option this flag is true |
||
| 125 | * |
||
| 126 | * @var bool |
||
| 127 | */ |
||
| 128 | protected $_directRootDir = false; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | protected $_magentoDetected = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var ObjectManager |
||
| 137 | */ |
||
| 138 | protected $_objectManager = null; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param ClassLoader $autoloader |
||
|
|
|||
| 142 | */ |
||
| 143 | public function __construct($autoloader = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return InputDefinition |
||
| 151 | */ |
||
| 152 | protected function getDefaultInputDefinition() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Search for magento root folder |
||
| 205 | * |
||
| 206 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 207 | * @param OutputInterface $output [optional] |
||
|
1 ignored issue
–
show
|
|||
| 208 | * @return void |
||
| 209 | */ |
||
| 210 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Add own helpers to helperset. |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | protected function registerHelpers() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Try to bootstrap magento 2 and load cli application |
||
| 277 | * |
||
| 278 | * @param OutputInterface $output |
||
| 279 | */ |
||
| 280 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Override standard command registration. We want alias support. |
||
| 308 | * |
||
| 309 | * @param Command $command |
||
| 310 | * |
||
| 311 | * @return Command |
||
| 312 | */ |
||
| 313 | public function add(Command $command) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param bool $mode |
||
| 324 | */ |
||
| 325 | public function setPharMode($mode) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function isPharMode() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @TODO Move logic into "EventSubscriber" |
||
| 340 | * |
||
| 341 | * @param OutputInterface $output |
||
| 342 | * @return null|false |
||
| 343 | */ |
||
| 344 | public function checkVarDir(OutputInterface $output) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Loads and initializes the Magento application |
||
| 404 | * |
||
| 405 | * @param bool $soft |
||
| 406 | * |
||
| 407 | * @return bool false if magento root folder is not set, true otherwise |
||
| 408 | */ |
||
| 409 | public function initMagento($soft = false) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getHelp() |
||
| 432 | |||
| 433 | public function getLongVersion() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return boolean |
||
| 440 | */ |
||
| 441 | public function isMagentoEnterprise() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getMagentoRootFolder() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $magentoRootFolder |
||
| 456 | */ |
||
| 457 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return int |
||
| 464 | */ |
||
| 465 | public function getMagentoMajorVersion() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return ClassLoader |
||
| 472 | */ |
||
| 473 | public function getAutoloader() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param ClassLoader $autoloader |
||
| 480 | */ |
||
| 481 | public function setAutoloader(ClassLoader $autoloader) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | public function getConfig() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param array $config |
||
| 496 | */ |
||
| 497 | public function setConfig($config) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @return boolean |
||
| 504 | */ |
||
| 505 | public function isMagerunStopFileFound() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Runs the current application with possible command aliases |
||
| 512 | * |
||
| 513 | * @param InputInterface $input An Input instance |
||
| 514 | * @param OutputInterface $output An Output instance |
||
| 515 | * |
||
| 516 | * @return integer 0 if everything went fine, or an error code |
||
| 517 | */ |
||
| 518 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 539 | * @param OutputInterface $output [optional] |
||
| 540 | * |
||
| 541 | * @return int |
||
| 542 | */ |
||
| 543 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param array $initConfig [optional] |
||
| 578 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 579 | * @param OutputInterface $output [optional] |
||
| 580 | * |
||
| 581 | * @return void |
||
| 582 | */ |
||
| 583 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param array $initConfig [optional] |
||
| 639 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 640 | * @param OutputInterface $output [optional] |
||
| 641 | */ |
||
| 642 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @return void |
||
| 653 | */ |
||
| 654 | protected function registerEventSubscribers() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param InputInterface $input |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @return bool |
||
| 675 | */ |
||
| 676 | protected function _checkSkipMagento2CoreCommandsOption(InputInterface $input) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param InputInterface $input |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | protected function _checkRootDirOption(InputInterface $input) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Set root dir (chdir()) of magento directory |
||
| 695 | * |
||
| 696 | * @param string $path to Magento directory |
||
| 697 | */ |
||
| 698 | private function setRootDir($path) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 713 | * and $this unbound. |
||
| 714 | * |
||
| 715 | * @param string $path |
||
| 716 | */ |
||
| 717 | private function requireOnce($path) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * @param bool $soft |
||
| 731 | * |
||
| 732 | * @return void |
||
| 733 | */ |
||
| 734 | protected function _initMagento1($soft = false) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @return void |
||
| 741 | */ |
||
| 742 | protected function _initMagento2() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 762 | * |
||
| 763 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 764 | */ |
||
| 765 | private function outputMagerunCompatibilityNotice($version) |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @return EventDispatcher |
||
| 803 | */ |
||
| 804 | public function getDispatcher() |
||
| 808 | |||
| 809 | /** |
||
| 810 | * @param ConfigurationLoader $configurationLoader |
||
| 811 | */ |
||
| 812 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @param OutputInterface $output |
||
| 825 | */ |
||
| 826 | protected function _addOutputStyles(OutputInterface $output) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @return ObjectManager |
||
| 834 | */ |
||
| 835 | public function getObjectManager() |
||
| 839 | } |
||
| 840 |
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.