This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | $grammarFileToName = [ |
||
4 | __DIR__ . '/php5.y' => 'Php5', |
||
5 | __DIR__ . '/php7.y' => 'Php7', |
||
6 | ]; |
||
7 | |||
8 | $tokensFile = __DIR__ . '/tokens.y'; |
||
9 | $tokensTemplate = __DIR__ . '/tokens.template'; |
||
10 | $skeletonFile = __DIR__ . '/parser.template'; |
||
11 | $tmpGrammarFile = __DIR__ . '/tmp_parser.phpy'; |
||
12 | $tmpResultFile = __DIR__ . '/tmp_parser.php'; |
||
13 | $resultDir = __DIR__ . '/../lib/PhpParser/Parser'; |
||
14 | $tokensResultsFile = $resultDir . '/Tokens.php'; |
||
15 | |||
16 | // check for kmyacc.exe binary in this directory, otherwise fall back to global name |
||
17 | $kmyacc = __DIR__ . '/kmyacc.exe'; |
||
18 | if (!file_exists($kmyacc)) { |
||
19 | $kmyacc = 'kmyacc'; |
||
20 | } |
||
21 | |||
22 | $options = array_flip($argv); |
||
23 | $optionDebug = isset($options['--debug']); |
||
24 | $optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']); |
||
25 | |||
26 | /////////////////////////////// |
||
27 | /// Utility regex constants /// |
||
28 | /////////////////////////////// |
||
29 | |||
30 | const LIB = '(?(DEFINE) |
||
31 | (?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\') |
||
32 | (?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+") |
||
33 | (?<string>(?&singleQuotedString)|(?&doubleQuotedString)) |
||
34 | (?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/) |
||
35 | (?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+}) |
||
36 | )'; |
||
37 | |||
38 | const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?¶ms)\][^[\]]*+)*+)\]'; |
||
39 | const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)'; |
||
40 | |||
41 | /////////////////// |
||
42 | /// Main script /// |
||
43 | /////////////////// |
||
44 | |||
45 | $tokens = file_get_contents($tokensFile); |
||
46 | |||
47 | foreach ($grammarFileToName as $grammarFile => $name) { |
||
48 | echo "Building temporary $name grammar file.\n"; |
||
49 | |||
50 | $grammarCode = file_get_contents($grammarFile); |
||
51 | $grammarCode = str_replace('%tokens', $tokens, $grammarCode); |
||
52 | |||
53 | $grammarCode = resolveNodes($grammarCode); |
||
54 | $grammarCode = resolveMacros($grammarCode); |
||
55 | $grammarCode = resolveStackAccess($grammarCode); |
||
56 | |||
57 | file_put_contents($tmpGrammarFile, $grammarCode); |
||
58 | |||
59 | $additionalArgs = $optionDebug ? '-t -v' : ''; |
||
60 | |||
61 | echo "Building $name parser.\n"; |
||
62 | $output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile -p $name $tmpGrammarFile 2>&1")); |
||
63 | echo "Output: \"$output\"\n"; |
||
64 | |||
65 | $resultCode = file_get_contents($tmpResultFile); |
||
66 | $resultCode = removeTrailingWhitespace($resultCode); |
||
67 | |||
68 | ensureDirExists($resultDir); |
||
69 | file_put_contents("$resultDir/$name.php", $resultCode); |
||
70 | unlink($tmpResultFile); |
||
71 | |||
72 | echo "Building token definition.\n"; |
||
73 | $output = trim(shell_exec("$kmyacc -l -m $tokensTemplate $tmpGrammarFile 2>&1")); |
||
74 | assert($output === ''); |
||
75 | rename($tmpResultFile, $tokensResultsFile); |
||
76 | |||
77 | if (!$optionKeepTmpGrammar) { |
||
78 | unlink($tmpGrammarFile); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /////////////////////////////// |
||
83 | /// Preprocessing functions /// |
||
84 | /////////////////////////////// |
||
85 | |||
86 | function resolveNodes($code) { |
||
87 | return preg_replace_callback( |
||
88 | '~(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~', |
||
89 | function($matches) { |
||
90 | // recurse |
||
91 | $matches['params'] = resolveNodes($matches['params']); |
||
92 | |||
93 | $params = magicSplit( |
||
94 | '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,', |
||
95 | $matches['params'] |
||
96 | ); |
||
97 | |||
98 | $paramCode = ''; |
||
99 | foreach ($params as $param) { |
||
100 | $paramCode .= $param . ', '; |
||
101 | } |
||
102 | |||
103 | return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())'; |
||
104 | }, |
||
105 | $code |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | function resolveMacros($code) { |
||
110 | return preg_replace_callback( |
||
111 | '~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~', |
||
112 | function($matches) { |
||
113 | // recurse |
||
114 | $matches['args'] = resolveMacros($matches['args']); |
||
115 | |||
116 | $name = $matches['name']; |
||
117 | $args = magicSplit( |
||
118 | '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,', |
||
119 | $matches['args'] |
||
120 | ); |
||
121 | |||
122 | if ('attributes' == $name) { |
||
123 | assertArgs(0, $args, $name); |
||
124 | return '$this->startAttributeStack[#1] + $this->endAttributes'; |
||
125 | } |
||
126 | |||
127 | if ('init' == $name) { |
||
128 | return '$$ = array(' . implode(', ', $args) . ')'; |
||
129 | } |
||
130 | |||
131 | View Code Duplication | if ('push' == $name) { |
|
0 ignored issues
–
show
|
|||
132 | assertArgs(2, $args, $name); |
||
133 | |||
134 | return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0]; |
||
135 | } |
||
136 | |||
137 | if ('pushNormalizing' == $name) { |
||
138 | assertArgs(2, $args, $name); |
||
139 | |||
140 | return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }' |
||
141 | . ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }'; |
||
142 | } |
||
143 | |||
144 | View Code Duplication | if ('toArray' == $name) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
145 | assertArgs(1, $args, $name); |
||
146 | |||
147 | return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')'; |
||
148 | } |
||
149 | |||
150 | if ('parseVar' == $name) { |
||
151 | assertArgs(1, $args, $name); |
||
152 | |||
153 | return 'substr(' . $args[0] . ', 1)'; |
||
154 | } |
||
155 | |||
156 | View Code Duplication | if ('parseEncapsed' == $name) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
157 | assertArgs(3, $args, $name); |
||
158 | |||
159 | return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {' |
||
160 | . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }'; |
||
161 | } |
||
162 | |||
163 | View Code Duplication | if ('parseEncapsedDoc' == $name) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
164 | assertArgs(2, $args, $name); |
||
165 | |||
166 | return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {' |
||
167 | . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, null, ' . $args[1] . '); } }' |
||
168 | . ' $s->value = preg_replace(\'~(\r\n|\n|\r)\z~\', \'\', $s->value);' |
||
169 | . ' if (\'\' === $s->value) array_pop(' . $args[0] . ');'; |
||
170 | } |
||
171 | |||
172 | return $matches[0]; |
||
173 | }, |
||
174 | $code |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | function assertArgs($num, $args, $name) { |
||
179 | if ($num != count($args)) { |
||
180 | die('Wrong argument count for ' . $name . '().'); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | function resolveStackAccess($code) { |
||
185 | $code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code); |
||
186 | $code = preg_replace('/#(\d+)/', '$$1', $code); |
||
187 | return $code; |
||
188 | } |
||
189 | |||
190 | function removeTrailingWhitespace($code) { |
||
191 | $lines = explode("\n", $code); |
||
192 | $lines = array_map('rtrim', $lines); |
||
193 | return implode("\n", $lines); |
||
194 | } |
||
195 | |||
196 | function ensureDirExists($dir) { |
||
197 | if (!is_dir($dir)) { |
||
198 | mkdir($dir, 0777, true); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | ////////////////////////////// |
||
203 | /// Regex helper functions /// |
||
204 | ////////////////////////////// |
||
205 | |||
206 | function regex($regex) { |
||
0 ignored issues
–
show
The function
regex() has been defined more than once; this definition is ignored, only the first definition in grammar/analyze.php (L23-25) is considered.
This check looks for functions that have already been defined in other files. Some Codebases, like WordPress, make a practice of defining functions multiple times. This
may lead to problems with the detection of function parameters and types. If you really
need to do this, you can mark the duplicate definition with the /**
* @ignore
*/
function getUser() {
}
function getUser($id, $realm) {
}
See also the PhpDoc documentation for @ignore. ![]() |
|||
207 | return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~'; |
||
208 | } |
||
209 | |||
210 | function magicSplit($regex, $string) { |
||
0 ignored issues
–
show
The function
magicSplit() has been defined more than once; this definition is ignored, only the first definition in grammar/analyze.php (L27-35) is considered.
This check looks for functions that have already been defined in other files. Some Codebases, like WordPress, make a practice of defining functions multiple times. This
may lead to problems with the detection of function parameters and types. If you really
need to do this, you can mark the duplicate definition with the /**
* @ignore
*/
function getUser() {
}
function getUser($id, $realm) {
}
See also the PhpDoc documentation for @ignore. ![]() |
|||
211 | $pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string); |
||
212 | |||
213 | foreach ($pieces as &$piece) { |
||
214 | $piece = trim($piece); |
||
215 | } |
||
216 | |||
217 | if ($pieces === ['']) { |
||
218 | return []; |
||
219 | } |
||
220 | |||
221 | return $pieces; |
||
222 | } |
||
223 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.