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 |
||
| 27 | class Application extends BaseApplication |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const APP_NAME = 'n98-magerun2'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | const APP_VERSION = '1.1.4'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 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 | /** |
||
| 61 | * @var ClassLoader |
||
| 62 | */ |
||
| 63 | protected $autoloader; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Config |
||
| 67 | */ |
||
| 68 | protected $config; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $partialConfig = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $_magentoRootFolder = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | protected $_magentoEnterprise = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var EntryPoint |
||
| 92 | */ |
||
| 93 | protected $_magento2EntryPoint = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $_isPharMode = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | protected $_magerunStopFileFound = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $_magerunStopFileFolder = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | protected $_isInitialized = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var EventDispatcher |
||
| 117 | */ |
||
| 118 | protected $dispatcher; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * If root dir is set by root-dir option this flag is true |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | protected $_directRootDir = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | protected $_magentoDetected = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var ObjectManager |
||
| 134 | */ |
||
| 135 | protected $_objectManager = null; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param ClassLoader $autoloader |
||
|
|
|||
| 139 | */ |
||
| 140 | public function __construct($autoloader = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return \Symfony\Component\Console\Input\InputDefinition|void |
||
| 148 | */ |
||
| 149 | protected function getDefaultInputDefinition() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get names of sub-folders to be scanned during Magento detection |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function getDetectSubFolders() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Search for magento root folder |
||
| 204 | * |
||
| 205 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 206 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add own helpers to helperset. |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | protected function registerHelpers() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Try to bootstrap magento 2 and load cli application |
||
| 276 | * |
||
| 277 | * @param OutputInterface $output |
||
| 278 | */ |
||
| 279 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Override standard command registration. We want alias support. |
||
| 303 | * |
||
| 304 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 305 | * @return \Symfony\Component\Console\Command\Command |
||
| 306 | */ |
||
| 307 | public function add(Command $command) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param bool $mode |
||
| 318 | */ |
||
| 319 | public function setPharMode($mode) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function isPharMode() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @TODO Move logic into "EventSubscriber" |
||
| 334 | * |
||
| 335 | * @param OutputInterface $output |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function checkVarDir(OutputInterface $output) |
||
| 391 | |||
| 392 | public function initMagento() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function getHelp() |
||
| 414 | |||
| 415 | public function getLongVersion() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return boolean |
||
| 422 | */ |
||
| 423 | public function isMagentoEnterprise() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getMagentoRootFolder() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $magentoRootFolder |
||
| 438 | */ |
||
| 439 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return int |
||
| 446 | */ |
||
| 447 | public function getMagentoMajorVersion() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return ClassLoader |
||
| 454 | */ |
||
| 455 | public function getAutoloader() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param ClassLoader $autoloader |
||
| 462 | */ |
||
| 463 | public function setAutoloader(ClassLoader $autoloader) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | public function getConfig() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param array $config |
||
| 478 | */ |
||
| 479 | public function setConfig($config) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function isMagerunStopFileFound() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Runs the current application with possible command aliases |
||
| 494 | * |
||
| 495 | * @param InputInterface $input An Input instance |
||
| 496 | * @param OutputInterface $output An Output instance |
||
| 497 | * |
||
| 498 | * @return integer 0 if everything went fine, or an error code |
||
| 499 | */ |
||
| 500 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 525 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 526 | * @return int |
||
| 527 | */ |
||
| 528 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param array $initConfig |
||
| 563 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 564 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 565 | * |
||
| 566 | * @return void |
||
| 567 | */ |
||
| 568 | public function init($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param array $initConfig |
||
| 616 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 617 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 618 | */ |
||
| 619 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @return void |
||
| 627 | */ |
||
| 628 | protected function registerEventSubscribers() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | protected function _checkSkipConfigOption() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @return bool |
||
| 648 | */ |
||
| 649 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | protected function _checkRootDirOption() |
||
| 681 | |||
| 682 | protected function _initMagento1() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @return void |
||
| 716 | */ |
||
| 717 | protected function _initMagento2() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return EventDispatcher |
||
| 737 | */ |
||
| 738 | public function getDispatcher() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @param ConfigurationLoader $configurationLoader |
||
| 745 | */ |
||
| 746 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param OutputInterface $output |
||
| 753 | */ |
||
| 754 | protected function _addOutputStyles(OutputInterface $output) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @return ObjectManager |
||
| 762 | */ |
||
| 763 | public function getObjectManager() |
||
| 767 | } |
||
| 768 |
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.