Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

ProcessRunner::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 4
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
ccs 4
cts 7
cp 0.5714
crap 3.7085
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Utils;
6
7
use PhpSpellcheck\Exception\ProcessFailedException;
8
use Symfony\Component\Process\Exception\ExceptionInterface;
9
use Symfony\Component\Process\Process;
10
11
class ProcessRunner
12
{
13 14
    /**
14
     * @param int|float|null $timeout The timeout in seconds
15 14
     */
16
    public static function run(Process $process, $timeout = null, callable $callback = null, array $env = []): Process
17 14
    {
18
        if (method_exists($process, 'inheritEnvironmentVariables')) {
19
            // Symfony 3.2+
20
            $process->inheritEnvironmentVariables(true);
21
        } else {
22 14
            // Symfony < 3.2
23
            $process->setEnv(['LANG' => getenv('LANG')]);
24
        }
25 14
        $process->setTimeout($timeout);
26
27
        try {
28
            $process->mustRun($callback, $env);
29
        } catch (ExceptionInterface $e) {
30 14
            throw new ProcessFailedException($process, $e);
31
        }
32
33
        return $process;
34
    }
35
}
36