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.12'; |
||
| 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 InputDefinition |
||
| 143 | */ |
||
| 144 | protected function getDefaultInputDefinition() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Search for magento root folder |
||
| 197 | * |
||
| 198 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 199 | * @param OutputInterface $output [optional] |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add own helpers to helperset. |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | protected function registerHelpers() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Try to bootstrap magento 2 and load cli application |
||
| 261 | * |
||
| 262 | * @param OutputInterface $output |
||
| 263 | */ |
||
| 264 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Override standard command registration. We want alias support. |
||
| 290 | * |
||
| 291 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 292 | * @return \Symfony\Component\Console\Command\Command |
||
| 293 | */ |
||
| 294 | public function add(Command $command) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param bool $mode |
||
| 305 | */ |
||
| 306 | public function setPharMode($mode) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function isPharMode() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @TODO Move logic into "EventSubscriber" |
||
| 321 | * |
||
| 322 | * @param OutputInterface $output |
||
| 323 | * @return null|false |
||
| 324 | */ |
||
| 325 | public function checkVarDir(OutputInterface $output) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Loads and initializes the Magento application |
||
| 397 | * |
||
| 398 | * @return bool false if magento root folder is not set, true otherwise |
||
| 399 | */ |
||
| 400 | public function initMagento() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getHelp() |
||
| 422 | |||
| 423 | public function getLongVersion() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return boolean |
||
| 430 | */ |
||
| 431 | public function isMagentoEnterprise() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getMagentoRootFolder() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $magentoRootFolder |
||
| 446 | */ |
||
| 447 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return int |
||
| 454 | */ |
||
| 455 | public function getMagentoMajorVersion() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @return ClassLoader |
||
| 462 | */ |
||
| 463 | public function getAutoloader() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param ClassLoader $autoloader |
||
| 470 | */ |
||
| 471 | public function setAutoloader(ClassLoader $autoloader) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function getConfig() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array $config |
||
| 486 | */ |
||
| 487 | public function setConfig($config) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return boolean |
||
| 494 | */ |
||
| 495 | public function isMagerunStopFileFound() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Runs the current application with possible command aliases |
||
| 502 | * |
||
| 503 | * @param InputInterface $input An Input instance |
||
| 504 | * @param OutputInterface $output An Output instance |
||
| 505 | * |
||
| 506 | * @return integer 0 if everything went fine, or an error code |
||
| 507 | */ |
||
| 508 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 529 | * @param OutputInterface $output [optional] |
||
| 530 | * |
||
| 531 | * @return int |
||
| 532 | */ |
||
| 533 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param array $initConfig |
||
| 568 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 569 | * @param OutputInterface $output |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param array $initConfig [optional] |
||
| 628 | * @param InputInterface $input [optional] |
||
|
1 ignored issue
–
show
|
|||
| 629 | * @param OutputInterface $output [optional] |
||
| 630 | */ |
||
| 631 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | protected function registerEventSubscribers() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @return bool |
||
| 651 | */ |
||
| 652 | protected function _checkSkipConfigOption() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @return bool |
||
| 661 | */ |
||
| 662 | protected function _checkSkipMagento2CoreCommandsOption() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | protected function _checkRootDirOption() |
||
| 694 | |||
| 695 | protected function _initMagento1() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return void |
||
| 729 | */ |
||
| 730 | protected function _initMagento2() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @return EventDispatcher |
||
| 750 | */ |
||
| 751 | public function getDispatcher() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * @param ConfigurationLoader $configurationLoader |
||
| 758 | */ |
||
| 759 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param OutputInterface $output |
||
| 766 | */ |
||
| 767 | protected function _addOutputStyles(OutputInterface $output) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @return ObjectManager |
||
| 775 | */ |
||
| 776 | public function getObjectManager() |
||
| 780 | } |
||
| 781 |
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.