Conditions | 18 |
Paths | 1918 |
Total Lines | 124 |
Code Lines | 78 |
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 |
||
89 | protected function executeSqlAction($instanceList, $actionName, $getSqlActionCallable, $timed = false, $onForkedProcessOutput = null) |
||
90 | { |
||
91 | $processes = []; |
||
92 | $callables = []; |
||
93 | $outputFilters = []; |
||
94 | $tempSQLFileNames = []; |
||
95 | $executors = []; |
||
96 | |||
97 | try { |
||
98 | |||
99 | foreach ($instanceList as $instanceName => $dbConnectionSpec) { |
||
100 | |||
101 | $schemaManager = new DatabaseSchemaManager($dbConnectionSpec); |
||
102 | |||
103 | /** @var CommandAction|FileAction $sqlAction */ |
||
104 | $sqlAction = call_user_func_array($getSqlActionCallable, [$schemaManager, $instanceName]); |
||
105 | |||
106 | if ($sqlAction instanceof CommandAction) { |
||
107 | $filename = null; |
||
108 | $sql = $sqlAction->getCommand(); |
||
109 | } else if ($sqlAction instanceof FileAction) { |
||
110 | $filename = $sqlAction->getFilename(); |
||
111 | $sql = null; |
||
112 | } else { |
||
113 | // this is a coding error, not a sql execution error |
||
114 | throw new \Exception("Unsupported action type: " . get_class($sqlAction)); |
||
115 | } |
||
116 | $filterCallable = $sqlAction->getResultsFilterCallable(); |
||
117 | |||
118 | if ($filename === null && $sql === null) { |
||
119 | // no sql to execute as forked process - we run the 'filter' functions in a separate loop |
||
120 | $callables[$instanceName] = $filterCallable; |
||
121 | } else { |
||
122 | $outputFilters[$instanceName] = $filterCallable; |
||
123 | |||
124 | $executor = $this->executorFactory->createForkedExecutor($dbConnectionSpec, 'NativeClient', $timed); |
||
125 | $executors[$instanceName] = $executor; |
||
126 | |||
127 | if ($filename === null) { |
||
128 | if (!$sqlAction->isSingleStatement()) { |
||
129 | $tempSQLFileName = tempnam(sys_get_temp_dir(), 'db3v4l_') . '.sql'; |
||
130 | file_put_contents($tempSQLFileName, $sql); |
||
131 | $tempSQLFileNames[] = $tempSQLFileName; |
||
132 | |||
133 | $process = $executor->getExecuteFileProcess($tempSQLFileName); |
||
134 | } else { |
||
135 | $process = $executor->getExecuteStatementProcess($sql); |
||
136 | } |
||
137 | } else { |
||
138 | $process = $executor->getExecuteFileProcess($filename); |
||
139 | } |
||
140 | |||
141 | if ($this->outputFormat === 'text') { |
||
142 | $this->writeln('Command line: ' . $process->getCommandLine(), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
143 | } |
||
144 | |||
145 | $process->setTimeout($this->processTimeout); |
||
146 | |||
147 | $processes[$instanceName] = $process; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | $succeeded = 0; |
||
152 | $failed = 0; |
||
153 | $results = []; |
||
154 | |||
155 | foreach ($callables as $instanceName => $callable) { |
||
156 | try { |
||
157 | $results[$instanceName] = call_user_func($callable); |
||
158 | $succeeded++; |
||
159 | } catch (\Throwable $t) { |
||
160 | $failed++; |
||
161 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $t->getMessage() . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | if (count($processes)) { |
||
166 | if ($this->outputFormat === 'text') { |
||
167 | $this->writeln('<info>Starting parallel execution...</info>', OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
168 | } |
||
169 | $this->processManager->runParallel($processes, $this->maxParallelProcesses, 100, $onForkedProcessOutput); |
||
170 | |||
171 | foreach ($processes as $instanceName => $process) { |
||
172 | if ($process->isSuccessful()) { |
||
173 | /// @todo is it necessary to have rtrim here ? shall we maybe move it to the executor ? |
||
174 | $output = rtrim($process->getOutput()); |
||
175 | if (isset($outputFilters[$instanceName])) { |
||
176 | try { |
||
177 | $output = call_user_func_array($outputFilters[$instanceName], [$output, $executors[$instanceName]]); |
||
178 | } catch (\Throwable $t) { |
||
179 | /// @todo shall we reset $result to null or not? |
||
180 | //$result = null; |
||
181 | $failed++; |
||
182 | $succeeded--; |
||
183 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $t->getMessage() . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
184 | } |
||
185 | } |
||
186 | $results[$instanceName] = $output; |
||
187 | $succeeded++; |
||
188 | } else { |
||
189 | $results[$instanceName] = [ |
||
190 | 'stderr' => trim($process->getErrorOutput()), |
||
191 | 'exitcode' => $process->getExitCode() |
||
192 | ]; |
||
193 | $failed++; |
||
194 | $this->writeErrorln("\n<error>$actionName in instance '$instanceName' failed! Reason: " . $process->getErrorOutput() . "</error>\n", OutputInterface::VERBOSITY_NORMAL); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | } finally { |
||
200 | // make sure that we clean up temp files, as they might contain sensitive data |
||
201 | foreach($tempSQLFileNames as $tempSQLFileName) { |
||
202 | unlink($tempSQLFileName); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /// @todo implement proper sorting based on vendor name + version |
||
207 | ksort($results); |
||
208 | |||
209 | return [ |
||
210 | 'succeeded' => $succeeded, |
||
211 | 'failed' => $failed, |
||
212 | 'data' => $results |
||
213 | ]; |
||
257 |