Complex classes like AbstractMagentoCommand 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 AbstractMagentoCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class AbstractMagentoCommand extends Command |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $_magentoRootFolder = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_2; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $_magentoEnterprise = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $_deprecatedAlias = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $_websiteCodeMap = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ObjectManager |
||
| 59 | */ |
||
| 60 | protected $_objectManager = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initializes the command just after the input has been validated. |
||
| 64 | * |
||
| 65 | * This is mainly useful when a lot of commands extends one main command |
||
| 66 | * where some things need to be initialized based on the input arguments and options. |
||
| 67 | * |
||
| 68 | * @param InputInterface $input An InputInterface instance |
||
| 69 | * @param OutputInterface $output An OutputInterface instance |
||
| 70 | */ |
||
| 71 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 72 | { |
||
| 73 | $this->checkDeprecatedAliases($input, $output); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return ObjectManager |
||
| 78 | */ |
||
| 79 | protected function getObjectManager() |
||
| 80 | { |
||
| 81 | return $this->getApplication()->getObjectManager(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param array $codeArgument |
||
| 86 | * @param bool $status |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | protected function saveCacheStatus($codeArgument, $status) |
||
| 101 | |||
| 102 | private function _initWebsites() |
||
| 103 | { |
||
| 104 | $this->_websiteCodeMap = array(); |
||
| 105 | /** @var \Mage_Core_Model_Website[] $websites */ |
||
| 106 | $websites = Mage::app()->getWebsites(false); |
||
| 107 | foreach ($websites as $website) { |
||
| 108 | $this->_websiteCodeMap[$website->getId()] = $website->getCode(); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param int $websiteId |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | protected function _getWebsiteCodeById($websiteId) |
||
| 117 | { |
||
| 118 | if (empty($this->_websiteCodeMap)) { |
||
| 119 | $this->_initWebsites(); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (isset($this->_websiteCodeMap[$websiteId])) { |
||
| 123 | return $this->_websiteCodeMap[$websiteId]; |
||
| 124 | } |
||
| 125 | |||
| 126 | return ''; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $websiteCode |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | protected function _getWebsiteIdByCode($websiteCode) |
||
| 134 | { |
||
| 135 | if (empty($this->_websiteCodeMap)) { |
||
| 136 | $this->_initWebsites(); |
||
| 137 | } |
||
| 138 | $websiteMap = array_flip($this->_websiteCodeMap); |
||
| 139 | |||
| 140 | return $websiteMap[$websiteCode]; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string|null $commandClass |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | protected function getCommandConfig($commandClass = null) |
||
| 148 | { |
||
| 149 | if ($commandClass == null) { |
||
| 150 | $commandClass = get_class($this); |
||
| 151 | } |
||
| 152 | $configArray = $this->getApplication()->getConfig(); |
||
| 153 | if (isset($configArray['commands'][$commandClass])) { |
||
| 154 | return $configArray['commands'][$commandClass]; |
||
| 155 | } |
||
| 156 | |||
| 157 | return null; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param OutputInterface $output |
||
| 162 | * @param string $text |
||
| 163 | * @param string $style |
||
| 164 | */ |
||
| 165 | protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white') |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Bootstrap magento shop |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | protected function initMagento() |
||
| 183 | { |
||
| 184 | $init = $this->getApplication()->initMagento(); |
||
| 185 | if ($init) { |
||
| 186 | $this->_magentoRootFolder = $this->getApplication()->getMagentoRootFolder(); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $init; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Search for magento root folder |
||
| 194 | * |
||
| 195 | * @param OutputInterface $output |
||
| 196 | * @param bool $silent print debug messages |
||
| 197 | * @throws \RuntimeException |
||
| 198 | */ |
||
| 199 | public function detectMagento(OutputInterface $output, $silent = true) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Die if not Enterprise |
||
| 223 | */ |
||
| 224 | protected function requireEnterprise(OutputInterface $output) |
||
| 225 | { |
||
| 226 | if (!$this->_magentoEnterprise) { |
||
| 227 | $output->writeln('<error>Enterprise Edition is required but was not detected</error>'); |
||
| 228 | exit; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return \Mage_Core_Helper_Data |
||
| 234 | */ |
||
| 235 | protected function getCoreHelper() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param InputInterface $input |
||
| 246 | * @param OutputInterface $output |
||
| 247 | * @return \Composer\Downloader\DownloadManager |
||
| 248 | */ |
||
| 249 | public function getComposerDownloadManager($input, $output) |
||
| 250 | { |
||
| 251 | return $this->getComposer($input, $output)->getDownloadManager(); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param array|PackageInterface $config |
||
| 256 | * @return \Composer\Package\CompletePackage |
||
| 257 | */ |
||
| 258 | public function createComposerPackageByConfig($config) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param InputInterface $input |
||
| 267 | * @param OutputInterface $output |
||
| 268 | * @param array|PackageInterface $config |
||
| 269 | * @param string $targetFolder |
||
| 270 | * @param bool $preferSource |
||
| 271 | * @return \Composer\Package\CompletePackage |
||
| 272 | */ |
||
| 273 | public function downloadByComposerConfig( |
||
| 299 | |||
| 300 | /** |
||
| 301 | * brings locally cached repository up to date if it is missing the requested tag |
||
| 302 | * |
||
| 303 | * @param PackageInterface $package |
||
| 304 | * @param string $targetFolder |
||
| 305 | */ |
||
| 306 | protected function checkRepository(PackageInterface $package, $targetFolder) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $type |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function isSourceTypeRepository($type) |
||
| 339 | { |
||
| 340 | return in_array($type, array('git', 'hg')); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * obtain composer |
||
| 345 | * |
||
| 346 | * @param InputInterface $input |
||
| 347 | * @param OutputInterface $output |
||
| 348 | * |
||
| 349 | * @return \Composer\Composer |
||
| 350 | */ |
||
| 351 | public function getComposer(InputInterface $input, OutputInterface $output) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $alias |
||
| 365 | * @param string $message |
||
| 366 | * @return AbstractMagentoCommand |
||
| 367 | */ |
||
| 368 | protected function addDeprecatedAlias($alias, $message) |
||
| 369 | { |
||
| 370 | $this->_deprecatedAlias[$alias] = $message; |
||
| 371 | |||
| 372 | return $this; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param InputInterface $input |
||
| 377 | * @param OutputInterface $output |
||
| 378 | */ |
||
| 379 | protected function checkDeprecatedAliases(InputInterface $input, OutputInterface $output) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $value |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | protected function _parseBoolOption($value) |
||
| 394 | { |
||
| 395 | return in_array(strtolower($value), array('y', 'yes', 1, 'true')); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $value |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function parseBoolOption($value) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $value |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function formatActive($value) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param InputInterface $input |
||
| 422 | * @param OutputInterface $output |
||
| 423 | * |
||
| 424 | * @return int |
||
| 425 | */ |
||
| 426 | public function run(InputInterface $input, OutputInterface $output) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param OutputInterface $output |
||
| 437 | */ |
||
| 438 | public function injectObjects(OutputInterface $output) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param InputInterface $input |
||
| 455 | * @param OutputInterface $output |
||
| 456 | * @param string $baseNamespace If this is set we can use relative class names. |
||
| 457 | * |
||
| 458 | * @return SubCommandFactory |
||
| 459 | */ |
||
| 460 | protected function createSubCommandFactory( |
||
| 481 | } |
||
| 482 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.