Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

EvaluatingGeneratorStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 44
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\GeneratorStrategy;
6
7
use Zend\Code\Generator\ClassGenerator;
8
use function assert;
9
use function file_put_contents;
10
use function ini_get;
11
use function is_string;
12
use function sys_get_temp_dir;
13
use function tempnam;
14
use function unlink;
15
16
/**
17
 * Generator strategy that produces the code and evaluates it at runtime
18
 */
19
class EvaluatingGeneratorStrategy implements GeneratorStrategyInterface
20
{
21
    /** @var bool flag indicating whether {@see eval} can be used */
22
    private bool $canEval = true;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
23
24
    /**
25
     * Constructor
26
     */
27
    public function __construct()
28 1
    {
29
        // @codeCoverageIgnoreStart
30
        $this->canEval = ! ini_get('suhosin.executor.disable_eval');
31
        // @codeCoverageIgnoreEnd
32
    }
33 1
34
    /**
35
     * Evaluates the generated code before returning it
36
     *
37
     * {@inheritDoc}
38
     */
39
    public function generate(ClassGenerator $classGenerator) : string
40 1
    {
41
        $code = $classGenerator->generate();
42 1
43
        // @codeCoverageIgnoreStart
44
        if (! $this->canEval) {
45
            $fileName = tempnam(sys_get_temp_dir(), 'EvaluatingGeneratorStrategy.php.tmp.');
46
47
            assert(is_string($fileName));
48
49
            file_put_contents($fileName, "<?php\n" . $code);
50
            /* @noinspection PhpIncludeInspection */
51
            require $fileName;
52
            unlink($fileName);
53
54
            return $code;
55
        }
56
        // @codeCoverageIgnoreEnd
57
58
        eval($code);
59 1
60
        return $code;
61 1
    }
62
}
63