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 |
||
31 | class Application extends BaseApplication |
||
32 | { |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | const APP_NAME = 'n98-magerun'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | const APP_VERSION = '1.97.29'; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
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 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $autoExit = true; |
||
|
|||
68 | |||
69 | /** |
||
70 | * @var ClassLoader |
||
71 | */ |
||
72 | protected $autoloader; |
||
73 | |||
74 | /** |
||
75 | * @var Config |
||
76 | */ |
||
77 | protected $config; |
||
78 | |||
79 | /** |
||
80 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
81 | * @var ConfigurationLoader |
||
82 | */ |
||
83 | private $configurationLoaderInjected; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $_magentoRootFolder = null; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $_magentoEnterprise = false; |
||
94 | |||
95 | /** |
||
96 | * @var int |
||
97 | */ |
||
98 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
99 | |||
100 | /** |
||
101 | * @var EntryPoint |
||
102 | */ |
||
103 | protected $_magento2EntryPoint = null; |
||
104 | |||
105 | /** |
||
106 | * @var bool |
||
107 | */ |
||
108 | protected $_isPharMode = false; |
||
109 | |||
110 | /** |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $_magerunStopFileFound = false; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $_magerunStopFileFolder = null; |
||
119 | |||
120 | /** |
||
121 | * @var bool |
||
122 | */ |
||
123 | protected $_isInitialized = false; |
||
124 | |||
125 | /** |
||
126 | * @var EventDispatcher |
||
127 | */ |
||
128 | protected $dispatcher; |
||
129 | |||
130 | /** |
||
131 | * If root dir is set by root-dir option this flag is true |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | protected $_directRootDir = false; |
||
136 | |||
137 | /** |
||
138 | * @var bool |
||
139 | */ |
||
140 | protected $_magentoDetected = false; |
||
141 | |||
142 | /** |
||
143 | * @param ClassLoader $autoloader |
||
144 | */ |
||
145 | public function __construct($autoloader = null) |
||
150 | |||
151 | /** |
||
152 | * @param bool $boolean |
||
153 | * @return bool previous auto-exit state |
||
154 | */ |
||
155 | public function setAutoExit($boolean) |
||
163 | |||
164 | /** |
||
165 | * @return InputDefinition |
||
166 | */ |
||
167 | protected function getDefaultInputDefinition() |
||
206 | |||
207 | /** |
||
208 | * Search for magento root folder |
||
209 | * |
||
210 | * @param InputInterface $input [optional] |
||
211 | * @param OutputInterface $output [optional] |
||
212 | * @return void |
||
213 | */ |
||
214 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
252 | |||
253 | /** |
||
254 | * Add own helpers to helperset. |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | protected function registerHelpers() |
||
278 | |||
279 | /** |
||
280 | * @param InputInterface $input |
||
281 | * |
||
282 | * @return ArgvInput|InputInterface |
||
283 | */ |
||
284 | protected function checkConfigCommandAlias(InputInterface $input) |
||
290 | |||
291 | /** |
||
292 | * @param Command $command |
||
293 | */ |
||
294 | protected function registerConfigCommandAlias(Command $command) |
||
300 | |||
301 | /** |
||
302 | * Adds autoloader prefixes from user's config |
||
303 | */ |
||
304 | protected function registerCustomAutoloaders() |
||
310 | |||
311 | /** |
||
312 | * @return bool |
||
313 | */ |
||
314 | protected function hasCustomCommands() |
||
322 | |||
323 | /** |
||
324 | * @return void |
||
325 | */ |
||
326 | protected function registerCustomCommands() |
||
332 | |||
333 | /** |
||
334 | * @param string $class |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function isCommandDisabled($class) |
||
345 | |||
346 | /** |
||
347 | * Override standard command registration. We want alias support. |
||
348 | * |
||
349 | * @param Command $command |
||
350 | * |
||
351 | * @return Command |
||
352 | */ |
||
353 | public function add(Command $command) |
||
361 | |||
362 | /** |
||
363 | * @param bool $mode |
||
364 | */ |
||
365 | public function setPharMode($mode) |
||
369 | |||
370 | /** |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function isPharMode() |
||
377 | |||
378 | /** |
||
379 | * @TODO Move logic into "EventSubscriber" |
||
380 | * |
||
381 | * @param OutputInterface $output |
||
382 | * @return null|false |
||
383 | */ |
||
384 | public function checkVarDir(OutputInterface $output) |
||
441 | |||
442 | /** |
||
443 | * Loads and initializes the Magento application |
||
444 | * |
||
445 | * @param bool $soft |
||
446 | * |
||
447 | * @return bool false if magento root folder is not set, true otherwise |
||
448 | */ |
||
449 | public function initMagento($soft = false) |
||
464 | |||
465 | /** |
||
466 | * @return string |
||
467 | */ |
||
468 | public function getHelp() |
||
472 | |||
473 | public function getLongVersion() |
||
477 | |||
478 | /** |
||
479 | * @return boolean |
||
480 | */ |
||
481 | public function isMagentoEnterprise() |
||
485 | |||
486 | /** |
||
487 | * @return string |
||
488 | */ |
||
489 | public function getMagentoRootFolder() |
||
493 | |||
494 | /** |
||
495 | * @param string $magentoRootFolder |
||
496 | */ |
||
497 | public function setMagentoRootFolder($magentoRootFolder) |
||
501 | |||
502 | /** |
||
503 | * @return int |
||
504 | */ |
||
505 | public function getMagentoMajorVersion() |
||
509 | |||
510 | /** |
||
511 | * @return ClassLoader |
||
512 | */ |
||
513 | public function getAutoloader() |
||
517 | |||
518 | /** |
||
519 | * @param ClassLoader $autoloader |
||
520 | */ |
||
521 | public function setAutoloader(ClassLoader $autoloader) |
||
525 | |||
526 | /** |
||
527 | * Get config array |
||
528 | * |
||
529 | * Specify one key per parameter to traverse the config. Then returns null |
||
530 | * if the path of the key(s) can not be obtained. |
||
531 | * |
||
532 | * @param string|int $key ... (optional) |
||
533 | * |
||
534 | * @return array|null |
||
535 | */ |
||
536 | public function getConfig($key = null) |
||
553 | |||
554 | /** |
||
555 | * @param array $config |
||
556 | */ |
||
557 | public function setConfig($config) |
||
561 | |||
562 | /** |
||
563 | * @return boolean |
||
564 | */ |
||
565 | public function isMagerunStopFileFound() |
||
569 | |||
570 | /** |
||
571 | * Runs the current application with possible command aliases |
||
572 | * |
||
573 | * @param InputInterface $input An Input instance |
||
574 | * @param OutputInterface $output An Output instance |
||
575 | * |
||
576 | * @return integer 0 if everything went fine, or an error code |
||
577 | */ |
||
578 | public function doRun(InputInterface $input, OutputInterface $output) |
||
596 | |||
597 | /** |
||
598 | * @param InputInterface $input [optional] |
||
599 | * @param OutputInterface $output [optional] |
||
600 | * |
||
601 | * @return int |
||
602 | */ |
||
603 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
635 | |||
636 | /** |
||
637 | * @param array $initConfig [optional] |
||
638 | * @param InputInterface $input [optional] |
||
639 | * @param OutputInterface $output [optional] |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
685 | |||
686 | /** |
||
687 | * @param array $initConfig [optional] |
||
688 | * @param InputInterface $input [optional] |
||
689 | * @param OutputInterface $output [optional] |
||
690 | */ |
||
691 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
699 | |||
700 | /** |
||
701 | * @return void |
||
702 | */ |
||
703 | protected function registerEventSubscribers() |
||
712 | |||
713 | /** |
||
714 | * @param InputInterface $input |
||
715 | * @return bool |
||
716 | * @deprecated 1.97.27 |
||
717 | */ |
||
718 | protected function _checkSkipConfigOption(InputInterface $input) |
||
727 | |||
728 | /** |
||
729 | * @param InputInterface $input |
||
730 | * @return string |
||
731 | */ |
||
732 | protected function _checkRootDirOption(InputInterface $input) |
||
739 | |||
740 | /** |
||
741 | * Set root dir (chdir()) of magento directory |
||
742 | * |
||
743 | * @param string $path to Magento directory |
||
744 | */ |
||
745 | private function setRootDir($path) |
||
757 | |||
758 | /** |
||
759 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
760 | * and $this unbound. |
||
761 | * |
||
762 | * @param string $path |
||
763 | */ |
||
764 | private function requireOnce($path) |
||
775 | |||
776 | /** |
||
777 | * @param bool $soft |
||
778 | * |
||
779 | * @return void |
||
780 | */ |
||
781 | protected function _initMagento1($soft = false) |
||
802 | |||
803 | /** |
||
804 | * @return void |
||
805 | */ |
||
806 | protected function _initMagento2() |
||
810 | |||
811 | /** |
||
812 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
813 | * |
||
814 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
815 | */ |
||
816 | private function outputMagerunCompatibilityNotice($version) |
||
851 | |||
852 | /** |
||
853 | * @return EventDispatcher |
||
854 | */ |
||
855 | public function getDispatcher() |
||
859 | |||
860 | /** |
||
861 | * @param array $initConfig |
||
862 | * @param OutputInterface $output |
||
863 | * @return ConfigurationLoader |
||
864 | */ |
||
865 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
879 | |||
880 | /** |
||
881 | * @param ConfigurationLoader $configurationLoader |
||
882 | * |
||
883 | * @return $this |
||
884 | */ |
||
885 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
897 | |||
898 | /** |
||
899 | * @param OutputInterface $output |
||
900 | */ |
||
901 | protected function _addOutputStyles(OutputInterface $output) |
||
906 | } |
||
907 |