| Total Complexity | 79 |
| Total Lines | 484 |
| Duplicated Lines | 0 % |
| Changes | 40 | ||
| Bugs | 8 | Features | 0 |
Complex classes like ModuleUpgrader 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.
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 ModuleUpgrader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class ModuleUpgrader extends ModuleUpgraderBaseWithVariables |
||
| 9 | { |
||
| 10 | use Creator; |
||
| 11 | |||
| 12 | ############################### |
||
| 13 | # USEFUL COMMANDS |
||
| 14 | ############################### |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Executes given operations on the PHP2CommandLineSingleton instance |
||
| 18 | * Documentation for this can be found in the PHP2CommandLineSingleton module |
||
| 19 | * |
||
| 20 | * @param string $newDir root dir for ommand |
||
| 21 | * @param string $command actual command |
||
| 22 | * @param string $comment comment |
||
| 23 | * @param boolean $alwaysRun run even if you are just preparing a real run. Default FALSE |
||
| 24 | * |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | public function execMe( |
||
| 28 | string $newDir, |
||
| 29 | string $command, |
||
| 30 | string $comment, |
||
| 31 | ?bool $alwaysRun = false, |
||
| 32 | ?string $keyNotesLogFileLocation = '', |
||
| 33 | ?bool $verbose = true |
||
| 34 | ) { |
||
| 35 | if ($keyNotesLogFileLocation) { |
||
| 36 | $this->commandLineExec |
||
| 37 | ->setMakeKeyNotes(true) |
||
|
|
|||
| 38 | ->setKeyNotesFileLocation($keyNotesLogFileLocation); |
||
| 39 | } else { |
||
| 40 | $this->commandLineExec |
||
| 41 | ->setMakeKeyNotes(false); |
||
| 42 | } |
||
| 43 | $this->commandLineExec->setErrorMessage(''); |
||
| 44 | if ($this->getBreakOnAllErrors()) { |
||
| 45 | $this->commandLineExec->setErrorMessage(' |
||
| 46 | ------------------------------------------------------------------------ |
||
| 47 | To continue, please use the following parameter: startFrom=' . $this->currentlyRunning . ' |
||
| 48 | e.g. php runme.php startFrom=' . $this->currentlyRunning . ' |
||
| 49 | ------------------------------------------------------------------------ |
||
| 50 | '); |
||
| 51 | } |
||
| 52 | return $this->commandLineExec->execMe($newDir, $command, $comment, $alwaysRun, $verbose); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Executes given operations on the PHP2CommandLineSingleton instance |
||
| 57 | * Documentation for this can be found in the PHP2CommandLineSingleton module |
||
| 58 | */ |
||
| 59 | public function colourPrint($mixedVar, string $colour = 'dark_gray', $newLineCount = 1) |
||
| 60 | { |
||
| 61 | return $this->commandLineExec->colourPrint($mixedVar, $colour, $newLineCount); |
||
| 62 | } |
||
| 63 | |||
| 64 | ############################### |
||
| 65 | # RUN |
||
| 66 | ############################### |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Starts the command line output and prints some opening information to the output |
||
| 70 | * also initalises various environment variables |
||
| 71 | */ |
||
| 72 | public function run() |
||
| 73 | { |
||
| 74 | $this->applyRecipe(); |
||
| 75 | $this->colourPrint( |
||
| 76 | '===================== START ======================', |
||
| 77 | 'white', |
||
| 78 | 5 |
||
| 79 | ); |
||
| 80 | $this->loadNextStepInstructions(); |
||
| 81 | $this->loadGlobalVariables(); |
||
| 82 | $this->loadCustomVariablesForTasks(); |
||
| 83 | foreach ($this->arrayOfModules as $moduleDetails) { |
||
| 84 | $hasRun = false; |
||
| 85 | $nextStep = ''; |
||
| 86 | $this->loadVarsForModule($moduleDetails); |
||
| 87 | $this->workOutMethodsToRun(); |
||
| 88 | $this->printVarsForModule($moduleDetails); |
||
| 89 | foreach ($this->listOfTasks as $fauxClassName => $params) { |
||
| 90 | //get class without number |
||
| 91 | $properClass = current(explode('-', $fauxClassName)); |
||
| 92 | $nameSpacesArray = explode('\\', $properClass); |
||
| 93 | $shortClassCode = end($nameSpacesArray); |
||
| 94 | if (! class_exists($properClass)) { |
||
| 95 | $properClass = $this->defaultNamespaceForTasks . '\\' . $properClass; |
||
| 96 | } |
||
| 97 | if (class_exists($properClass)) { |
||
| 98 | $runItNow = $this->shouldWeRunIt((string) $fauxClassName); |
||
| 99 | $params['taskName'] = $shortClassCode; |
||
| 100 | $obj = $properClass::create($this, $params); |
||
| 101 | $taskName = $obj->getTaskName(); |
||
| 102 | if ($taskName) { |
||
| 103 | $params['taskName'] = $taskName; |
||
| 104 | } |
||
| 105 | if ($hasRun && ! $nextStep) { |
||
| 106 | $nextStep = $params['taskName']; |
||
| 107 | } |
||
| 108 | if ($runItNow) { |
||
| 109 | $this->currentlyRunning = $fauxClassName; |
||
| 110 | $this->colourPrint('# --------------------', 'yellow', 3); |
||
| 111 | $this->colourPrint('# ' . $obj->getTitle() . ' (' . $params['taskName'] . ')', 'yellow'); |
||
| 112 | $this->colourPrint('# --------------------', 'yellow'); |
||
| 113 | $this->colourPrint('# ' . $obj->getDescriptionNice(), 'light_green'); |
||
| 114 | $this->colourPrint('# --------------------', 'light_green'); |
||
| 115 | $obj->run(); |
||
| 116 | if ($this->runInteractively) { |
||
| 117 | $hasRun = true; |
||
| 118 | if ($this->outOfOrderTask === false) { |
||
| 119 | $this->getSessionManager()->setSessionValue('Completed', $fauxClassName); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | if (! $this->runInteractively) { |
||
| 124 | $this->colourPrint('# --------------------', 'yellow', 3); |
||
| 125 | $this->colourPrint('# ' . $obj->getTitle() . ' (' . $params['taskName'] . ')', 'yellow'); |
||
| 126 | $this->colourPrint('# --------------------', 'yellow'); |
||
| 127 | $this->colourPrint('# skipped', 'yellow'); |
||
| 128 | $this->colourPrint('# --------------------', 'yellow'); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $obj = $properClass::deleteTask($params); |
||
| 132 | } else { |
||
| 133 | user_error( |
||
| 134 | $properClass . ' could not be found as class. |
||
| 135 | You can add namespacing to include your own classes.', |
||
| 136 | E_USER_ERROR |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | $this->colourPrint( |
||
| 141 | '===================== END =======================', |
||
| 142 | 'white', |
||
| 143 | 5 |
||
| 144 | ); |
||
| 145 | $this->colourPrint( |
||
| 146 | 'Next: ' . $nextStep, |
||
| 147 | 'yellow', |
||
| 148 | 5 |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | $this->endPHP2CommandLine(); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function applyRecipe(?string $recipeName = null) |
||
| 155 | { |
||
| 156 | if ($recipeName === null) { |
||
| 157 | $recipeName = $this->getRecipe(); |
||
| 158 | } |
||
| 159 | if ($recipeName) { |
||
| 160 | if (isset($this->availableRecipes[$recipeName])) { |
||
| 161 | $recipeClass = $this->availableRecipes[$recipeName]; |
||
| 162 | $obj = new $recipeClass(); |
||
| 163 | $vars = $obj->getVariables(); |
||
| 164 | foreach ($vars as $variable => $value) { |
||
| 165 | $methodSet = 'set' . ucwords($variable); |
||
| 166 | $this->{$methodSet}($value); |
||
| 167 | } |
||
| 168 | } else { |
||
| 169 | user_error( |
||
| 170 | 'Recipe ' . $recipeName . ' not available. |
||
| 171 | Available Recipes are: ' . print_r($this->getAvailableRecipes(), 1) |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getLastMethodRun(): string |
||
| 180 | } |
||
| 181 | |||
| 182 | protected function loadNextStepInstructions() |
||
| 183 | { |
||
| 184 | $this->restartSession = $this->getCommandLineOrArgumentAsBoolean('restart'); |
||
| 185 | $this->runLastOneAgain = $this->getCommandLineOrArgumentAsBoolean('again'); |
||
| 186 | if ($this->getCommandLineOrArgumentAsString('startFrom')) { |
||
| 187 | $this->startFrom = $this->getCommandLineOrArgumentAsString('startFrom'); |
||
| 188 | if ($this->runInteractively) { |
||
| 189 | $this->onlyRun = $this->getCommandLineOrArgumentAsString('startFrom'); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | if ($this->getCommandLineOrArgumentAsString('endWith')) { |
||
| 193 | $this->endWith = $this->getCommandLineOrArgumentAsString('endWith'); |
||
| 194 | } |
||
| 195 | if ($this->getCommandLineOrArgumentAsString('task')) { |
||
| 196 | $this->runInteractively = true; |
||
| 197 | $this->onlyRun = $this->getCommandLineOrArgumentAsString('task'); |
||
| 198 | $this->outOfOrderTask = true; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function loadGlobalVariables() |
||
| 203 | { |
||
| 204 | $attempt = $this->aboveWebRootDirLocation; |
||
| 205 | $this->aboveWebRootDirLocation = $this->checkIfPathExistsAndCleanItUp( |
||
| 206 | $this->aboveWebRootDirLocation |
||
| 207 | ); |
||
| 208 | if (! $this->aboveWebRootDirLocation) { |
||
| 209 | die('You need the following directory for this application to work: ' . $attempt); |
||
| 210 | } |
||
| 211 | $this->webRootDirLocation = $this->checkIfPathExistsAndCleanItUp( |
||
| 212 | $this->aboveWebRootDirLocation . '/' . $this->webRootName, |
||
| 213 | true |
||
| 214 | ); |
||
| 215 | $this->themeDirLocation = $this->checkIfPathExistsAndCleanItUp( |
||
| 216 | $this->webRootDirLocation . '/themes', |
||
| 217 | true |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function loadCustomVariablesForTasks() |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Loads in and sets all the meta data for a module from the inputed array |
||
| 241 | * @param array $moduleDetails |
||
| 242 | */ |
||
| 243 | protected function loadVarsForModule($moduleDetails) |
||
| 244 | { |
||
| 245 | //Is Module Upgrade |
||
| 246 | //do this first as a lot of other functions rely on it ... |
||
| 247 | $this->isModuleUpgrade = isset($moduleDetails['IsModuleUpgrade']) ? (bool) $moduleDetails['IsModuleUpgrade'] : true; |
||
| 248 | $this->isOnPackagist = isset($moduleDetails['isOnPackagist']) ? (bool) $moduleDetails['isOnPackagist'] : false; |
||
| 249 | |||
| 250 | //VendorName |
||
| 251 | $this->vendorName = $moduleDetails['VendorName']; |
||
| 252 | |||
| 253 | //VendorNamespace |
||
| 254 | if (isset($moduleDetails['VendorNamespace'])) { |
||
| 255 | $this->vendorNamespace = $moduleDetails['VendorNamespace']; |
||
| 256 | } else { |
||
| 257 | $this->vendorNamespace = $this->cleanCamelCase($this->vendorName); |
||
| 258 | } |
||
| 259 | |||
| 260 | //PackageName |
||
| 261 | $this->packageName = $moduleDetails['PackageName']; |
||
| 262 | |||
| 263 | //PackageNamespace |
||
| 264 | if (isset($moduleDetails['PackageNamespace'])) { |
||
| 265 | $this->packageNamespace = $moduleDetails['PackageNamespace']; |
||
| 266 | } else { |
||
| 267 | $this->packageNamespace = $this->cleanCamelCase($this->packageName); |
||
| 268 | } |
||
| 269 | |||
| 270 | if (isset($moduleDetails['GitLink'])) { |
||
| 271 | $this->gitLink = $moduleDetails['GitLink']; |
||
| 272 | } else { |
||
| 273 | $this->gitLink = '[email protected]:' . $this->vendorName . '/silverstripe-' . $this->packageName . '.git'; |
||
| 274 | $this->gitLink = str_replace('silverstripe-silverstripe-', 'silverstripe-', $this->gitLink); |
||
| 275 | } |
||
| 276 | //see: https://stackoverflow.com/questions/5573334/remove-a-part-of-a-string-but-only-when-it-is-at-the-end-of-the-string |
||
| 277 | $gitLinkWithoutExtension = preg_replace('/' . preg_quote('.git', '/') . '$/', '', $this->gitLink); |
||
| 278 | $this->gitLinkAsHTTPS = str_replace( |
||
| 279 | '[email protected]:', |
||
| 280 | 'https://github.com/', |
||
| 281 | $gitLinkWithoutExtension |
||
| 282 | ); |
||
| 283 | $this->gitLinkAsRawHTTPS = str_replace( |
||
| 284 | '[email protected]:', |
||
| 285 | 'https://raw.githubusercontent.com/', |
||
| 286 | $gitLinkWithoutExtension |
||
| 287 | ); |
||
| 288 | $this->gitLinkAsHTTPS = str_replace( |
||
| 289 | '[email protected]:', |
||
| 290 | 'https://bitbucket.org/', |
||
| 291 | $gitLinkWithoutExtension |
||
| 292 | ); |
||
| 293 | $this->gitLinkAsRawHTTPS = str_replace( |
||
| 294 | '[email protected]:', |
||
| 295 | 'https://bitbucket.org/', |
||
| 296 | $gitLinkWithoutExtension |
||
| 297 | ); |
||
| 298 | if (stripos($this->gitLinkAsRawHTTPS, 'bitbucket') > 0) { |
||
| 299 | $this->gitLinkAsRawHTTPS .= '/raw'; |
||
| 300 | } |
||
| 301 | |||
| 302 | //Origin Composer FileLocation |
||
| 303 | $this->originComposerFileLocation = $moduleDetails['OriginComposerFileLocation'] ?? ''; |
||
| 304 | |||
| 305 | $this->workoutPackageFolderName($moduleDetails); |
||
| 306 | |||
| 307 | //ss4 location |
||
| 308 | if (isset($moduleDetails['VendorAndPackageFolderNameForInstall'])) { |
||
| 309 | $this->vendorAndPackageFolderNameForInstall = $moduleDetails['VendorAndPackageFolderNameForInstall']; |
||
| 310 | } else { |
||
| 311 | $this->vendorAndPackageFolderNameForInstall = strtolower($this->vendorName . '/' . $this->packageName); |
||
| 312 | } |
||
| 313 | |||
| 314 | //UpgradeAsFork |
||
| 315 | $this->upgradeAsFork = empty($moduleDetails['UpgradeAsFork']) ? false : true; |
||
| 316 | |||
| 317 | //NameOfBranchForBaseCode |
||
| 318 | $this->nameOfBranchForBaseCode = $moduleDetails['NameOfBranchForBaseCode'] ?? $this->nameOfBranchForBaseCode; |
||
| 319 | |||
| 320 | //LogFileLocation |
||
| 321 | $this->logFileLocation = ''; |
||
| 322 | if ($this->logFolderDirLocation) { |
||
| 323 | $this->logFileLocation = |
||
| 324 | $this->logFolderDirLocation . '/' . $this->packageName . |
||
| 325 | '-upgrade-log-' . date('Y-m-d') . |
||
| 326 | '.txt'; |
||
| 327 | $this->commandLineExec->setLogFileLocation($this->logFileLocation); |
||
| 328 | } else { |
||
| 329 | $this->commandLineExec->setLogFileLocation(''); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($this->restartSession) { |
||
| 333 | $this->getSessionManager()->deleteSession(); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | protected function vendorModuleLocation(): string |
||
| 338 | { |
||
| 339 | return $this->webRootDirLocation . '/vendor/' . $this->vendorName . '/' . $this->packageName; |
||
| 340 | } |
||
| 341 | |||
| 342 | protected function workoutPackageFolderName(array $moduleDetails): string |
||
| 392 | } |
||
| 393 | |||
| 394 | protected function testLocationFromRootDir(string $dir): bool |
||
| 395 | { |
||
| 396 | return (bool) file_exists($this->webRootDirLocation . '/' . $dir); |
||
| 397 | } |
||
| 398 | |||
| 399 | protected function printVarsForModule(array $moduleDetails) |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * work out the current one to run! |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function workOutMethodsToRun() |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * start the method ... |
||
| 462 | * - should we run it? |
||
| 463 | * |
||
| 464 | * @param string $name whatever is listed in the listOfTasks |
||
| 465 | */ |
||
| 466 | protected function shouldWeRunIt(string $name): bool |
||
| 494 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.