| Conditions | 25 |
| Paths | 4764 |
| Total Lines | 161 |
| Code Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 105 | protected function executeSqlAction($instanceList, $actionName, $getSqlActionCallable, $timed = false, $onForkedProcessOutput = null) |
||
| 106 | { |
||
| 107 | $processes = []; |
||
| 108 | $callables = []; |
||
| 109 | $outputFilters = []; |
||
| 110 | $tempSQLFileNames = []; |
||
| 111 | $executors = []; |
||
| 112 | |||
| 113 | try { |
||
| 114 | |||
| 115 | foreach ($instanceList as $instanceName => $dbConnectionSpec) { |
||
| 116 | |||
| 117 | $schemaManager = $this->databaseManagerFactory->getDatabaseManager($dbConnectionSpec); |
||
| 118 | |||
| 119 | /** @var CommandAction|FileAction $sqlAction */ |
||
| 120 | $sqlAction = call_user_func_array($getSqlActionCallable, [$schemaManager, $instanceName]); |
||
| 121 | |||
| 122 | if ($sqlAction instanceof CommandAction) { |
||
| 123 | $filename = null; |
||
| 124 | $sql = $sqlAction->getCommand(); |
||
| 125 | } else if ($sqlAction instanceof FileAction) { |
||
| 126 | $filename = $sqlAction->getFilename(); |
||
| 127 | $sql = null; |
||
| 128 | } else { |
||
| 129 | // this is a coding error, not a sql execution error |
||
| 130 | throw new \Exception("Unsupported action type: " . get_class($sqlAction)); |
||
| 131 | } |
||
| 132 | $filterCallable = $sqlAction->getResultsFilterCallable(); |
||
| 133 | |||
| 134 | if ($filename === null && $sql === null) { |
||
| 135 | // no sql to execute as forked process - we run the 'filter' functions in a separate loop |
||
| 136 | $callables[$instanceName] = $filterCallable; |
||
| 137 | } else { |
||
| 138 | $outputFilters[$instanceName] = $filterCallable; |
||
| 139 | |||
| 140 | if ($filename === null && !$sqlAction->isSingleStatement()) { |
||
| 141 | $filename = tempnam(sys_get_temp_dir(), 'db3v4l_') . '.sql'; |
||
| 142 | file_put_contents($filename, $sql); |
||
| 143 | $tempSQLFileNames[] = $filename; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->executeInProcess) { |
||
| 147 | $executor = $this->executorFactory->createInProcessExecutor($instanceName, $dbConnectionSpec, $this->executionStrategy, $timed); |
||
| 148 | if ($filename === null) { |
||
| 149 | $callables[$instanceName] = $executor->getExecuteCommandCallable($sql); |
||
| 150 | } else { |
||
| 151 | $callables[$instanceName] = $executor->getExecuteFileCallable($filename); |
||
| 152 | } |
||
| 153 | } else { |
||
| 154 | $executor = $this->executorFactory->createForkedExecutor($instanceName, $dbConnectionSpec, $this->executionStrategy, $timed); |
||
| 155 | $executors[$instanceName] = $executor; |
||
| 156 | |||
| 157 | if ($filename === null) { |
||
| 158 | $process = $executor->getExecuteStatementProcess($sql); |
||
| 159 | } else { |
||
| 160 | $process = $executor->getExecuteFileProcess($filename); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($this->outputFormat === 'text') { |
||
| 164 | $this->writeln('Command line: ' . $process->getCommandLine(), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 165 | } |
||
| 166 | |||
| 167 | $process->setTimeout($this->processTimeout); |
||
| 168 | |||
| 169 | $processes[$instanceName] = $process; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /// @todo refactor the filtering loop so that filters can be applied as well to inProcess executors |
||
| 175 | |||
| 176 | $succeeded = 0; |
||
| 177 | $failed = 0; |
||
| 178 | $results = []; |
||
| 179 | |||
| 180 | foreach ($callables as $instanceName => $callable) { |
||
| 181 | try { |
||
| 182 | $results[$instanceName] = call_user_func($callable); |
||
| 183 | $succeeded++; |
||
| 184 | } catch (\Throwable $t) { |
||
| 185 | $results[$instanceName] = [ |
||
| 186 | 'exitcode' => $t->getCode(), |
||
| 187 | 'stderr' => $t->getMessage(), |
||
| 188 | ]; |
||
| 189 | $failed++; |
||
| 190 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $t->getMessage() . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | if (count($processes)) { |
||
| 195 | if ($this->outputFormat === 'text') { |
||
| 196 | $this->writeln('<info>Starting parallel execution...</info>', OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 197 | } |
||
| 198 | $this->processManager->runParallel($processes, $this->maxParallelProcesses, 100, $onForkedProcessOutput); |
||
| 199 | |||
| 200 | foreach ($processes as $instanceName => $process) { |
||
| 201 | if ($process->isSuccessful()) { |
||
| 202 | /// @todo is it necessary to have rtrim here ? shall we maybe move it to the executor ? |
||
| 203 | $output = rtrim($process->getOutput()); |
||
| 204 | if (isset($outputFilters[$instanceName])) { |
||
| 205 | try { |
||
| 206 | $output = call_user_func_array($outputFilters[$instanceName], [$output, $executors[$instanceName]]); |
||
| 207 | } catch (\Throwable $t) { |
||
| 208 | $output = [ |
||
| 209 | // q: shall we add to the results the sql output ? |
||
| 210 | 'exitcode' => $t->getCode(), |
||
| 211 | 'stderr' => $t->getMessage(), |
||
| 212 | ]; |
||
| 213 | $failed++; |
||
| 214 | $succeeded--; |
||
| 215 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $t->getMessage() . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $results[$instanceName] = $output; |
||
| 219 | $succeeded++; |
||
| 220 | } else { |
||
| 221 | $err = trim($process->getErrorOutput()); |
||
| 222 | // some command-line database tools mix up stdout and stderr - we go out of our way to accommodate them... |
||
| 223 | if ($err === '') { |
||
| 224 | $err = trim($process->getOutput()); |
||
| 225 | } |
||
| 226 | $results[$instanceName] = [ |
||
| 227 | 'exitcode' => $process->getExitCode(), |
||
| 228 | 'stderr' => $err, |
||
| 229 | ]; |
||
| 230 | |||
| 231 | $failed++; |
||
| 232 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $err . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | } finally { |
||
| 238 | // make sure that we clean up temp files, as they might contain sensitive data |
||
| 239 | foreach($tempSQLFileNames as $tempSQLFileName) { |
||
| 240 | unlink($tempSQLFileName); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | uksort($results, function ($a, $b) { |
||
| 245 | $aParts = explode('_', $a, 2); |
||
| 246 | $bParts = explode('_', $b, 2); |
||
| 247 | $cmp = strcasecmp($aParts[0], $bParts[0]); |
||
| 248 | if ($cmp !== 0) { |
||
| 249 | return $cmp; |
||
| 250 | } |
||
| 251 | if (count($aParts) == 1) { |
||
| 252 | return -1; |
||
| 253 | } |
||
| 254 | if (count($bParts) == 1) { |
||
| 255 | return 1; |
||
| 256 | } |
||
| 257 | $aVersion = str_replace('_', '.', $aParts[1]); |
||
| 258 | $bVersion = str_replace('_', '.', $bParts[1]); |
||
| 259 | return version_compare($aVersion, $bVersion); |
||
| 260 | }); |
||
| 261 | |||
| 262 | return [ |
||
| 263 | 'succeeded' => $succeeded, |
||
| 264 | 'failed' => $failed, |
||
| 265 | 'data' => $results |
||
| 266 | ]; |
||
| 323 |