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 string |
||
| 72 | */ |
||
| 73 | protected $_magentoRootFolder = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | protected $_magentoEnterprise = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var EntryPoint |
||
| 87 | */ |
||
| 88 | protected $_magento2EntryPoint = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var bool |
||
| 92 | */ |
||
| 93 | protected $_isPharMode = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $_magerunStopFileFound = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $_magerunStopFileFolder = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | protected $_isInitialized = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var EventDispatcher |
||
| 112 | */ |
||
| 113 | protected $dispatcher; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * If root dir is set by root-dir option this flag is true |
||
| 117 | * |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $_directRootDir = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | protected $_magentoDetected = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var ObjectManager |
||
| 129 | */ |
||
| 130 | protected $_objectManager = null; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param ClassLoader $autoloader |
||
|
|
|||
| 134 | */ |
||
| 135 | public function __construct($autoloader = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return \Symfony\Component\Console\Input\InputDefinition|void |
||
| 143 | */ |
||
| 144 | protected function getDefaultInputDefinition() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Search for magento root folder |
||
| 184 | * |
||
| 185 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 186 | * @param OutputInterface $output [optional] |
||
| 187 | */ |
||
| 188 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Add own helpers to helperset. |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | protected function registerHelpers() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Try to bootstrap magento 2 and load cli application |
||
| 255 | * |
||
| 256 | * @param OutputInterface $output |
||
| 257 | */ |
||
| 258 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Override standard command registration. We want alias support. |
||
| 282 | * |
||
| 283 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 284 | * @return \Symfony\Component\Console\Command\Command |
||
| 285 | */ |
||
| 286 | public function add(Command $command) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param bool $mode |
||
| 297 | */ |
||
| 298 | public function setPharMode($mode) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function isPharMode() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @TODO Move logic into "EventSubscriber" |
||
| 313 | * |
||
| 314 | * @param OutputInterface $output |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function checkVarDir(OutputInterface $output) |
||
| 383 | |||
| 384 | public function initMagento() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getHelp() |
||
| 406 | |||
| 407 | public function getLongVersion() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return boolean |
||
| 414 | */ |
||
| 415 | public function isMagentoEnterprise() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getMagentoRootFolder() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $magentoRootFolder |
||
| 430 | */ |
||
| 431 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return int |
||
| 438 | */ |
||
| 439 | public function getMagentoMajorVersion() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return ClassLoader |
||
| 446 | */ |
||
| 447 | public function getAutoloader() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param ClassLoader $autoloader |
||
| 454 | */ |
||
| 455 | public function setAutoloader(ClassLoader $autoloader) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getConfig() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param array $config |
||
| 470 | */ |
||
| 471 | public function setConfig($config) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return boolean |
||
| 478 | */ |
||
| 479 | public function isMagerunStopFileFound() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Runs the current application with possible command aliases |
||
| 486 | * |
||
| 487 | * @param InputInterface $input An Input instance |
||
| 488 | * @param OutputInterface $output An Output instance |
||
| 489 | * |
||
| 490 | * @return integer 0 if everything went fine, or an error code |
||
| 491 | */ |
||
| 492 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 517 | * @param OutputInterface $output [optional] |
||
| 518 | * |
||
| 519 | * @return int |
||
| 520 | */ |
||
| 521 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param array $initConfig |
||
| 556 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 557 | * @param OutputInterface $output |
||
| 558 | * |
||
| 559 | * @return void |
||
| 560 | */ |
||
| 561 | public function init($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param array $initConfig [optional] |
||
| 609 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 610 | * @param OutputInterface $output [optional] |
||
| 611 | */ |
||
| 612 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return void |
||
| 620 | */ |
||
| 621 | protected function registerEventSubscribers() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | protected function _checkSkipConfigOption() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return bool |
||
| 641 | */ |
||
| 642 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | protected function _checkRootDirOption() |
||
| 674 | |||
| 675 | protected function _initMagento1() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return void |
||
| 709 | */ |
||
| 710 | protected function _initMagento2() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * @return EventDispatcher |
||
| 730 | */ |
||
| 731 | public function getDispatcher() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @param ConfigurationLoader $configurationLoader |
||
| 738 | */ |
||
| 739 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @param OutputInterface $output |
||
| 746 | */ |
||
| 747 | protected function _addOutputStyles(OutputInterface $output) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @return ObjectManager |
||
| 755 | */ |
||
| 756 | public function getObjectManager() |
||
| 760 | } |
||
| 761 |
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.