| Conditions | 11 |
| Paths | 13 |
| Total Lines | 63 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 64 | public function run(array $inputArgs): int |
||
| 65 | { |
||
| 66 | try { |
||
| 67 | $config = $this->applicationFactory->createConfig($this->rootDirectory, $inputArgs); |
||
| 68 | } catch (InvalidParameterException $e) { |
||
| 69 | $this->notifyError($e->getMessage() . "\n"); |
||
| 70 | return self::RESULT_ERROR; |
||
| 71 | } |
||
| 72 | |||
| 73 | $scriptFinder = $this->applicationFactory->createScriptFinder($config); |
||
| 74 | |||
| 75 | $scriptNames = $this->extractScriptNames($inputArgs); |
||
| 76 | if (count($scriptNames) > 0 && $scriptNames[0] === 'bash_autocompletion_dump') { |
||
| 77 | $scripts = $scriptFinder->getAllScripts(); |
||
| 78 | $commands = array_map(function (Script $script) { |
||
| 79 | return $script->getName(); |
||
| 80 | }, $scripts); |
||
| 81 | echo implode(' ', $commands); |
||
| 82 | return self::RESULT_SUCCESS; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->printHeader($config); |
||
| 86 | |||
| 87 | $configFiles = $this->applicationFactory->getConfigFiles($this->rootDirectory); |
||
| 88 | $this->printConfigFiles($configFiles); |
||
| 89 | |||
| 90 | try { |
||
| 91 | foreach ($scriptNames as $scriptName) { |
||
| 92 | $executionExitCode = $this->execute($scriptFinder->findScriptByName($scriptName), $config); |
||
| 93 | |||
| 94 | if ($executionExitCode !== self::RESULT_SUCCESS) { |
||
| 95 | return $executionExitCode; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (isset($executionExitCode)) { |
||
| 100 | return $executionExitCode; |
||
| 101 | } |
||
| 102 | } catch (ScriptNotFoundException $e) { |
||
| 103 | $this->notifyError("Script with name {$inputArgs[1]} not found\n"); |
||
| 104 | |||
| 105 | $scripts = []; |
||
| 106 | foreach ($scriptNames as $scriptName) { |
||
| 107 | $newScripts = $scriptFinder->findScriptsByPartialName($scriptName); |
||
| 108 | $scripts = array_merge($scripts, $newScripts); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (count($scripts) > 0) { |
||
| 112 | $this->cliMate->yellow()->bold('Have you been looking for this?'); |
||
| 113 | $this->showListing($scripts); |
||
| 114 | } |
||
| 115 | return self::RESULT_ERROR; |
||
| 116 | } |
||
| 117 | |||
| 118 | try { |
||
| 119 | $this->showListing($scriptFinder->getAllScripts()); |
||
| 120 | } catch (ScriptPathNotValidException $e) { |
||
| 121 | $this->notifyError($e->getMessage() . "\n"); |
||
| 122 | return self::RESULT_ERROR; |
||
| 123 | } |
||
| 124 | |||
| 125 | return self::RESULT_SUCCESS; |
||
| 126 | } |
||
| 127 | |||
| 256 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: