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 |
||
30 | class Application extends BaseApplication |
||
31 | { |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | const APP_NAME = 'n98-magerun2'; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | const APP_VERSION = '1.3.0'; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private static $logo = " |
||
56 | ___ ___ ___ |
||
57 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |_ ) |
||
58 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ / / |
||
59 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_/___| |
||
60 | |___/ |
||
61 | "; |
||
62 | /** |
||
63 | * @var ClassLoader |
||
64 | */ |
||
65 | protected $autoloader; |
||
66 | |||
67 | /** |
||
68 | * @var Config |
||
69 | */ |
||
70 | protected $config; |
||
71 | |||
72 | /** |
||
73 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
74 | * @var ConfigurationLoader |
||
75 | */ |
||
76 | private $configurationLoaderInjected; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $_magentoRootFolder = null; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | protected $_magentoEnterprise = false; |
||
87 | |||
88 | /** |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
92 | |||
93 | /** |
||
94 | * @var EntryPoint |
||
95 | */ |
||
96 | protected $_magento2EntryPoint = null; |
||
97 | |||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $_isPharMode = false; |
||
102 | |||
103 | /** |
||
104 | * @var bool |
||
105 | */ |
||
106 | protected $_magerunStopFileFound = false; |
||
107 | |||
108 | /** |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $_magerunStopFileFolder = null; |
||
112 | |||
113 | /** |
||
114 | * @var bool |
||
115 | */ |
||
116 | protected $_isInitialized = false; |
||
117 | |||
118 | /** |
||
119 | * @var EventDispatcher |
||
120 | */ |
||
121 | protected $dispatcher; |
||
122 | |||
123 | /** |
||
124 | * If root dir is set by root-dir option this flag is true |
||
125 | * |
||
126 | * @var bool |
||
127 | */ |
||
128 | protected $_directRootDir = false; |
||
129 | |||
130 | /** |
||
131 | * @var bool |
||
132 | */ |
||
133 | protected $_magentoDetected = false; |
||
134 | |||
135 | /** |
||
136 | * @var ObjectManager |
||
137 | */ |
||
138 | protected $_objectManager = null; |
||
139 | |||
140 | /** |
||
141 | * @var boolean |
||
142 | */ |
||
143 | private $autoExit = true; |
||
|
|||
144 | |||
145 | /** |
||
146 | * @param ClassLoader $autoloader |
||
147 | */ |
||
148 | public function __construct($autoloader = null) |
||
153 | |||
154 | /** |
||
155 | * @return InputDefinition |
||
156 | */ |
||
157 | protected function getDefaultInputDefinition() |
||
207 | |||
208 | /** |
||
209 | * Sets whether to automatically exit after a command execution or not. |
||
210 | * |
||
211 | * Implemented on this level to allow early exit on configuration exceptions |
||
212 | * @see run() |
||
213 | * |
||
214 | * @param bool $boolean Whether to automatically exit after a command execution or not |
||
215 | */ |
||
216 | public function setAutoExit($boolean) |
||
221 | |||
222 | /** |
||
223 | * Search for magento root folder |
||
224 | * |
||
225 | * @param InputInterface $input [optional] |
||
226 | * @param OutputInterface $output [optional] |
||
227 | * @return void |
||
228 | */ |
||
229 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
267 | |||
268 | /** |
||
269 | * Add own helpers to helperset. |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function registerHelpers() |
||
293 | |||
294 | /** |
||
295 | * Try to bootstrap magento 2 and load cli application |
||
296 | * |
||
297 | * @param OutputInterface $output |
||
298 | */ |
||
299 | protected function registerMagentoCoreCommands(OutputInterface $output) |
||
324 | |||
325 | /** |
||
326 | * Override standard command registration. We want alias support. |
||
327 | * |
||
328 | * @param Command $command |
||
329 | * |
||
330 | * @return Command |
||
331 | */ |
||
332 | public function add(Command $command) |
||
340 | |||
341 | /** |
||
342 | * @param bool $mode |
||
343 | */ |
||
344 | public function setPharMode($mode) |
||
348 | |||
349 | /** |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function isPharMode() |
||
356 | |||
357 | /** |
||
358 | * @TODO Move logic into "EventSubscriber" |
||
359 | * |
||
360 | * @param OutputInterface $output |
||
361 | * @return null|false |
||
362 | */ |
||
363 | public function checkVarDir(OutputInterface $output) |
||
420 | |||
421 | /** |
||
422 | * Loads and initializes the Magento application |
||
423 | * |
||
424 | * @param bool $soft |
||
425 | * |
||
426 | * @return bool false if magento root folder is not set, true otherwise |
||
427 | */ |
||
428 | public function initMagento($soft = false) |
||
443 | |||
444 | /** |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getHelp() |
||
451 | |||
452 | public function getLongVersion() |
||
456 | |||
457 | /** |
||
458 | * @return boolean |
||
459 | */ |
||
460 | public function isMagentoEnterprise() |
||
464 | |||
465 | /** |
||
466 | * @return string |
||
467 | */ |
||
468 | public function getMagentoRootFolder() |
||
472 | |||
473 | /** |
||
474 | * @param string $magentoRootFolder |
||
475 | */ |
||
476 | public function setMagentoRootFolder($magentoRootFolder) |
||
480 | |||
481 | /** |
||
482 | * @return int |
||
483 | */ |
||
484 | public function getMagentoMajorVersion() |
||
488 | |||
489 | /** |
||
490 | * @return ClassLoader |
||
491 | */ |
||
492 | public function getAutoloader() |
||
496 | |||
497 | /** |
||
498 | * @param ClassLoader $autoloader |
||
499 | */ |
||
500 | public function setAutoloader(ClassLoader $autoloader) |
||
504 | |||
505 | /** |
||
506 | * @return array |
||
507 | */ |
||
508 | public function getConfig() |
||
512 | |||
513 | /** |
||
514 | * @param array $config |
||
515 | */ |
||
516 | public function setConfig($config) |
||
520 | |||
521 | /** |
||
522 | * @return boolean |
||
523 | */ |
||
524 | public function isMagerunStopFileFound() |
||
528 | |||
529 | /** |
||
530 | * Runs the current application with possible command aliases |
||
531 | * |
||
532 | * @param InputInterface $input An Input instance |
||
533 | * @param OutputInterface $output An Output instance |
||
534 | * |
||
535 | * @return integer 0 if everything went fine, or an error code |
||
536 | */ |
||
537 | public function doRun(InputInterface $input, OutputInterface $output) |
||
555 | |||
556 | /** |
||
557 | * @param InputInterface $input [optional] |
||
558 | * @param OutputInterface $output [optional] |
||
559 | * |
||
560 | * @return int |
||
561 | */ |
||
562 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
599 | |||
600 | /** |
||
601 | * @param array $initConfig [optional] |
||
602 | * @param InputInterface $input [optional] |
||
603 | * @param OutputInterface $output [optional] |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
660 | |||
661 | /** |
||
662 | * @param array $initConfig [optional] |
||
663 | * @param InputInterface $input [optional] |
||
664 | * @param OutputInterface $output [optional] |
||
665 | */ |
||
666 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
674 | |||
675 | /** |
||
676 | * @return void |
||
677 | */ |
||
678 | protected function registerEventSubscribers() |
||
687 | |||
688 | /** |
||
689 | * @param InputInterface $input |
||
690 | * @return bool |
||
691 | */ |
||
692 | protected function _checkSkipConfigOption(InputInterface $input) |
||
696 | |||
697 | /** |
||
698 | * @return bool |
||
699 | */ |
||
700 | protected function _checkSkipMagento2CoreCommandsOption(InputInterface $input) |
||
704 | |||
705 | /** |
||
706 | * @param InputInterface $input |
||
707 | * @return string |
||
708 | */ |
||
709 | protected function _checkRootDirOption(InputInterface $input) |
||
716 | |||
717 | /** |
||
718 | * Set root dir (chdir()) of magento directory |
||
719 | * |
||
720 | * @param string $path to Magento directory |
||
721 | */ |
||
722 | private function setRootDir($path) |
||
734 | |||
735 | /** |
||
736 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
737 | * and $this unbound. |
||
738 | * |
||
739 | * @param string $path |
||
740 | */ |
||
741 | private function requireOnce($path) |
||
752 | |||
753 | /** |
||
754 | * @param bool $soft |
||
755 | * |
||
756 | * @return void |
||
757 | */ |
||
758 | protected function _initMagento1($soft = false) |
||
762 | |||
763 | /** |
||
764 | * @return void |
||
765 | */ |
||
766 | protected function _initMagento2() |
||
783 | |||
784 | /** |
||
785 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
786 | * |
||
787 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
788 | */ |
||
789 | private function outputMagerunCompatibilityNotice($version) |
||
824 | |||
825 | /** |
||
826 | * @return EventDispatcher |
||
827 | */ |
||
828 | public function getDispatcher() |
||
832 | |||
833 | /** |
||
834 | * @param ConfigurationLoader $configurationLoader |
||
835 | */ |
||
836 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
846 | |||
847 | /** |
||
848 | * @param OutputInterface $output |
||
849 | */ |
||
850 | protected function _addOutputStyles(OutputInterface $output) |
||
855 | |||
856 | /** |
||
857 | * @return ObjectManager |
||
858 | */ |
||
859 | public function getObjectManager() |
||
863 | } |
||
864 |