Test Failed
Push — master ( e042c7...883f5c )
by Hannes
02:15
created

ProcessRunner::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Runner;
6
7
use hanneskod\readmetester\Parser\CodeBlock;
8
use Symfony\Component\Process\PhpProcess;
9
10
/**
11
 * Execute code in isolation using symfony php-process
12
 */
13
class ProcessRunner implements RunnerInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $bootstrapCode;
19
20
    public function __construct(string $bootstrap = '')
21
    {
22
        $this->bootstrapCode = $bootstrap ? "require '$bootstrap';" : '';
23
    }
24
25
    public function run(CodeBlock $codeBlock): OutcomeInterface
26
    {
27
        $filename = tempnam(sys_get_temp_dir(), 'doctestphp');
28
29
        file_put_contents($filename, "<?php $codeBlock");
30
31
        $process = new PhpProcess("<?php {$this->bootstrapCode} require '$filename';");
32
        $process->run();
33
34
        unlink($filename);
35
36
        if ($errorOutput = $process->getErrorOutput()) {
37
            return new ErrorOutcome(trim($errorOutput));
38
        }
39
40
        if ($output = $process->getOutput()) {
41
            return new OutputOutcome($output);
42
        }
43
44
        return new VoidOutcome;
45
    }
46
}
47