Test Failed
Push — master ( 397588...614ce5 )
by Hannes
02:13
created

EvalRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 29 6
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 = eval($codeBlock);
1 ignored issue
show
introduced by
The use of eval() is discouraged.
Loading history...
25
26
            if ($returnValue) {
27
                $outcomes[] = new ReturnOutcome(
28
                    is_scalar($returnValue) ? @(string)$returnValue : '',
29
                    gettype($returnValue),
30
                    is_object($returnValue) ? get_class($returnValue) : ''
31
                );
32
            }
33
        } catch (\Exception $e) {
34
            $outcomes[] = new ExceptionOutcome(
35
                get_class($e),
36
                $e->getMessage(),
37
                $e->getCode()
38
            );
39
        }
40
41
        if ($output = ob_get_clean()) {
42
            $outcomes[] = new OutputOutcome($output);
43
        }
44
45
        return $outcomes;
46
    }
47
}
48