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.14'; |
||
| 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) |
||
| 137 | { |
||
| 138 | $this->autoloader = $autoloader; |
||
| 139 | parent::__construct(self::APP_NAME, self::APP_VERSION); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return InputDefinition |
||
| 144 | */ |
||
| 145 | protected function getDefaultInputDefinition() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Search for magento root folder |
||
| 198 | * |
||
| 199 | * @param InputInterface|null $input [optional] |
||
| 200 | * @param OutputInterface|null $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) |
||
| 266 | { |
||
| 267 | if ($this->getMagentoRootFolder()) { |
||
| 268 | // Magento was found -> register core cli commands |
||
| 269 | require_once $this->getMagentoRootFolder() . '/app/bootstrap.php'; |
||
| 270 | |||
| 271 | $coreCliApplication = new \Magento\Framework\Console\Cli(); |
||
| 272 | $coreCliApplicationCommands = $coreCliApplication->all(); |
||
| 273 | |||
| 274 | foreach ($coreCliApplicationCommands as $coreCliApplicationCommand) { |
||
| 275 | if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { |
||
| 276 | $output->writeln( |
||
| 277 | sprintf( |
||
| 278 | '<debug>Add core command </debug> <info>%s</info> -> <comment>%s</comment>', |
||
| 279 | $coreCliApplicationCommand->getName(), |
||
| 280 | get_class($coreCliApplicationCommand) |
||
| 281 | ) |
||
| 282 | ); |
||
| 283 | } |
||
| 284 | $this->add($coreCliApplicationCommand); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Override standard command registration. We want alias support. |
||
| 291 | * |
||
| 292 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 293 | * @return \Symfony\Component\Console\Command\Command |
||
| 294 | */ |
||
| 295 | public function add(Command $command) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param bool $mode |
||
| 306 | */ |
||
| 307 | public function setPharMode($mode) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function isPharMode() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @TODO Move logic into "EventSubscriber" |
||
| 322 | * |
||
| 323 | * @param OutputInterface $output |
||
| 324 | * @return null|false |
||
| 325 | */ |
||
| 326 | public function checkVarDir(OutputInterface $output) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Loads and initializes the Magento application |
||
| 398 | * |
||
| 399 | * @return bool false if magento root folder is not set, true otherwise |
||
| 400 | */ |
||
| 401 | public function initMagento() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function getHelp() |
||
| 423 | |||
| 424 | public function getLongVersion() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return boolean |
||
| 431 | */ |
||
| 432 | public function isMagentoEnterprise() |
||
| 433 | { |
||
| 434 | return $this->_magentoEnterprise; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getMagentoRootFolder() |
||
| 441 | { |
||
| 442 | return $this->_magentoRootFolder; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $magentoRootFolder |
||
| 447 | */ |
||
| 448 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 449 | { |
||
| 450 | $this->_magentoRootFolder = $magentoRootFolder; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return int |
||
| 455 | */ |
||
| 456 | public function getMagentoMajorVersion() |
||
| 457 | { |
||
| 458 | return $this->_magentoMajorVersion; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return ClassLoader |
||
| 463 | */ |
||
| 464 | public function getAutoloader() |
||
| 465 | { |
||
| 466 | return $this->autoloader; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param ClassLoader $autoloader |
||
| 471 | */ |
||
| 472 | public function setAutoloader(ClassLoader $autoloader) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | public function getConfig() |
||
| 481 | { |
||
| 482 | return $this->config->getConfig(); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param array $config |
||
| 487 | */ |
||
| 488 | public function setConfig($config) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return boolean |
||
| 495 | */ |
||
| 496 | public function isMagerunStopFileFound() |
||
| 497 | { |
||
| 498 | return $this->_magerunStopFileFound; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Runs the current application with possible command aliases |
||
| 503 | * |
||
| 504 | * @param InputInterface $input An Input instance |
||
| 505 | * @param OutputInterface $output An Output instance |
||
| 506 | * |
||
| 507 | * @return integer 0 if everything went fine, or an error code |
||
| 508 | */ |
||
| 509 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 530 | * @param OutputInterface $output [optional] |
||
|
1 ignored issue
–
show
|
|||
| 531 | * |
||
| 532 | * @return int |
||
| 533 | */ |
||
| 534 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param array $initConfig |
||
| 569 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 570 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 571 | * |
||
| 572 | * @return void |
||
| 573 | */ |
||
| 574 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 575 | { |
||
| 576 | if ($this->_isInitialized) { |
||
| 577 | return; |
||
| 578 | } |
||
| 579 | |||
| 580 | // Suppress DateTime warnings |
||
| 581 | date_default_timezone_set(@date_default_timezone_get()); |
||
| 582 | |||
| 583 | // Initialize EventDispatcher early |
||
| 584 | $this->dispatcher = new EventDispatcher(); |
||
| 585 | $this->setDispatcher($this->dispatcher); |
||
| 586 | |||
| 587 | if (null === $input) { |
||
| 588 | $input = new ArgvInput(); |
||
| 589 | } |
||
| 590 | |||
| 591 | if (null === $output) { |
||
| 592 | $output = new ConsoleOutput(); |
||
| 593 | } |
||
| 594 | |||
| 595 | if (null !== $this->config) { |
||
| 596 | throw new UnexpectedValueException(sprintf('Config already initialized')); |
||
| 597 | } |
||
| 598 | |||
| 599 | $loadExternalConfig = !$this->_checkSkipConfigOption(); |
||
| 600 | |||
| 601 | $this->config = $config = new Config($initConfig, $this->isPharMode(), $output); |
||
| 602 | $configLoader = $config->getLoader(); |
||
| 603 | $config->loadPartialConfig($loadExternalConfig); |
||
| 604 | $this->detectMagento($input, $output); |
||
| 605 | $configLoader->loadStageTwo($this->_magentoRootFolder, $loadExternalConfig, $this->_magerunStopFileFolder); |
||
| 606 | $config->load(); |
||
| 607 | |||
| 608 | if ($autoloader = $this->autoloader) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Include commands shipped by Magento 2 core |
||
| 612 | */ |
||
| 613 | if (!$this->_checkSkipMagento2CoreCommandsOption()) { |
||
| 614 | $this->registerMagentoCoreCommands($output); |
||
| 615 | } |
||
| 616 | |||
| 617 | $this->config->registerCustomAutoloaders($autoloader); |
||
| 618 | $this->registerEventSubscribers(); |
||
| 619 | $config->registerCustomCommands($this); |
||
| 620 | } |
||
| 621 | |||
| 622 | $this->registerHelpers(); |
||
| 623 | |||
| 624 | $this->_isInitialized = true; |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param array $initConfig [optional] |
||
| 629 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 630 | * @param OutputInterface $output [optional] |
||
|
1 ignored issue
–
show
|
|||
| 631 | */ |
||
| 632 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | protected function registerEventSubscribers() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | protected function _checkSkipConfigOption() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | protected function _checkRootDirOption() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Show a hint that this is Magento 1 and how to obtain magerun for it |
||
| 698 | */ |
||
| 699 | protected function _initMagento1() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @return void |
||
| 736 | */ |
||
| 737 | protected function _initMagento2() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @return EventDispatcher |
||
| 757 | */ |
||
| 758 | public function getDispatcher() |
||
| 759 | { |
||
| 760 | return $this->dispatcher; |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param ConfigurationLoader $configurationLoader |
||
| 765 | */ |
||
| 766 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * @param OutputInterface $output |
||
| 773 | */ |
||
| 774 | protected function _addOutputStyles(OutputInterface $output) |
||
| 775 | { |
||
| 776 | $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('magenta', 'white')); |
||
| 777 | $output->getFormatter()->setStyle('warning', new OutputFormatterStyle('red', 'yellow', array('bold'))); |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @return ObjectManager |
||
| 782 | */ |
||
| 783 | public function getObjectManager() |
||
| 787 | } |
||
| 788 |
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.