EvalExecution::patch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kambo\Testing\ClassOpener\ClassManipulation\Patcher;
4
5
use PhpParser\PrettyPrinter\Standard;
6
use Kambo\Testing\ClassOpener\ClassManipulation\Patcher;
7
8
/**
9
 * Patch class with its modified version.
10
 *
11
 * @author  Bohuslav Simek <[email protected]>
12
 * @license MIT
13
 */
14
class EvalExecution implements Patcher
15
{
16
    /**
17
     * Replace global class definition with class defined in the provided nodes.
18
     *
19
     * @param Node[] $statmentNodes Array of statements
20
     *
21
     * @return void
22
     */
23 3
    public function patch(array $statmentNodes)
24
    {
25 3
        $prettyPrinter = new Standard;
26 3
        $newClassCode  = $prettyPrinter->prettyPrintFile($statmentNodes).PHP_EOL;
27
28
        // Force PHP interpreter to use modified version of the class, by evaluating.
29 3
        eval('?>' . $newClassCode);
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...
30 3
    }
31
}
32