Complex classes like ProjectConfig 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 ProjectConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ProjectConfig |
||
| 17 | { |
||
| 18 | // Config Keys |
||
| 19 | const EXTRA_KEY = 'extra'; |
||
| 20 | |||
| 21 | const SORT_PRIORITY_KEY = 'magento-deploy-sort-priority'; |
||
| 22 | |||
| 23 | const MAGENTO_ROOT_DIR_KEY = 'magento-root-dir'; |
||
| 24 | |||
| 25 | const MAGENTO_PROJECT_KEY = 'magento-project'; |
||
| 26 | |||
| 27 | const MAGENTO_DEPLOY_STRATEGY_KEY = 'magento-deploystrategy'; |
||
| 28 | const MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY = 'magento-deploystrategy-overwrite'; |
||
| 29 | const MAGENTO_MAP_OVERWRITE_KEY = 'magento-map-overwrite'; |
||
| 30 | const MAGENTO_DEPLOY_IGNORE_KEY = 'magento-deploy-ignore'; |
||
| 31 | |||
| 32 | const MAGENTO_FORCE_KEY = 'magento-force'; |
||
| 33 | |||
| 34 | const AUTO_APPEND_GITIGNORE_KEY = 'auto-append-gitignore'; |
||
| 35 | |||
| 36 | const PATH_MAPPINGS_TRANSLATIONS_KEY = 'path-mapping-translations'; |
||
| 37 | |||
| 38 | // Default Values |
||
| 39 | const DEFAULT_MAGENTO_ROOT_DIR = 'root'; |
||
| 40 | |||
| 41 | const EXTRA_WITH_BOOTSTRAP_PATCH_KEY = 'with-bootstrap-patch'; |
||
| 42 | |||
| 43 | const EXTRA_WITH_SKIP_SUGGEST_KEY = 'skip-suggest-repositories'; |
||
| 44 | |||
| 45 | protected $libraryPath; |
||
| 46 | protected $libraryPackages; |
||
| 47 | protected $extra; |
||
| 48 | protected $composerConfig; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param array $extra |
||
| 52 | * @param array $composerConfig |
||
| 53 | */ |
||
| 54 | 31 | public function __construct(array $extra, array $composerConfig) |
|
| 55 | { |
||
| 56 | 31 | $this->extra = $extra; |
|
| 57 | 31 | $this->composerConfig = $composerConfig; |
|
| 58 | |||
| 59 | 31 | if (!is_null($projectConfig = $this->fetchVarFromConfigArray($this->extra, self::MAGENTO_PROJECT_KEY))) { |
|
| 60 | $this->applyMagentoConfig($projectConfig); |
||
| 61 | } |
||
| 62 | 31 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $array |
||
| 66 | * @param string|integer $key |
||
| 67 | * @param mixed $default |
||
| 68 | * |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | 31 | protected function fetchVarFromConfigArray($array, $key, $default = null) |
|
| 72 | { |
||
| 73 | 31 | $array = (array)$array; |
|
| 74 | 31 | $result = $default; |
|
| 75 | 31 | if (isset($array[$key])) { |
|
| 76 | 23 | $result = $array[$key]; |
|
| 77 | } |
||
| 78 | |||
| 79 | 31 | return $result; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $key |
||
| 84 | * @param null $default |
||
| 85 | * |
||
| 86 | * @return null |
||
| 87 | */ |
||
| 88 | 28 | protected function fetchVarFromExtraConfig($key, $default = null) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param $config |
||
| 95 | */ |
||
| 96 | protected function applyMagentoConfig($config) |
||
| 97 | { |
||
| 98 | $this->libraryPath = $this->fetchVarFromConfigArray($config, 'libraryPath'); |
||
| 99 | $this->libraryPackages = $this->fetchVarFromConfigArray($config, 'libraries'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 2 | public function getLibraryPath() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param $packagename |
||
| 112 | * |
||
| 113 | * @return null |
||
| 114 | */ |
||
| 115 | public function getLibraryConfigByPackagename($packagename) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 19 | public function getMagentoRootDir() |
|
| 124 | { |
||
| 125 | 19 | return rtrim( |
|
| 126 | trim( |
||
| 127 | 19 | $this->fetchVarFromExtraConfig( |
|
| 128 | 19 | self::MAGENTO_ROOT_DIR_KEY, |
|
| 129 | 19 | self::DEFAULT_MAGENTO_ROOT_DIR |
|
| 130 | ) |
||
| 131 | ), |
||
| 132 | 19 | DIRECTORY_SEPARATOR |
|
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param $rootDir |
||
| 138 | */ |
||
| 139 | public function setMagentoRootDir($rootDir) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 2 | public function hasMagentoRootDir() |
|
| 151 | |||
| 152 | public function getMagentoVarDir() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $deployStrategy |
||
| 159 | */ |
||
| 160 | public function setDeployStrategy($deployStrategy) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 11 | public function getDeployStrategy() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function hasDeployStrategy() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 6 | public function getDeployStrategyOverwrite() |
|
| 185 | { |
||
| 186 | 6 | return (array)$this->transformArrayKeysToLowerCase( |
|
| 187 | 6 | $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY, array()) |
|
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 6 | public function hasDeployStrategyOverwrite() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param $packagename |
||
| 201 | * |
||
| 202 | * @return integer |
||
| 203 | */ |
||
| 204 | 5 | public function getModuleSpecificDeployStrategy($packagename) |
|
| 205 | { |
||
| 206 | 5 | $moduleSpecificDeployStrategies = $this->getDeployStrategyOverwrite(); |
|
| 207 | |||
| 208 | 5 | $strategyName = $this->getDeployStrategy(); |
|
| 209 | 5 | if (isset($moduleSpecificDeployStrategies[$packagename])) { |
|
| 210 | $strategyName = $moduleSpecificDeployStrategies[$packagename]; |
||
| 211 | } |
||
| 212 | 5 | return $strategyName; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $packagename |
||
| 217 | * |
||
| 218 | * @return integer |
||
| 219 | */ |
||
| 220 | 1 | public function getModuleSpecificSortValue($packagename) |
|
| 221 | { |
||
| 222 | 1 | $sortPriorityArray = $this->fetchVarFromExtraConfig(self::SORT_PRIORITY_KEY, array()); |
|
|
|
|||
| 223 | 1 | if (isset($sortPriorityArray[$packagename])) { |
|
| 224 | $sortValue = $sortPriorityArray[$packagename]; |
||
| 225 | } else { |
||
| 226 | 1 | $sortValue = 100; |
|
| 227 | 1 | if ($this->getModuleSpecificDeployStrategy($packagename) === 'copy') { |
|
| 228 | $sortValue++; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | 1 | return $sortValue; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function getMagentoDeployIgnore() |
||
| 238 | { |
||
| 239 | return (array)$this->transformArrayKeysToLowerCase( |
||
| 240 | $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_IGNORE_KEY) |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param $packagename |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 11 | public function getModuleSpecificDeployIgnores($packagename) |
|
| 250 | { |
||
| 251 | 11 | $moduleSpecificDeployIgnores = array(); |
|
| 252 | 11 | if ($this->hasMagentoDeployIgnore()) { |
|
| 253 | $magentoDeployIgnore = $this->getMagentoDeployIgnore(); |
||
| 254 | if (isset($magentoDeployIgnore['*'])) { |
||
| 255 | $moduleSpecificDeployIgnores = $magentoDeployIgnore['*']; |
||
| 256 | } |
||
| 257 | if (isset($magentoDeployIgnore[$packagename])) { |
||
| 258 | $moduleSpecificDeployIgnores = array_merge( |
||
| 259 | $moduleSpecificDeployIgnores, |
||
| 260 | $magentoDeployIgnore[$packagename] |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | 11 | return $moduleSpecificDeployIgnores; |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 11 | public function hasMagentoDeployIgnore() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param $magentoForce |
||
| 277 | */ |
||
| 278 | public function setMagentoForce($magentoForce) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 11 | public function getMagentoForce() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function hasMagentoForce() |
||
| 298 | |||
| 299 | 11 | public function getMagentoForceByPackageName($packagename) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 5 | public function hasAutoAppendGitignore() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 1 | public function getPathMappingTranslations() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 2 | public function hasPathMappingTranslations() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function getMagentoDeployOverwrite() |
||
| 332 | { |
||
| 333 | return (array)$this->transformArrayKeysToLowerCase( |
||
| 334 | $this->fetchVarFromExtraConfig(self::MAGENTO_DEPLOY_STRATEGY_OVERWRITE_KEY) |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | 5 | public function getMagentoMapOverwrite() |
|
| 339 | { |
||
| 340 | 5 | return $this->transformArrayKeysToLowerCase( |
|
| 341 | 5 | (array)$this->fetchVarFromExtraConfig(self::MAGENTO_MAP_OVERWRITE_KEY) |
|
| 342 | ); |
||
| 343 | } |
||
| 344 | 18 | protected function hasExtraField($key) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param $key |
||
| 351 | * @param $value |
||
| 352 | */ |
||
| 353 | protected function updateExtraConfig($key, $value) |
||
| 354 | { |
||
| 355 | $this->extra[$key] = $value; |
||
| 356 | $this->updateExtraJson(); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @throws \Exception |
||
| 361 | */ |
||
| 362 | protected function updateExtraJson() |
||
| 363 | { |
||
| 364 | $composerFile = Factory::getComposerFile(); |
||
| 365 | |||
| 366 | if (!file_exists($composerFile) && !file_put_contents($composerFile, "{\n}\n")) { |
||
| 367 | throw new Exception(sprintf('%s could not be created', $composerFile)); |
||
| 368 | } |
||
| 369 | |||
| 370 | if (!is_readable($composerFile)) { |
||
| 371 | throw new Exception(sprintf('%s is not readable', $composerFile)); |
||
| 372 | } |
||
| 373 | |||
| 374 | if (!is_writable($composerFile)) { |
||
| 375 | throw new Exception(sprintf('%s is not writable', $composerFile)); |
||
| 376 | } |
||
| 377 | |||
| 378 | $json = new JsonFile($composerFile); |
||
| 379 | $composer = $json->read(); |
||
| 380 | |||
| 381 | $baseExtra = array_key_exists(self::EXTRA_KEY, $composer) |
||
| 382 | ? $composer[self::EXTRA_KEY] |
||
| 383 | : array(); |
||
| 384 | |||
| 385 | if (!$this->updateFileCleanly($json, $baseExtra, $this->extra, self::EXTRA_KEY)) { |
||
| 386 | foreach ($this->extra as $key => $value) { |
||
| 387 | $baseExtra[$key] = $value; |
||
| 388 | } |
||
| 389 | |||
| 390 | $composer[self::EXTRA_KEY] = $baseExtra; |
||
| 391 | $json->write($composer); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param JsonFile $json |
||
| 397 | * @param array $base |
||
| 398 | * @param array $new |
||
| 399 | * @param $rootKey |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | private function updateFileCleanly(JsonFile $json, array $base, array $new, $rootKey) |
||
| 404 | { |
||
| 405 | $contents = file_get_contents($json->getPath()); |
||
| 406 | |||
| 407 | $manipulator = new JsonManipulator($contents); |
||
| 408 | |||
| 409 | foreach ($new as $childKey => $childValue) { |
||
| 410 | if (!$manipulator->addLink($rootKey, $childKey, $childValue)) { |
||
| 411 | return false; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | file_put_contents($json->getPath(), $manipulator->getContents()); |
||
| 416 | |||
| 417 | return true; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $array |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | 11 | public function transformArrayKeysToLowerCase(array $array) |
|
| 429 | |||
| 430 | 4 | public function getComposerRepositories() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Get Composer vendor directory |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | 7 | public function getVendorDir() |
|
| 441 | { |
||
| 442 | 7 | return $this->fetchVarFromConfigArray( |
|
| 443 | 7 | isset($this->composerConfig['config']) ? $this->composerConfig['config'] : array(), |
|
| 444 | 7 | 'vendor-dir', |
|
| 445 | 7 | getcwd() . '/vendor' |
|
| 446 | ); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return boolean |
||
| 451 | */ |
||
| 452 | 7 | public function mustApplyBootstrapPatch() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return boolean |
||
| 459 | */ |
||
| 460 | 5 | public function skipSuggestComposerRepositories() |
|
| 464 | } |
||
| 465 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.