Issues (1426)

app/src/Service/ProcessManager.php (26 issues)

1
<?php
2
0 ignored issues
show
Missing file doc comment
Loading history...
3
namespace Db3v4l\Service;
4
5
use Symfony\Component\Process\Process;
0 ignored issues
show
The type Symfony\Component\Process\Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * This ProcessManager is a simple wrapper to enable parallel processing using Symfony Process component.
9
 * Original source code from https://github.com/jagandecapri/symfony-parallel-process/blob/master/src/ProcessManager.php (MIT lic.)
10
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @author tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
11
class ProcessManager
12
{
13
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
14
     * @param Process[] $processes
0 ignored issues
show
Missing parameter comment
Loading history...
15
     * @param int $maxParallel
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 7 spaces after parameter type; 1 found
Loading history...
16
     * @param int $poll microseconds
0 ignored issues
show
Expected 7 spaces after parameter type; 1 found
Loading history...
Expected 8 spaces after parameter name; 1 found
Loading history...
17
     * @param Callable $callback takes 4 args: $type, $buffer, $processIndex, $process
0 ignored issues
show
Expected 2 spaces after parameter type; 1 found
Loading history...
Expected 4 spaces after parameter name; 1 found
Loading history...
18
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
19
    public function runParallel(array $processes, $maxParallel, $poll = 1000, $callback = null)
20
    {
21
        $this->validateProcesses($processes);
22
        // do not modify the object pointers in the argument, copy to local working variable
23
        $processesQueue = $processes;
24
        // fix maxParallel to be max the number of processes or positive
25
        $maxParallel = min(abs($maxParallel), count($processesQueue));
26
        // get the first stack of processes to start at the same time
27
        /** @var Process[] $currentProcesses */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
28
        $currentProcesses = array_splice($processesQueue, 0, $maxParallel);
29
        // start the initial stack of processes
30
        foreach ($currentProcesses as $idx => $process) {
31
            $process->start(function ($type, $buffer) use ($callback, $idx, $process) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
32
                if ($callback) {
33
                    $callback($type, $buffer, $idx, $process);
34
                }
35
            });
0 ignored issues
show
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
36
        }
37
        do {
38
            // wait for the given time
39
            usleep($poll);
40
            // remove all finished processes from the stack
41
            foreach ($currentProcesses as $index => $process) {
42
                if (!$process->isRunning()) {
43
                    unset($currentProcesses[$index]);
44
                    // directly add and start new process after the previous finished
45
                    if (count($processesQueue) > 0) {
46
                        $nextProcess = array_shift($processesQueue);
47
                        $nextProcess->start(function ($type, $buffer) use ($callback, $nextProcess) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
48
                            if ($callback) {
49
                                $callback($type, $buffer, $nextProcess);
50
                            }
51
                        });
0 ignored issues
show
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
52
                        $currentProcesses[] = $nextProcess;
53
                    }
54
                }
55
            }
56
            // continue loop while there are processes being executed or waiting for execution
57
        } while (count($processesQueue) > 0 || count($currentProcesses) > 0);
58
    }
59
60
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
61
     * @param Process[] $processes
0 ignored issues
show
Missing parameter comment
Loading history...
62
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
63
    protected function validateProcesses(array $processes)
64
    {
65
        if (empty($processes)) {
66
            throw new \InvalidArgumentException('Can not run in parallel 0 commands');
67
        }
68
        foreach ($processes as $process) {
69
            if (!($process instanceof Process)) {
70
                throw new \InvalidArgumentException('Process in array need to be instance of Symfony Process');
71
            }
72
        }
73
    }
74
}
75