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_1 = 1; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $_magentoRootFolder = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $_magentoEnterprise = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $_deprecatedAlias = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $_websiteCodeMap = array(); |
||
| 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) |
||
| 75 | |||
| 76 | private function _initWebsites() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param int $websiteId |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | protected function _getWebsiteCodeById($websiteId) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $websiteCode |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | protected function _getWebsiteIdByCode($websiteCode) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string|null $commandClass |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | protected function getCommandConfig($commandClass = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param OutputInterface $output |
||
| 134 | * @param string $text |
||
| 135 | * @param string $style |
||
| 136 | */ |
||
| 137 | protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white') |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Bootstrap magento shop |
||
| 148 | * |
||
| 149 | * @param bool $soft |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | protected function initMagento($soft = false) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Search for magento root folder |
||
| 165 | * |
||
| 166 | * @param OutputInterface $output |
||
| 167 | * @param bool $silent print debug messages |
||
| 168 | * @throws RuntimeException |
||
| 169 | */ |
||
| 170 | public function detectMagento(OutputInterface $output, $silent = true) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Die if not Enterprise |
||
| 194 | */ |
||
| 195 | protected function requireEnterprise(OutputInterface $output) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return \Mage_Core_Helper_Data |
||
| 205 | */ |
||
| 206 | protected function getCoreHelper() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param InputInterface $input |
||
| 216 | * @param OutputInterface $output |
||
| 217 | * @return \Composer\Downloader\DownloadManager |
||
| 218 | */ |
||
| 219 | protected function getComposerDownloadManager($input, $output) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param array|PackageInterface $config |
||
| 226 | * @return \Composer\Package\CompletePackage |
||
| 227 | */ |
||
| 228 | protected function createComposerPackageByConfig($config) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param InputInterface $input |
||
| 236 | * @param OutputInterface $output |
||
| 237 | * @param array|PackageInterface $config |
||
| 238 | * @param string $targetFolder |
||
| 239 | * @param bool $preferSource |
||
| 240 | * @return \Composer\Package\CompletePackage |
||
| 241 | */ |
||
| 242 | protected function downloadByComposerConfig( |
||
| 268 | |||
| 269 | /** |
||
| 270 | * brings locally cached repository up to date if it is missing the requested tag |
||
| 271 | * |
||
| 272 | * @param PackageInterface $package |
||
| 273 | * @param string $targetFolder |
||
| 274 | */ |
||
| 275 | protected function checkRepository($package, $targetFolder) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * normalize paths on windows / cygwin / msysgit |
||
| 304 | * |
||
| 305 | * when using a path value that has been created in a cygwin shell but then PHP uses it inside a cmd shell it needs |
||
| 306 | * to be filtered. |
||
| 307 | * |
||
| 308 | * @param string $path |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | protected function normalizePath($path) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * obtain composer |
||
| 321 | * |
||
| 322 | * @param InputInterface $input |
||
| 323 | * @param OutputInterface $output |
||
| 324 | * |
||
| 325 | * @return \Composer\Composer |
||
| 326 | */ |
||
| 327 | protected function getComposer(InputInterface $input, OutputInterface $output) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $alias |
||
| 341 | * @param string $message |
||
| 342 | * @return AbstractMagentoCommand |
||
| 343 | */ |
||
| 344 | protected function addDeprecatedAlias($alias, $message) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param InputInterface $input |
||
| 353 | * @param OutputInterface $output |
||
| 354 | */ |
||
| 355 | protected function checkDeprecatedAliases(InputInterface $input, OutputInterface $output) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Magento 1 / 2 switches |
||
| 367 | * |
||
| 368 | * @param string $mage1code Magento 1 class code |
||
| 369 | * @param string $mage2class Magento 2 class name |
||
| 370 | * @return \Mage_Core_Model_Abstract |
||
| 371 | */ |
||
| 372 | protected function _getModel($mage1code, $mage2class) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Magento 1 / 2 switches |
||
| 383 | * |
||
| 384 | * @param string $mage1code Magento 1 class code |
||
| 385 | * @param string $mage2class Magento 2 class name |
||
| 386 | * @return \Mage_Core_Helper_Abstract |
||
| 387 | */ |
||
| 388 | protected function _getHelper($mage1code, $mage2class) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Magento 1 / 2 switches |
||
| 399 | * |
||
| 400 | * @param string $mage1code Magento 1 class code |
||
| 401 | * @param string $mage2class Magento 2 class name |
||
| 402 | * @return \Mage_Core_Model_Abstract |
||
| 403 | */ |
||
| 404 | protected function _getSingleton($mage1code, $mage2class) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Magento 1 / 2 switches |
||
| 415 | * |
||
| 416 | * @param string $mage1code Magento 1 class code |
||
| 417 | * @param string $mage2class Magento 2 class name |
||
| 418 | * @return \Mage_Core_Model_Abstract |
||
| 419 | */ |
||
| 420 | protected function _getResourceModel($mage1code, $mage2class) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Magento 1 / 2 switches |
||
| 431 | * |
||
| 432 | * @param string $mage1code Magento 1 class code |
||
| 433 | * @param string $mage2class Magento 2 class name |
||
| 434 | * @return \Mage_Core_Model_Abstract |
||
| 435 | */ |
||
| 436 | protected function _getResourceSingleton($mage1code, $mage2class) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $value |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | protected function _parseBoolOption($value) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $value |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | protected function formatActive($value) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param InputInterface $input |
||
| 465 | * @param OutputInterface $output |
||
| 466 | * |
||
| 467 | * @return int |
||
| 468 | */ |
||
| 469 | public function run(InputInterface $input, OutputInterface $output) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param InputInterface $input |
||
| 478 | * @param OutputInterface $output |
||
| 479 | */ |
||
| 480 | protected function chooseInstallationFolder(InputInterface $input, OutputInterface $output) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param string $type |
||
| 558 | * |
||
| 559 | * @return bool |
||
| 560 | */ |
||
| 561 | protected function isSourceTypeRepository($type) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param string $argument |
||
| 568 | * @param InputInterface $input |
||
| 569 | * @param OutputInterface $output |
||
| 570 | * @param string $message |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | protected function getOrAskForArgument($argument, InputInterface $input, OutputInterface $output, $message = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param array $entries zero-indexed array of entries (represented by strings) to select from |
||
| 589 | * @param OutputInterface $output |
||
| 590 | * @param string $question |
||
| 591 | * @return mixed |
||
| 592 | */ |
||
| 593 | protected function askForArrayEntry(array $entries, OutputInterface $output, $question) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $argument |
||
| 614 | * @param string $message [optional] |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | protected function getArgumentMessage($argument, $message = null) |
||
| 625 | } |
||
| 626 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: