Code::getCodeExecutable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 40
ccs 4
cts 4
cp 1
crap 1
rs 9.344
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool;
13
14
class Code
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $code = [];
20
21
    /**
22
     * @param  string $statement
23
     * @return Code
24
     */
25 12
    public static function fromString($statement)
26
    {
27 12
        $code = new static();
28 12
        $code->addStatement($statement);
29
30 12
        return $code;
31
    }
32
33
    /**
34
     * @param string $statement
35
     */
36 31
    public function addStatement($statement)
37
    {
38 31
        $this->code[] = $statement;
39 31
    }
40
41
    /**
42
     * @param string[] $statements
43
     */
44 1
    public function addStatements($statements)
45
    {
46 1
        $this->code = array_merge($this->code, $statements);
47 1
    }
48
49
    /**
50
     * @return string
51
     */
52 31
    public function getCode()
53
    {
54 31
        return implode(PHP_EOL, $this->code);
55
    }
56
57
    /**
58
     * @param  string $file
59
     * @return null
60
     */
61 22
    public function writeTo($file)
62
    {
63 22
        $code = '<?php' . PHP_EOL . $this->getCodeExecutable();
64 22
        $chksum = md5($code);
65
66 22
        if (false === @file_put_contents($file, $code)) {
67 3
            throw new \RuntimeException("Could not write to `{$file}`: No such file or directory.");
68
        }
69
70 19
        if ($chksum !== md5_file($file)) {
71 1
            throw new \RuntimeException("Aborted due to security constraints: After writing to `{$file}` the contents were not the same.");
72
        }
73 18
    }
74
75
    /**
76
     * @return string
77
     */
78 25
    public function getCodeExecutable()
79
    {
80
        $template =<<<'EOF'
81 25
if (extension_loaded('newrelic')) {
82
    newrelic_ignore_transaction();
83
}
84
85
$errors = array();
86
87
$cachetool_error_handler_%s = function($errno, $errstr, $errfile, $errline) use (&$errors) {
88
    $errors[] = array(
89
        'no' => $errno,
90
        'str' => $errstr,
91
    );
92
};
93
94
$cachetool_exec_%s = function() use (&$errors) {
95
    try {
96
        %s
97
    } catch (\Exception $e) {
98
        $errors[] = array(
99
            'no' => $e->getCode(),
100
            'str' => $e->getMessage(),
101
        );
102
    }
103
};
104
105
set_error_handler($cachetool_error_handler_%s);
106
107
$result = $cachetool_exec_%s();
108
109
echo serialize(array(
110
    'result' => $result,
111
    'errors' => $errors
112
));
113
EOF;
114
115 25
        $uniq = uniqid();
116
117 25
        return sprintf($template, $uniq, $uniq, $this->getCode(), $uniq, $uniq);
118
    }
119
}
120