Completed
Push — master ( 94304e...b7ad07 )
by Hannes
02:10
created

CodeBlock::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
/**
8
 * Wrapps an executable block of code
9
 */
10
class CodeBlock
11
{
12
    /**
13
     * @var string The contained code
14
     */
15
    private $code;
16
17
    public function __construct(string $code)
18
    {
19
        $this->code = $code;
20
    }
21
22
    /**
23
     * Prepend this code block with the contents of $codeBlock
24
     */
25
    public function prepend(CodeBlock $codeBlock)
26
    {
27
        $this->code = sprintf(
28
            "%s\n%s%s\n%s",
29
            'ob_start();',
30
            $codeBlock->getCode(),
31
            'ob_end_clean();',
32
            $this->code
33
        );
34
    }
35
36
    /**
37
     * Grab contained code
38
     */
39
    public function getCode(): string
40
    {
41
        return $this->code;
42
    }
43
44
    /**
45
     * Execute code block
46
     *
47
     * @return Result The result of the executed code
48
     */
49
    public function execute(): Result
50
    {
51
        $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...
52
        $exception = null;
53
54
        ob_start();
55
56
        try {
57
            $returnValue = eval($this->code);
58
        } 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...
59
            $exception = $e;
60
        }
61
62
        return new Result($returnValue, ob_get_clean(), $exception);
63
    }
64
}
65