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