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 |
||
| 28 | class Application extends BaseApplication |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | const APP_NAME = 'n98-magerun2'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const APP_VERSION = '1.1.6'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private static $logo = " |
||
| 54 | ____ ____ ___ |
||
| 55 | ____ / __ \\( __ ) ____ ___ ____ _____ ____ _______ ______ |__ \\ |
||
| 56 | / __ \\/ /_/ / __ |_____/ __ `__ \\/ __ `/ __ `/ _ \\/ ___/ / / / __ \\__/ / |
||
| 57 | / / / /\\__, / /_/ /_____/ / / / / / /_/ / /_/ / __/ / / /_/ / / / / __/ |
||
| 58 | /_/ /_//____/\\____/ /_/ /_/ /_/\\__,_/\\__, /\\___/_/ \\__,_/_/ /_/____/ |
||
| 59 | /____/ |
||
| 60 | "; |
||
| 61 | /** |
||
| 62 | * @var ClassLoader |
||
| 63 | */ |
||
| 64 | protected $autoloader; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Config |
||
| 68 | */ |
||
| 69 | protected $config; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $_magentoRootFolder = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $_magentoEnterprise = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var EntryPoint |
||
| 88 | */ |
||
| 89 | protected $_magento2EntryPoint = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected $_isPharMode = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | */ |
||
| 99 | protected $_magerunStopFileFound = false; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $_magerunStopFileFolder = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $_isInitialized = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var EventDispatcher |
||
| 113 | */ |
||
| 114 | protected $dispatcher; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * If root dir is set by root-dir option this flag is true |
||
| 118 | * |
||
| 119 | * @var bool |
||
| 120 | */ |
||
| 121 | protected $_directRootDir = false; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $_magentoDetected = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var ObjectManager |
||
| 130 | */ |
||
| 131 | protected $_objectManager = null; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param ClassLoader $autoloader |
||
|
|
|||
| 135 | */ |
||
| 136 | public function __construct($autoloader = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return InputDefinition |
||
| 144 | */ |
||
| 145 | protected function getDefaultInputDefinition() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Search for magento root folder |
||
| 198 | * |
||
| 199 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 200 | * @param OutputInterface $output [optional] |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 204 | { |
||
| 205 | // do not detect magento twice |
||
| 206 | if ($this->_magentoDetected) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($this->getMagentoRootFolder() === null) { |
||
| 211 | $this->_checkRootDirOption(); |
||
| 212 | $folder = OperatingSystem::getCwd(); |
||
| 213 | } else { |
||
| 214 | $folder = $this->getMagentoRootFolder(); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->getHelperSet()->set(new MagentoHelper($input, $output), 'magento'); |
||
| 218 | $magentoHelper = $this->getHelperSet()->get('magento'); |
||
| 219 | /* @var $magentoHelper MagentoHelper */ |
||
| 220 | if (!$this->_directRootDir) { |
||
| 221 | $subFolders = $this->config->getDetectSubFolders(); |
||
| 222 | } else { |
||
| 223 | $subFolders = array(); |
||
| 224 | } |
||
| 225 | |||
| 226 | $this->_magentoDetected = $magentoHelper->detect($folder, $subFolders); |
||
| 227 | $this->_magentoRootFolder = $magentoHelper->getRootFolder(); |
||
| 228 | $this->_magentoEnterprise = $magentoHelper->isEnterpriseEdition(); |
||
| 229 | $this->_magentoMajorVersion = $magentoHelper->getMajorVersion(); |
||
| 230 | $this->_magerunStopFileFound = $magentoHelper->isMagerunStopFileFound(); |
||
| 231 | $this->_magerunStopFileFolder = $magentoHelper->getMagerunStopFileFolder(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add own helpers to helperset. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected function registerHelpers() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Try to bootstrap magento 2 and load cli application |
||
| 262 | * |
||
| 263 | * @param OutputInterface $output |
||
| 264 | */ |
||
| 265 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Override standard command registration. We want alias support. |
||
| 289 | * |
||
| 290 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 291 | * @return \Symfony\Component\Console\Command\Command |
||
| 292 | */ |
||
| 293 | public function add(Command $command) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param bool $mode |
||
| 304 | */ |
||
| 305 | public function setPharMode($mode) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function isPharMode() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @TODO Move logic into "EventSubscriber" |
||
| 320 | * |
||
| 321 | * @param OutputInterface $output |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function checkVarDir(OutputInterface $output) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Loads and initializes the Magento application |
||
| 396 | * |
||
| 397 | * @return bool false if magento root folder is not set, true otherwise |
||
| 398 | */ |
||
| 399 | public function initMagento() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getHelp() |
||
| 421 | |||
| 422 | public function getLongVersion() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return boolean |
||
| 429 | */ |
||
| 430 | public function isMagentoEnterprise() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function getMagentoRootFolder() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $magentoRootFolder |
||
| 445 | */ |
||
| 446 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function getMagentoMajorVersion() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return ClassLoader |
||
| 461 | */ |
||
| 462 | public function getAutoloader() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param ClassLoader $autoloader |
||
| 469 | */ |
||
| 470 | public function setAutoloader(ClassLoader $autoloader) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | public function getConfig() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param array $config |
||
| 485 | */ |
||
| 486 | public function setConfig($config) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return boolean |
||
| 493 | */ |
||
| 494 | public function isMagerunStopFileFound() |
||
| 495 | { |
||
| 496 | return $this->_magerunStopFileFound; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Runs the current application with possible command aliases |
||
| 501 | * |
||
| 502 | * @param InputInterface $input An Input instance |
||
| 503 | * @param OutputInterface $output An Output instance |
||
| 504 | * |
||
| 505 | * @return integer 0 if everything went fine, or an error code |
||
| 506 | */ |
||
| 507 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 528 | * @param OutputInterface $output [optional] |
||
| 529 | * |
||
| 530 | * @return int |
||
| 531 | */ |
||
| 532 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param array $initConfig |
||
| 567 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 568 | * @param OutputInterface $output |
||
| 569 | * |
||
| 570 | * @return void |
||
| 571 | */ |
||
| 572 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 573 | { |
||
| 574 | if ($this->_isInitialized) { |
||
| 575 | return; |
||
| 576 | } |
||
| 577 | |||
| 578 | // Suppress DateTime warnings |
||
| 579 | date_default_timezone_set(@date_default_timezone_get()); |
||
| 580 | |||
| 581 | // Initialize EventDispatcher early |
||
| 582 | $this->dispatcher = new EventDispatcher(); |
||
| 583 | $this->setDispatcher($this->dispatcher); |
||
| 584 | |||
| 585 | if (null === $input) { |
||
| 586 | $input = new ArgvInput(); |
||
| 587 | } |
||
| 588 | |||
| 589 | if (null === $output) { |
||
| 590 | $output = new ConsoleOutput(); |
||
| 591 | } |
||
| 592 | |||
| 593 | if (null !== $this->config) { |
||
| 594 | throw new UnexpectedValueException(sprintf('Config already initialized')); |
||
| 595 | } |
||
| 596 | |||
| 597 | $loadExternalConfig = !$this->_checkSkipConfigOption(); |
||
| 598 | |||
| 599 | $this->config = $config = new Config($initConfig, $this->isPharMode(), $output); |
||
| 600 | $configLoader = $config->getLoader(); |
||
| 601 | $config->loadPartialConfig($loadExternalConfig); |
||
| 602 | $this->detectMagento($input, $output); |
||
| 603 | $configLoader->loadStageTwo($this->_magentoRootFolder, $loadExternalConfig, $this->_magerunStopFileFolder); |
||
| 604 | $config->load(); |
||
| 605 | |||
| 606 | if ($autoloader = $this->autoloader) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Include commands shipped by Magento 2 core |
||
| 610 | */ |
||
| 611 | if (!$this->_checkSkipMagento2CoreCommandsOption()) { |
||
| 612 | $this->registerMagentoCoreCommands($output); |
||
| 613 | } |
||
| 614 | |||
| 615 | $this->config->registerCustomAutoloaders($autoloader); |
||
| 616 | $this->registerEventSubscribers(); |
||
| 617 | $config->registerCustomCommands($this); |
||
| 618 | } |
||
| 619 | |||
| 620 | $this->registerHelpers(); |
||
| 621 | |||
| 622 | $this->_isInitialized = true; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param array $initConfig [optional] |
||
| 627 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 628 | * @param OutputInterface $output [optional] |
||
| 629 | */ |
||
| 630 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return void |
||
| 639 | */ |
||
| 640 | protected function registerEventSubscribers() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @return bool |
||
| 650 | */ |
||
| 651 | protected function _checkSkipConfigOption() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @return bool |
||
| 660 | */ |
||
| 661 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | protected function _checkRootDirOption() |
||
| 693 | |||
| 694 | protected function _initMagento1() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return void |
||
| 728 | */ |
||
| 729 | protected function _initMagento2() |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @return EventDispatcher |
||
| 749 | */ |
||
| 750 | public function getDispatcher() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param ConfigurationLoader $configurationLoader |
||
| 757 | */ |
||
| 758 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param OutputInterface $output |
||
| 765 | */ |
||
| 766 | protected function _addOutputStyles(OutputInterface $output) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * @return ObjectManager |
||
| 774 | */ |
||
| 775 | public function getObjectManager() |
||
| 779 | } |
||
| 780 |
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.