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 MagentoHelper 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 MagentoHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class MagentoHelper extends AbstractHelper |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $_magentoRootFolder = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $_magentoMajorVersion = \N98\Magento\Application::MAGENTO_MAJOR_VERSION_1; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $_magentoEnterprise = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $_magerunStopFileFound = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $_magerunStopFileFolder = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var InputInterface |
||
| 49 | */ |
||
| 50 | protected $input; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var OutputInterface |
||
| 54 | */ |
||
| 55 | protected $output; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $baseConfig = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $_customConfigFilename = 'n98-magerun2.yaml'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the canonical name of this helper. |
||
| 69 | * |
||
| 70 | * @return string The canonical name |
||
| 71 | * |
||
| 72 | * @api |
||
| 73 | */ |
||
| 74 | public function getName() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 81 | * @param OutputInterface $output |
||
| 82 | */ |
||
| 83 | public function __construct(InputInterface $input = null, OutputInterface $output = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Start Magento detection |
||
| 99 | * |
||
| 100 | * @param string $folder |
||
| 101 | * @param array $subFolders [optional] sub-folders to check |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function detect($folder, array $subFolders = array()) |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function getRootFolder() |
||
| 133 | |||
| 134 | public function getEdition() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function isEnterpriseEdition() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | public function getMajorVersion() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return boolean |
||
| 157 | */ |
||
| 158 | public function isMagerunStopFileFound() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getMagerunStopFileFolder() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $folder |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | protected function splitPathFolders($folder) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check for modman file and .basedir |
||
| 193 | * |
||
| 194 | * @param array $folders |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | protected function checkModman(array $folders) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check for .n98-magerun file |
||
| 242 | * |
||
| 243 | * @param array $folders |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | protected function checkMagerunFile(array $folders) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $searchFolder |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | protected function _search($searchFolder) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return array |
||
| 345 | * @throws RuntimeException |
||
| 346 | */ |
||
| 347 | public function getBaseConfig() |
||
| 355 | |||
| 356 | private function initBaseConfig() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * private getter for application that has magento detected |
||
| 374 | * |
||
| 375 | * @return Application|\Symfony\Component\Console\Application |
||
| 376 | */ |
||
| 377 | private function getApplication() |
||
| 389 | |||
| 390 | private function addBaseConfig($root, $configFileName) |
||
| 405 | } |
||
| 406 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.