Test Failed
Push — master ( e9b74b...fb97fd )
by Hannes
02:09
created

EvalRunner::run()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 42
nop 1
dl 0
loc 40
rs 5.3846
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
    /**
15
     * @return OutcomeInterface[]
16
     */
17
    public function run(CodeBlock $codeBlock): array
18
    {
19
        $outcomes = [];
20
21
        ob_start();
22
23
        try {
24
            $returnValue = null;
25
26
            try {
27
                $lastErrorBefore = error_get_last();
28
                $returnValue = @eval($codeBlock);
29
                $lastErrorAfter = error_get_last();
30
                if ($lastErrorBefore != $lastErrorAfter) {
31
                    $outcomes[] = new ErrorOutcome("{$lastErrorAfter['type']}: {$lastErrorAfter['message']}");
32
                }
33
            } catch (\Error $e) {
34
                $outcomes[] = new ErrorOutcome((string)$e);
35
            }
36
37
            if ($returnValue) {
38
                $outcomes[] = new ReturnOutcome(
39
                    is_scalar($returnValue) ? @(string)$returnValue : '',
40
                    gettype($returnValue),
41
                    is_object($returnValue) ? get_class($returnValue) : ''
42
                );
43
            }
44
        } catch (\Exception $e) {
45
            $outcomes[] = new ExceptionOutcome(
46
                get_class($e),
47
                $e->getMessage(),
48
                $e->getCode()
49
            );
50
        }
51
52
        if ($output = ob_get_clean()) {
53
            $outcomes[] = new OutputOutcome($output);
54
        }
55
56
        return $outcomes;
57
    }
58
}
59