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-magerun2'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | const APP_VERSION = '1.1.17'; |
||
| 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 | * @var ClassLoader |
||
| 64 | */ |
||
| 65 | protected $autoloader; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Config |
||
| 69 | */ |
||
| 70 | protected $config; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $_magentoRootFolder = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected $_magentoEnterprise = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var EntryPoint |
||
| 89 | */ |
||
| 90 | protected $_magento2EntryPoint = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $_isPharMode = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $_magerunStopFileFound = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $_magerunStopFileFolder = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $_isInitialized = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var EventDispatcher |
||
| 114 | */ |
||
| 115 | protected $dispatcher; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * If root dir is set by root-dir option this flag is true |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $_directRootDir = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var bool |
||
| 126 | */ |
||
| 127 | protected $_magentoDetected = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var ObjectManager |
||
| 131 | */ |
||
| 132 | protected $_objectManager = null; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param ClassLoader $autoloader |
||
|
|
|||
| 136 | */ |
||
| 137 | public function __construct($autoloader = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return InputDefinition |
||
| 145 | */ |
||
| 146 | protected function getDefaultInputDefinition() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Search for magento root folder |
||
| 199 | * |
||
| 200 | * @param InputInterface|null $input [optional] |
||
| 201 | * @param OutputInterface|null $output [optional] |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add own helpers to helperset. |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | protected function registerHelpers() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Try to bootstrap magento 2 and load cli application |
||
| 263 | * |
||
| 264 | * @param OutputInterface $output |
||
| 265 | */ |
||
| 266 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Override standard command registration. We want alias support. |
||
| 292 | * |
||
| 293 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 294 | * @return \Symfony\Component\Console\Command\Command |
||
| 295 | */ |
||
| 296 | public function add(Command $command) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param bool $mode |
||
| 307 | */ |
||
| 308 | public function setPharMode($mode) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function isPharMode() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @TODO Move logic into "EventSubscriber" |
||
| 323 | * |
||
| 324 | * @param OutputInterface $output |
||
| 325 | * @return null|false |
||
| 326 | */ |
||
| 327 | public function checkVarDir(OutputInterface $output) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Loads and initializes the Magento application |
||
| 399 | * |
||
| 400 | * @return bool false if magento root folder is not set, true otherwise |
||
| 401 | */ |
||
| 402 | public function initMagento() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getHelp() |
||
| 424 | |||
| 425 | public function getLongVersion() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return boolean |
||
| 432 | */ |
||
| 433 | public function isMagentoEnterprise() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getMagentoRootFolder() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param string $magentoRootFolder |
||
| 448 | */ |
||
| 449 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @return int |
||
| 456 | */ |
||
| 457 | public function getMagentoMajorVersion() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return ClassLoader |
||
| 464 | */ |
||
| 465 | public function getAutoloader() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param ClassLoader $autoloader |
||
| 472 | */ |
||
| 473 | public function setAutoloader(ClassLoader $autoloader) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | public function getConfig() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param array $config |
||
| 488 | */ |
||
| 489 | public function setConfig($config) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @return boolean |
||
| 496 | */ |
||
| 497 | 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] |
||
|
1 ignored issue
–
show
|
|||
| 531 | * @param OutputInterface $output [optional] |
||
| 532 | * |
||
| 533 | * @return int |
||
| 534 | */ |
||
| 535 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param array $initConfig |
||
| 570 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 571 | * @param OutputInterface $output |
||
| 572 | * |
||
| 573 | * @return void |
||
| 574 | */ |
||
| 575 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param array $initConfig [optional] |
||
| 630 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 631 | * @param OutputInterface $output [optional] |
||
| 632 | */ |
||
| 633 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @return void |
||
| 642 | */ |
||
| 643 | protected function registerEventSubscribers() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @return bool |
||
| 654 | */ |
||
| 655 | protected function _checkSkipConfigOption() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @return bool |
||
| 664 | */ |
||
| 665 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | protected function _checkRootDirOption() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $path |
||
| 696 | */ |
||
| 697 | private function setRootDir($path) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Show a hint that this is Magento 1 and how to obtain magerun for it |
||
| 712 | */ |
||
| 713 | protected function _initMagento1() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @return void |
||
| 750 | */ |
||
| 751 | protected function _initMagento2() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @return EventDispatcher |
||
| 771 | */ |
||
| 772 | public function getDispatcher() |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @param ConfigurationLoader $configurationLoader |
||
| 779 | */ |
||
| 780 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @param OutputInterface $output |
||
| 787 | */ |
||
| 788 | protected function _addOutputStyles(OutputInterface $output) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @return ObjectManager |
||
| 796 | */ |
||
| 797 | public function getObjectManager() |
||
| 801 | } |
||
| 802 |
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.