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 |
||
| 22 | class MagentoHelper extends AbstractHelper |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $_magentoRootFolder = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $_magentoMajorVersion = \N98\Magento\Application::MAGENTO_MAJOR_VERSION_1; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $_magentoEnterprise = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $_magerunStopFileFound = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $_magerunStopFileFolder = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var InputInterface |
||
| 51 | */ |
||
| 52 | protected $input; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var OutputInterface |
||
| 56 | */ |
||
| 57 | protected $output; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $baseConfig = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $_customConfigFilename = 'n98-magerun2.yaml'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the canonical name of this helper. |
||
| 71 | * |
||
| 72 | * @return string The canonical name |
||
| 73 | * |
||
| 74 | * @api |
||
| 75 | */ |
||
| 76 | public function getName() |
||
| 77 | { |
||
| 78 | return 'magento'; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param InputInterface $input |
||
|
1 ignored issue
–
show
|
|||
| 83 | * @param OutputInterface $output |
||
|
1 ignored issue
–
show
|
|||
| 84 | */ |
||
| 85 | public function __construct(InputInterface $input = null, OutputInterface $output = null) |
||
| 86 | { |
||
| 87 | if (null === $input) { |
||
| 88 | $input = new ArgvInput(); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (null === $output) { |
||
| 92 | $output = new ConsoleOutput(); |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->input = $input; |
||
| 96 | $this->output = $output; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Start Magento detection |
||
| 101 | * |
||
| 102 | * @param string $folder |
||
| 103 | * @param array $subFolders [optional] sub-folders to check |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function detect($folder, array $subFolders = array()) |
||
| 107 | { |
||
| 108 | $folders = $this->splitPathFolders($folder); |
||
| 109 | $folders = $this->checkMagerunFile($folders); |
||
| 110 | $folders = $this->checkModman($folders); |
||
| 111 | $folders = array_merge($folders, $subFolders); |
||
| 112 | |||
| 113 | foreach (array_reverse($folders) as $searchFolder) { |
||
| 114 | if (!is_dir($searchFolder) || !is_readable($searchFolder)) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | $found = $this->_search($searchFolder); |
||
| 119 | if ($found) { |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getRootFolder() |
||
| 132 | { |
||
| 133 | return $this->_magentoRootFolder; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getEdition() |
||
| 137 | { |
||
| 138 | return $this->_magentoMajorVersion; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function isEnterpriseEdition() |
||
| 145 | { |
||
| 146 | return $this->_magentoEnterprise; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function getMajorVersion() |
||
| 153 | { |
||
| 154 | return $this->_magentoMajorVersion; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | public function isMagerunStopFileFound() |
||
| 161 | { |
||
| 162 | return $this->_magerunStopFileFound; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getMagerunStopFileFolder() |
||
| 169 | { |
||
| 170 | return $this->_magerunStopFileFolder; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $folder |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | protected function splitPathFolders($folder) |
||
| 179 | { |
||
| 180 | $folders = array(); |
||
| 181 | |||
| 182 | $folderParts = explode('/', $folder); |
||
| 183 | foreach ($folderParts as $key => $part) { |
||
| 184 | $explodedFolder = implode('/', array_slice($folderParts, 0, $key + 1)); |
||
| 185 | if ($explodedFolder !== '') { |
||
| 186 | $folders[] = $explodedFolder; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return $folders; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check for modman file and .basedir |
||
| 195 | * |
||
| 196 | * @param array $folders |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function checkModman(array $folders) |
||
| 201 | { |
||
| 202 | foreach ($this->searchFolders($folders) as $searchFolder) { |
||
| 203 | $finder = Finder::create(); |
||
| 204 | $finder |
||
| 205 | ->files() |
||
| 206 | ->ignoreUnreadableDirs(true) |
||
| 207 | ->depth(0) |
||
| 208 | ->followLinks() |
||
| 209 | ->ignoreDotFiles(false) |
||
| 210 | ->name('.basedir') |
||
| 211 | ->in($searchFolder); |
||
| 212 | |||
| 213 | $count = $finder->count(); |
||
| 214 | if ($count > 0) { |
||
| 215 | $baseFolderContent = trim(file_get_contents($searchFolder . '/.basedir')); |
||
| 216 | $this->writeDebug('Found modman .basedir file with content <info>' . $baseFolderContent . '</info>'); |
||
| 217 | |||
| 218 | if (!empty($baseFolderContent)) { |
||
| 219 | array_push($folders, $searchFolder . '/../' . $baseFolderContent); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | return $folders; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Check for magerun stop-file |
||
| 229 | * |
||
| 230 | * @param array $folders |
||
| 231 | * |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | protected function checkMagerunFile(array $folders) |
||
| 235 | { |
||
| 236 | foreach ($this->searchFolders($folders) as $searchFolder) { |
||
| 237 | $stopFile = '.' . pathinfo($this->_customConfigFilename, PATHINFO_FILENAME); |
||
| 238 | $finder = Finder::create(); |
||
| 239 | $finder |
||
| 240 | ->files() |
||
| 241 | ->ignoreUnreadableDirs(true) |
||
| 242 | ->depth(0) |
||
| 243 | ->followLinks() |
||
| 244 | ->ignoreDotFiles(false) |
||
| 245 | ->name($stopFile) |
||
| 246 | ->in($searchFolder); |
||
| 247 | |||
| 248 | $count = $finder->count(); |
||
| 249 | if ($count > 0) { |
||
| 250 | $this->_magerunStopFileFound = true; |
||
| 251 | $this->_magerunStopFileFolder = $searchFolder; |
||
| 252 | $magerunFilePath = $searchFolder . '/' . $stopFile; |
||
| 253 | $magerunFileContent = trim(file_get_contents($magerunFilePath)); |
||
| 254 | $message = sprintf( |
||
| 255 | 'Found stopfile \'%s\' file with content <info>%s</info>', |
||
| 256 | $stopFile, |
||
| 257 | $magerunFileContent |
||
| 258 | ); |
||
| 259 | $this->writeDebug($message); |
||
| 260 | |||
| 261 | array_push($folders, $searchFolder . '/' . $magerunFileContent); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | return $folders; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Turn an array of folders into a Traversable of readable paths. |
||
| 270 | * |
||
| 271 | * @param array $folders |
||
| 272 | * @return CallbackFilterIterator Traversable of strings that are readable paths |
||
| 273 | */ |
||
| 274 | private function searchFolders(array $folders) |
||
| 275 | { |
||
| 276 | $that = $this; |
||
| 277 | |||
| 278 | $callback = function ($searchFolder) use ($that) { |
||
| 279 | if (!is_readable($searchFolder)) { |
||
| 280 | $that->writeDebug('Folder <info>' . $searchFolder . '</info> is not readable. Skip.'); |
||
| 281 | |||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | return true; |
||
| 286 | }; |
||
| 287 | |||
| 288 | return new CallbackFilterIterator( |
||
| 289 | new ArrayIterator(array_reverse($folders)), |
||
| 290 | $callback |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $searchFolder |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | protected function _search($searchFolder) |
||
| 300 | { |
||
| 301 | $this->writeDebug('Search for Magento in folder <info>' . $searchFolder . '</info>'); |
||
| 302 | |||
| 303 | if (!is_dir($searchFolder . '/app')) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | |||
| 307 | $finder = Finder::create(); |
||
| 308 | $finder |
||
| 309 | ->ignoreUnreadableDirs(true) |
||
| 310 | ->depth(0) |
||
| 311 | ->followLinks() |
||
| 312 | ->name('Mage.php') |
||
| 313 | ->name('bootstrap.php') |
||
| 314 | ->name('autoload.php') |
||
| 315 | ->in($searchFolder . '/app'); |
||
| 316 | |||
| 317 | if ($finder->count() > 0) { |
||
| 318 | $files = iterator_to_array($finder, false); |
||
| 319 | /* @var $file \SplFileInfo */ |
||
| 320 | |||
| 321 | $hasMageFile = false; |
||
| 322 | foreach ($files as $file) { |
||
| 323 | if ($file->getFilename() == 'Mage.php') { |
||
| 324 | $hasMageFile = true; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->_magentoRootFolder = $searchFolder; |
||
| 329 | |||
| 330 | // Magento 2 does not have a god class and thus if this file is not there it is version 2 |
||
| 331 | if ($hasMageFile == false) { |
||
| 332 | $this->_magentoMajorVersion = Application::MAGENTO_MAJOR_VERSION_2; |
||
| 333 | } else { |
||
| 334 | $this->_magentoMajorVersion = Application::MAGENTO_MAJOR_VERSION_1; |
||
| 335 | } |
||
| 336 | |||
| 337 | $this->writeDebug('Found Magento in folder <info>' . $this->_magentoRootFolder . '</info>'); |
||
| 338 | |||
| 339 | return true; |
||
| 340 | } |
||
| 341 | |||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return array |
||
| 347 | * @throws RuntimeException |
||
| 348 | */ |
||
| 349 | public function getBaseConfig() |
||
| 357 | |||
| 358 | private function initBaseConfig() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * private getter for application that has magento detected |
||
| 376 | * |
||
| 377 | * @return Application |
||
| 378 | */ |
||
| 379 | private function getApplication() |
||
| 380 | { |
||
| 381 | $command = $this->getHelperSet()->getCommand(); |
||
| 382 | |||
| 383 | $application = $command ? $command->getApplication() : new Application(); |
||
| 384 | |||
| 385 | // verify type because of detectMagento() call below |
||
| 386 | if (!$application instanceof Application) { |
||
| 387 | throw new UnexpectedValueException( |
||
| 388 | sprintf('Expected magerun application got %s', get_class($application)) |
||
| 389 | ); |
||
| 390 | } |
||
| 391 | |||
| 392 | $application->detectMagento(); |
||
| 393 | |||
| 394 | return $application; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $root |
||
| 399 | * @param string $configFileName |
||
| 400 | */ |
||
| 401 | private function addBaseConfig($root, $configFileName) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $message |
||
| 419 | * @return void |
||
| 420 | */ |
||
| 421 | private function writeDebug($message) |
||
| 429 | } |
||
| 430 |
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.