Completed
Push — master ( abec3a...7e1653 )
by Hannes
02:00
created

CodeBlock::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace hanneskod\readmetester;
4
5
/**
6
 * Wrapps an executable block of code
7
 */
8
class CodeBlock
9
{
10
    /**
11
     * @var string The contained code
12
     */
13
    private $code;
14
15
    /**
16
     * @param string $code
17
     */
18
    public function __construct($code)
19
    {
20
        $this->code = $code;
21
    }
22
23
    /**
24
     * Grab contained code
25
     *
26
     * @return string
27
     */
28
    public function getCode()
29
    {
30
        return $this->code;
31
    }
32
33
    /**
34
     * Execute code block
35
     *
36
     * @return Result The result of the executed code
37
     */
38
    public function execute()
39
    {
40
        $returnValue = '';
0 ignored issues
show
Unused Code introduced by
$returnValue is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
        $exception = null;
42
43
        ob_start();
44
45
        try {
46
            $returnValue = eval($this->code);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
47
        } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { $exception = $e; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
            $exception = $e;
49
        }
50
51
        return new Result($returnValue, ob_get_clean(), $exception);
52
    }
53
}
54