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

EvalRunner::run()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 7
nop 1
dl 0
loc 22
rs 8.9197
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
9
/**
10
 * Execute code using eval()
11
 */
12
class EvalRunner implements RunnerInterface
13
{
14
    public function __construct(string $bootstrap = '')
15
    {
16
        if ($bootstrap) {
17
            require_once $bootstrap;
18
        }
19
    }
20
21
    public function run(CodeBlock $codeBlock): OutcomeInterface
22
    {
23
        ob_start();
24
25
        try {
26
            $lastErrorBefore = error_get_last();
27
            eval($codeBlock);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
28
            $lastErrorAfter = error_get_last();
29
            if ($lastErrorBefore != $lastErrorAfter) {
30
                ob_end_clean();
31
                return new ErrorOutcome("{$lastErrorAfter['type']}: {$lastErrorAfter['message']}");
32
            }
33
        } catch (\Throwable $e) {
34
            ob_end_clean();
35
            return new ErrorOutcome((string)$e);
36
        }
37
38
        if ($output = ob_get_clean()) {
39
            return new OutputOutcome($output);
40
        }
41
42
        return new VoidOutcome;
43
    }
44
}
45