Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 35 | class Application extends BaseApplication |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | const APP_NAME = 'n98-magerun2'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const APP_VERSION = '4.5.0-dev'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 51 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $logo = " |
||
| 57 | ___ ___ ___ |
||
| 58 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |_ ) |
||
| 59 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ / / |
||
| 60 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_/___| |
||
| 61 | |___/ |
||
| 62 | "; |
||
| 63 | /** |
||
| 64 | * @var ClassLoader |
||
| 65 | */ |
||
| 66 | protected $autoloader; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Config |
||
| 70 | */ |
||
| 71 | protected $config; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $_isPharMode = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected $_isInitialized = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var EventDispatcher |
||
| 85 | */ |
||
| 86 | protected $dispatcher; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ObjectManagerInterface |
||
| 90 | */ |
||
| 91 | protected $_objectManager; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 95 | * @var ConfigurationLoader |
||
| 96 | */ |
||
| 97 | private $configurationLoaderInjected; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string [optional] root folder not detected, but set via public setter |
||
| 101 | * @see setMagentoRootFolder() |
||
| 102 | */ |
||
| 103 | private $magentoRootFolderInjected; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var int Magento Major Version to operate on by this Magerun application |
||
| 107 | */ |
||
| 108 | private $magerunMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var DetectionResult of the Magento application (e.g. v1/v2, Enterprise/Community, root-path) |
||
| 112 | */ |
||
| 113 | private $detectionResult; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var boolean |
||
| 117 | */ |
||
| 118 | private $autoExit = true; |
||
|
|
|||
| 119 | |||
| 120 | /** |
||
| 121 | * @param ClassLoader $autoloader |
||
| 122 | */ |
||
| 123 | public function __construct($autoloader = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Sets whether to automatically exit after a command execution or not. |
||
| 133 | * |
||
| 134 | * Implemented on this level to allow early exit on configuration exceptions |
||
| 135 | * |
||
| 136 | * @see run() |
||
| 137 | * |
||
| 138 | * @param bool $boolean Whether to automatically exit after a command execution or not |
||
| 139 | */ |
||
| 140 | public function setAutoExit($boolean) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param bool $mode |
||
| 148 | */ |
||
| 149 | public function setPharMode($mode) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getHelp() |
||
| 161 | |||
| 162 | public function getLongVersion() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function isMagentoEnterprise() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $magentoRootFolder |
||
| 177 | */ |
||
| 178 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return int|null |
||
| 185 | */ |
||
| 186 | public function getMagentoMajorVersion() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function getConfig() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param array $config |
||
| 202 | */ |
||
| 203 | public function setConfig($config) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Runs the current application with possible command aliases |
||
| 210 | * |
||
| 211 | * @param InputInterface $input An Input instance |
||
| 212 | * @param OutputInterface $output An Output instance |
||
| 213 | * |
||
| 214 | * @return int 0 if everything went fine, or an error code |
||
| 215 | * @throws \Magento\Framework\Exception\FileSystemException |
||
| 216 | * @throws \Exception |
||
| 217 | * @throws \Throwable |
||
| 218 | */ |
||
| 219 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Loads and initializes the Magento application |
||
| 231 | * |
||
| 232 | * @param bool $soft |
||
| 233 | * |
||
| 234 | * @return bool false if magento root folder is not set, true otherwise |
||
| 235 | * @throws \Exception |
||
| 236 | */ |
||
| 237 | public function initMagento($soft = false) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param bool $preventException [optional] on uninitialized magento root folder (returns null then, caution!) |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function getMagentoRootFolder($preventException = false) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return ClassLoader |
||
| 279 | */ |
||
| 280 | public function getAutoloader() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param ClassLoader $autoloader |
||
| 287 | */ |
||
| 288 | public function setAutoloader(ClassLoader $autoloader) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param InputInterface $input [optional] |
||
| 295 | * @param OutputInterface $output [optional] |
||
| 296 | * |
||
| 297 | * @return int |
||
| 298 | * @throws \Exception |
||
| 299 | */ |
||
| 300 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param OutputInterface $output |
||
| 341 | */ |
||
| 342 | protected function _addOutputStyles(OutputInterface $output) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param array $initConfig [optional] |
||
| 350 | * @param InputInterface $input [optional] |
||
| 351 | * @param OutputInterface $output [optional] |
||
| 352 | * |
||
| 353 | * @return void |
||
| 354 | * @throws \Exception |
||
| 355 | */ |
||
| 356 | public function init(array $initConfig = [], InputInterface $input = null, OutputInterface $output = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param InputInterface $input |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public function isPharMode() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Search for magento root folder |
||
| 429 | * |
||
| 430 | * @param InputInterface $input [optional] |
||
| 431 | * @param OutputInterface $output [optional] |
||
| 432 | * @return void |
||
| 433 | * @throws \Exception |
||
| 434 | */ |
||
| 435 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | protected function _checkSkipMagento2CoreCommandsOption(InputInterface $input) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Try to bootstrap magento 2 and load cli application |
||
| 471 | * |
||
| 472 | * @param OutputInterface $output |
||
| 473 | */ |
||
| 474 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 518 | * and $this unbound. |
||
| 519 | * |
||
| 520 | * @param string $path |
||
| 521 | */ |
||
| 522 | View Code Duplication | private function requireOnce($path) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Override standard command registration. We want alias support. |
||
| 536 | * |
||
| 537 | * @param Command $command |
||
| 538 | * |
||
| 539 | * @return Command |
||
| 540 | */ |
||
| 541 | public function add(Command $command) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @return void |
||
| 552 | */ |
||
| 553 | protected function registerEventSubscribers(InputInterface $input, OutputInterface $output) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Add own helpers to helperset. |
||
| 575 | * |
||
| 576 | * @return void |
||
| 577 | */ |
||
| 578 | protected function registerHelpers() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param array $initConfig [optional] |
||
| 605 | * @param InputInterface $input [optional] |
||
| 606 | * @param OutputInterface $output [optional] |
||
| 607 | * @throws \Exception |
||
| 608 | */ |
||
| 609 | public function reinit($initConfig = [], InputInterface $input = null, OutputInterface $output = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @param ConfigurationLoader $configurationLoader |
||
| 619 | */ |
||
| 620 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @return ObjectManagerInterface |
||
| 633 | */ |
||
| 634 | public function getObjectManager() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return InputDefinition |
||
| 641 | */ |
||
| 642 | protected function getDefaultInputDefinition() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Force to load some classes before the Magento core loads the classes |
||
| 706 | * in a different version |
||
| 707 | */ |
||
| 708 | private function preloadClassesBeforeMagentoCore() |
||
| 714 | } |
||
| 715 |