Total Complexity | 127 |
Total Lines | 776 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ExecTask 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 ExecTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class ExecTask extends Task |
||
48 | { |
||
49 | use LogLevelAware; |
||
50 | |||
51 | public const INVALID = PHP_INT_MAX; |
||
52 | |||
53 | /** |
||
54 | * Command to be executed. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $realCommand; |
||
59 | |||
60 | /** |
||
61 | * Commandline managing object. |
||
62 | * |
||
63 | * @var Commandline |
||
64 | */ |
||
65 | protected $commandline; |
||
66 | |||
67 | /** |
||
68 | * Working directory. |
||
69 | * |
||
70 | * @var File |
||
71 | */ |
||
72 | protected $dir; |
||
73 | |||
74 | protected $currdir; |
||
75 | |||
76 | /** |
||
77 | * Operating system. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $os; |
||
82 | |||
83 | /** |
||
84 | * Whether to escape shell command using escapeshellcmd(). |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $escape = false; |
||
89 | |||
90 | /** |
||
91 | * Where to direct output. |
||
92 | * |
||
93 | * @var File |
||
94 | */ |
||
95 | protected $output; |
||
96 | |||
97 | /** |
||
98 | * Whether to use PHP's passthru() function instead of exec(). |
||
99 | * |
||
100 | * @var bool |
||
101 | */ |
||
102 | protected $passthru = false; |
||
103 | |||
104 | /** |
||
105 | * Whether to log returned output as MSG_INFO instead of MSG_VERBOSE. |
||
106 | * |
||
107 | * @var bool |
||
108 | */ |
||
109 | protected $logOutput = false; |
||
110 | |||
111 | /** |
||
112 | * Where to direct error output. |
||
113 | * |
||
114 | * @var File |
||
115 | */ |
||
116 | protected $error; |
||
117 | |||
118 | /** |
||
119 | * If spawn is set then [unix] programs will redirect stdout and add '&'. |
||
120 | * |
||
121 | * @var bool |
||
122 | */ |
||
123 | protected $spawn = false; |
||
124 | |||
125 | /** |
||
126 | * Property name to set with return value from exec call. |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $returnProperty; |
||
131 | |||
132 | /** |
||
133 | * Property name to set with output value from exec call. |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | protected $outputProperty; |
||
138 | |||
139 | /** |
||
140 | * Whether to check the return code. |
||
141 | * |
||
142 | * @var bool |
||
143 | */ |
||
144 | protected $checkreturn = false; |
||
145 | |||
146 | private $exitValue = self::INVALID; |
||
147 | |||
148 | private $osFamily; |
||
149 | private $executable; |
||
150 | private $resolveExecutable = false; |
||
151 | private $searchPath = false; |
||
152 | private $env; |
||
153 | |||
154 | /** |
||
155 | * @throws BuildException |
||
156 | */ |
||
157 | public function __construct() |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Main method: wraps execute() command. |
||
166 | * |
||
167 | * @throws BuildException |
||
168 | */ |
||
169 | public function main() |
||
170 | { |
||
171 | if (!$this->isValidOs()) { |
||
172 | return null; |
||
173 | } |
||
174 | |||
175 | // Suggest Task instead of executable |
||
176 | if ($this->executable && ($hint = $this->findHint($this->executable))) { |
||
177 | $this->log($hint, Project::MSG_VERBOSE); |
||
178 | } |
||
179 | |||
180 | try { |
||
181 | $this->commandline->setExecutable($this->resolveExecutable($this->executable, $this->searchPath)); |
||
182 | } catch (IOException | \InvalidArgumentException $e) { |
||
183 | throw new BuildException($e); |
||
184 | } |
||
185 | |||
186 | $this->prepare(); |
||
187 | $this->buildCommand(); |
||
188 | [$return, $output] = $this->executeCommand(); |
||
189 | $this->cleanup($return, $output); |
||
190 | |||
191 | return $return; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param int $exitValue |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function isFailure($exitValue = null) |
||
200 | { |
||
201 | if (null === $exitValue) { |
||
202 | $exitValue = $this->getExitValue(); |
||
203 | } |
||
204 | |||
205 | return 0 !== $exitValue; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Query the exit value of the process. |
||
210 | * |
||
211 | * @return int the exit value or self::INVALID if no exit value has |
||
212 | * been received |
||
213 | */ |
||
214 | public function getExitValue(): int |
||
215 | { |
||
216 | return $this->exitValue; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * The executable to use. |
||
221 | * |
||
222 | * @param bool|string $value String or string-compatible (e.g. w/ __toString()). |
||
223 | */ |
||
224 | public function setExecutable($value): void |
||
225 | { |
||
226 | if (is_bool($value)) { |
||
227 | $value = true === $value ? 'true' : 'false'; |
||
228 | } |
||
229 | $this->executable = $value; |
||
230 | $this->commandline->setExecutable($value); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Whether to use escapeshellcmd() to escape command. |
||
235 | * |
||
236 | * @param bool $escape If the command shall be escaped or not |
||
237 | */ |
||
238 | public function setEscape(bool $escape): void |
||
239 | { |
||
240 | $this->escape = $escape; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Specify the working directory for executing this command. |
||
245 | * |
||
246 | * @param File $dir Working directory |
||
247 | */ |
||
248 | public function setDir(File $dir): void |
||
249 | { |
||
250 | $this->dir = $dir; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Specify OS (or multiple OS) that must match in order to execute this command. |
||
255 | * |
||
256 | * @param string $os Operating system string (e.g. "Linux") |
||
257 | */ |
||
258 | public function setOs($os): void |
||
259 | { |
||
260 | $this->os = (string) $os; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * List of operating systems on which the command may be executed. |
||
265 | */ |
||
266 | public function getOs(): string |
||
267 | { |
||
268 | return $this->os; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Restrict this execution to a single OS Family. |
||
273 | * |
||
274 | * @param string $osFamily the family to restrict to |
||
275 | */ |
||
276 | public function setOsFamily($osFamily): void |
||
277 | { |
||
278 | $this->osFamily = strtolower($osFamily); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Restrict this execution to a single OS Family. |
||
283 | */ |
||
284 | public function getOsFamily() |
||
285 | { |
||
286 | return $this->osFamily; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * File to which output should be written. |
||
291 | * |
||
292 | * @param File $f Output log file |
||
293 | */ |
||
294 | public function setOutput(File $f): void |
||
295 | { |
||
296 | $this->output = $f; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * File to which error output should be written. |
||
301 | * |
||
302 | * @param File $f Error log file |
||
303 | */ |
||
304 | public function setError(File $f): void |
||
305 | { |
||
306 | $this->error = $f; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Whether to use PHP's passthru() function instead of exec(). |
||
311 | * |
||
312 | * @param bool $passthru If passthru shall be used |
||
313 | */ |
||
314 | public function setPassthru(bool $passthru): void |
||
315 | { |
||
316 | $this->passthru = $passthru; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Whether to log returned output as MSG_INFO instead of MSG_VERBOSE. |
||
321 | * |
||
322 | * @param bool $logOutput If output shall be logged visibly |
||
323 | */ |
||
324 | public function setLogoutput($logOutput): void |
||
325 | { |
||
326 | $this->logOutput = $logOutput; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Whether to suppress all output and run in the background. |
||
331 | * |
||
332 | * @param bool $spawn If the command is to be run in the background |
||
333 | */ |
||
334 | public function setSpawn($spawn): void |
||
335 | { |
||
336 | $this->spawn = $spawn; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Whether to check the return code. |
||
341 | * |
||
342 | * @param bool $checkreturn If the return code shall be checked |
||
343 | */ |
||
344 | public function setCheckreturn($checkreturn): void |
||
345 | { |
||
346 | $this->checkreturn = $checkreturn; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * The name of property to set to return value from exec() call. |
||
351 | * |
||
352 | * @param string $prop Property name |
||
353 | */ |
||
354 | public function setReturnProperty($prop): void |
||
355 | { |
||
356 | $this->returnProperty = $prop; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * The name of property to set to output value from exec() call. |
||
361 | * |
||
362 | * @param string $prop Property name |
||
363 | */ |
||
364 | public function setOutputProperty($prop): void |
||
365 | { |
||
366 | $this->outputProperty = $prop; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Add an environment variable to the launched process. |
||
371 | * |
||
372 | * @param EnvVariable $var new environment variable |
||
373 | */ |
||
374 | public function addEnv(EnvVariable $var) |
||
375 | { |
||
376 | $this->env->addVariable($var); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Creates a nested <arg> tag. |
||
381 | * |
||
382 | * @return CommandlineArgument Argument object |
||
383 | */ |
||
384 | public function createArg() |
||
385 | { |
||
386 | return $this->commandline->createArgument(); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Set whether to attempt to resolve the executable to a file. |
||
391 | * |
||
392 | * @param bool $resolveExecutable if true, attempt to resolve the |
||
393 | * path of the executable |
||
394 | */ |
||
395 | public function setResolveExecutable($resolveExecutable): void |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Set whether to search nested, then |
||
402 | * system PATH environment variables for the executable. |
||
403 | * |
||
404 | * @param bool $searchPath if true, search PATHs |
||
405 | */ |
||
406 | public function setSearchPath($searchPath): void |
||
407 | { |
||
408 | $this->searchPath = $searchPath; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Indicates whether to attempt to resolve the executable to a |
||
413 | * file. |
||
414 | * |
||
415 | * @return bool the resolveExecutable flag |
||
416 | */ |
||
417 | public function getResolveExecutable(): bool |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Prepares the command building and execution, i.e. |
||
424 | * changes to the specified directory. |
||
425 | * |
||
426 | * @throws BuildException |
||
427 | */ |
||
428 | protected function prepare() |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Builds the full command to execute and stores it in $command. |
||
465 | * |
||
466 | * @throws BuildException |
||
467 | * |
||
468 | * @uses $command |
||
469 | */ |
||
470 | protected function buildCommand() |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Executes the command and returns return code and output. |
||
527 | * |
||
528 | * @throws BuildException |
||
529 | * |
||
530 | * @return array array(return code, array with output) |
||
531 | */ |
||
532 | protected function executeCommand() |
||
533 | { |
||
534 | $cmdl = $this->realCommand; |
||
535 | |||
536 | $this->log('Executing command: ' . $cmdl, $this->logLevel); |
||
537 | |||
538 | $output = []; |
||
539 | $return = null; |
||
540 | |||
541 | if ($this->passthru) { |
||
542 | passthru($cmdl, $return); |
||
543 | } else { |
||
544 | exec($cmdl, $output, $return); |
||
545 | } |
||
546 | |||
547 | return [$return, $output]; |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Runs all tasks after command execution: |
||
552 | * - change working directory back |
||
553 | * - log output |
||
554 | * - verify return value. |
||
555 | * |
||
556 | * @param int $return Return code |
||
557 | * @param array $output Array with command output |
||
558 | * |
||
559 | * @throws BuildException |
||
560 | */ |
||
561 | protected function cleanup($return, $output): void |
||
562 | { |
||
563 | if (null !== $this->dir) { |
||
564 | @chdir($this->currdir); |
||
565 | } |
||
566 | |||
567 | $outloglevel = $this->logOutput ? Project::MSG_INFO : Project::MSG_VERBOSE; |
||
568 | foreach ($output as $line) { |
||
569 | $this->log($line, $outloglevel); |
||
570 | } |
||
571 | |||
572 | $this->maybeSetReturnPropertyValue($return); |
||
573 | |||
574 | if ($this->outputProperty) { |
||
575 | $this->project->setProperty( |
||
576 | $this->outputProperty, |
||
577 | implode("\n", $output) |
||
578 | ); |
||
579 | } |
||
580 | |||
581 | $this->setExitValue($return); |
||
582 | |||
583 | if (0 !== $return) { |
||
584 | if ($this->checkreturn) { |
||
585 | throw new BuildException($this->getTaskType() . ' returned: ' . $return, $this->getLocation()); |
||
586 | } |
||
587 | $this->log('Result: ' . $return, Project::MSG_ERR); |
||
588 | } |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Set the exit value. |
||
593 | * |
||
594 | * @param int $value exit value of the process |
||
595 | */ |
||
596 | protected function setExitValue($value): void |
||
599 | } |
||
600 | |||
601 | protected function maybeSetReturnPropertyValue(int $return) |
||
602 | { |
||
603 | if ($this->returnProperty) { |
||
604 | $this->getProject()->setNewProperty($this->returnProperty, $return); |
||
605 | } |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * Is this the OS the user wanted? |
||
610 | * |
||
611 | * @return bool. |
||
612 | * <ul> |
||
613 | * <li> |
||
614 | * <li><code>true</code> if the os and osfamily attributes are null.</li> |
||
615 | * <li><code>true</code> if osfamily is set, and the os family and must match |
||
616 | * that of the current OS, according to the logic of |
||
617 | * {@link Os#isOs(String, String, String, String)}, and the result of the |
||
618 | * <code>os</code> attribute must also evaluate true. |
||
619 | * </li> |
||
620 | * <li> |
||
621 | * <code>true</code> if os is set, and the system.property os.name |
||
622 | * is found in the os attribute,</li> |
||
623 | * <li><code>false</code> otherwise.</li> |
||
624 | * </ul> |
||
625 | */ |
||
626 | protected function isValidOs(): bool |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * The method attempts to figure out where the executable is so that we can feed |
||
653 | * the full path. We first try basedir, then the exec dir, and then |
||
654 | * fallback to the straight executable name (i.e. on the path). |
||
655 | * |
||
656 | * @param string $exec the name of the executable |
||
657 | * @param bool $mustSearchPath if true, the executable will be looked up in |
||
658 | * the PATH environment and the absolute path |
||
659 | * is returned |
||
660 | * |
||
661 | * @throws BuildException |
||
662 | * @throws IOException |
||
663 | * |
||
664 | * @return string the executable as a full path if it can be determined |
||
665 | */ |
||
666 | protected function resolveExecutable($exec, $mustSearchPath): ?string |
||
711 | } |
||
712 | |||
713 | private function isPath($line) |
||
716 | } |
||
717 | |||
718 | private function getPath($value) |
||
719 | { |
||
720 | if (is_string($value)) { |
||
721 | return StringHelper::substring($value, strlen('PATH=')); |
||
722 | } |
||
723 | |||
724 | if (is_array($value)) { |
||
725 | $p = $value['PATH']; |
||
726 | |||
727 | return $p ?? $value['Path']; |
||
728 | } |
||
729 | |||
730 | throw new InvalidArgumentException('$value should be of type array or string.'); |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * Give a Task as an alternative to executable |
||
735 | */ |
||
736 | public function findHint(string $executable): ?string |
||
823 | } |
||
824 | } |
||
825 |