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 |
||
| 18 | class MagentoHelper extends AbstractHelper |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $_magentoRootFolder = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $_magentoMajorVersion = \N98\Magento\Application::MAGENTO_MAJOR_VERSION_1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $_magentoEnterprise = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $_magerunStopFileFound = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $_magerunStopFileFolder = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var InputInterface |
||
| 47 | */ |
||
| 48 | protected $input; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OutputInterface |
||
| 52 | */ |
||
| 53 | protected $output; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $_customConfigFilename = 'n98-magerun.yaml'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns the canonical name of this helper. |
||
| 62 | * |
||
| 63 | * @return string The canonical name |
||
| 64 | * |
||
| 65 | * @api |
||
| 66 | */ |
||
| 67 | public function getName() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param InputInterface $input |
||
|
|
|||
| 74 | * @param OutputInterface $output |
||
| 75 | */ |
||
| 76 | public function __construct(InputInterface $input = null, OutputInterface $output = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Start Magento detection |
||
| 92 | * |
||
| 93 | * @param string $folder |
||
| 94 | * @param array $subFolders [optional] sub-folders to check |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function detect($folder, array $subFolders = array()) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getRootFolder() |
||
| 125 | |||
| 126 | public function getEdition() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isEnterpriseEdition() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getMajorVersion() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | public function isMagerunStopFileFound() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getMagerunStopFileFolder() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $folder |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | protected function splitPathFolders($folder) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check for modman file and .basedir |
||
| 185 | * |
||
| 186 | * @param array $folders |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | protected function checkModman(array $folders) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check for magerun stop-file |
||
| 234 | * |
||
| 235 | * @param array $folders |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | protected function checkMagerunFile(array $folders) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $searchFolder |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | protected function _search($searchFolder) |
||
| 344 | } |
||
| 345 |
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.