These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | |||
4 | Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: |
||
5 | MIT License |
||
6 | Copyright (c) 2013-2016 Yahoo! Inc. All Rights Reserved. |
||
7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
||
8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
||
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
10 | |||
11 | Origin: https://github.com/zordius/lightncandy |
||
12 | */ |
||
13 | |||
14 | /** |
||
15 | * file to keep LightnCandy Exporter |
||
16 | * |
||
17 | * @package LightnCandy |
||
18 | * @author Zordius <[email protected]> |
||
19 | */ |
||
20 | |||
21 | namespace LightnCandy; |
||
22 | |||
23 | /** |
||
24 | * LightnCandy major static class |
||
25 | */ |
||
26 | class Exporter |
||
27 | { |
||
28 | /** |
||
29 | * Get PHP code string from a closure of function as string |
||
30 | * |
||
31 | * @param array<string,array|string|integer> $context current compile context |
||
32 | * @param object $closure Closure object |
||
33 | * |
||
34 | * @return string |
||
35 | * |
||
36 | * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)), function ($a) {return;} |
||
37 | * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)), function ($a) {return;} |
||
38 | */ |
||
39 | 77 | protected static function closure($context, $closure) { |
|
40 | 77 | if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) { |
|
41 | 2 | $ref = new \ReflectionMethod($matched[1], $matched[2]); |
|
42 | } else { |
||
43 | 76 | $ref = new \ReflectionFunction($closure); |
|
44 | } |
||
45 | 77 | $meta = static::getMeta($ref); |
|
46 | |||
47 | 77 | return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*\\((.+)\\}.*?\s*$/s', 'function($2}', static::replaceSafeString($context, $meta['code'])); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Export required custom helper functions |
||
52 | * |
||
53 | * @param array<string,array|string|integer> $context current compile context |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | 687 | public static function helpers($context) { |
|
58 | 687 | $ret = ''; |
|
59 | 687 | foreach ($context['helpers'] as $name => $func) { |
|
0 ignored issues
–
show
|
|||
60 | 199 | if (!isset($context['usedCount']['helpers'][$name])) { |
|
61 | 36 | continue; |
|
62 | } |
||
63 | 184 | if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) { |
|
64 | 76 | $ret .= (" '$name' => " . static::closure($context, $func) . ",\n"); |
|
65 | 76 | continue; |
|
66 | } |
||
67 | 108 | $ret .= " '$name' => '$func',\n"; |
|
68 | } |
||
69 | |||
70 | 687 | return "array($ret)"; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Replace SafeString class with alias class name |
||
75 | * |
||
76 | * @param array<string,array|string|integer> $context current compile context |
||
77 | * @param string $str the PHP code to be replaced |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 575 | protected static function replaceSafeString($context, $str) { |
|
82 | 575 | return $context['flags']['standalone'] ? str_replace($context['safestring'], $context['safestringalias'], $str) : $str; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get methods from ReflectionClass |
||
87 | * |
||
88 | * @param array<string,array|string|integer> $context current compile context |
||
89 | * @param \ReflectionClass $class instance of the ReflectionClass |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 511 | public static function getClassMethods($context, $class) { |
|
94 | 511 | $methods = array(); |
|
95 | |||
96 | 511 | foreach ($class->getMethods() as $method) { |
|
97 | 511 | $meta = static::getMeta($method); |
|
98 | 511 | $methods[$meta['name']] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", $meta['code']), $meta['code']); |
|
99 | } |
||
100 | |||
101 | 511 | return $methods; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get statics code from ReflectionClass |
||
106 | * |
||
107 | * @param \ReflectionClass $class instance of the ReflectionClass |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 334 | public static function getClassStatics($class) { |
|
112 | 334 | $ret = ''; |
|
113 | |||
114 | 334 | foreach ($class->getStaticProperties() as $name => $value) { |
|
115 | 334 | $ret .= " public static \${$name} = " . var_export($value, true) . ";\n"; |
|
116 | } |
||
117 | |||
118 | 334 | return $ret; |
|
119 | } |
||
120 | |||
121 | |||
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Get metadata from ReflectionObject |
||
127 | * |
||
128 | * @param object $refobj instance of the ReflectionObject |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 575 | public static function getMeta($refobj) { |
|
133 | 575 | $fname = $refobj->getFileName(); |
|
134 | 575 | $lines = file_get_contents($fname); |
|
135 | 575 | $file = new \SplFileObject($fname); |
|
136 | 575 | $file->seek($refobj->getStartLine() - 2); |
|
137 | 575 | $spos = $file->ftell(); |
|
138 | 575 | $file->seek($refobj->getEndLine() - 1); |
|
139 | 575 | $epos = $file->ftell(); |
|
140 | 575 | unset($file); |
|
141 | return array( |
||
142 | 575 | 'name' => $refobj->getName(), |
|
143 | 575 | 'code' => substr($lines, $spos, $epos - $spos) |
|
144 | ); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Export SafeString class as string |
||
149 | * |
||
150 | * @param array<string,array|string|integer> $context current compile context |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 334 | public static function safestring($context) { |
|
155 | 334 | $class = new \ReflectionClass($context['safestring']); |
|
156 | |||
157 | return array_reduce(static::getClassMethods($context, $class), function ($in, $cur) { |
||
158 | 334 | return $in . $cur[2]; |
|
159 | 334 | }, "if (!class_exists(\"" . addslashes($context['safestringalias']) . "\")) {\nclass {$context['safestringalias']} {\n" . static::getClassStatics($class)) . "}\n}\n"; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Export required standalone Runtime methods |
||
164 | * |
||
165 | * @param array<string,array|string|integer> $context current compile context |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | 511 | public static function runtime($context) { |
|
170 | 511 | $class = new \ReflectionClass($context['runtime']); |
|
171 | 511 | $ret = ''; |
|
172 | 511 | $methods = static::getClassMethods($context, $class); |
|
173 | |||
174 | 511 | $exports = array_keys($context['usedCount']['runtime']); |
|
175 | |||
176 | 511 | while (true) { |
|
177 | if (array_sum(array_map(function ($name) use (&$exports, $methods) { |
||
178 | 469 | $n = 0; |
|
179 | 469 | foreach ($methods[$name][1] as $child => $count) { |
|
180 | 468 | if (!in_array($child, $exports)) { |
|
181 | 468 | $exports[] = $child; |
|
182 | 468 | $n++; |
|
183 | } |
||
184 | } |
||
185 | 469 | return $n; |
|
186 | 511 | }, $exports)) == 0) { |
|
187 | 511 | break; |
|
188 | } |
||
189 | } |
||
190 | |||
191 | 511 | foreach ($exports as $export) { |
|
192 | 469 | $ret .= ($methods[$export][0] . "\n"); |
|
193 | } |
||
194 | |||
195 | 511 | return $ret; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Export Runtime constants |
||
200 | * |
||
201 | * @param array<string,array|string|integer> $context current compile context |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 687 | public static function constants($context) { |
|
206 | 687 | if ($context['flags']['standalone'] == 0) { |
|
207 | 665 | return 'array()'; |
|
208 | } |
||
209 | |||
210 | 511 | $class = new \ReflectionClass($context['runtime']); |
|
211 | 511 | $constants = $class->getConstants(); |
|
212 | 511 | $ret = " array(\n"; |
|
213 | 511 | foreach($constants as $name => $value) { |
|
214 | 511 | $ret .= " '$name' => ". (is_string($value) ? "'$value'" : $value ) . ",\n"; |
|
215 | } |
||
216 | 511 | $ret .= " )"; |
|
217 | 511 | return $ret; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Scan for required standalone functions |
||
222 | * |
||
223 | * @param array<string,array|string|integer> $context current compile context |
||
224 | * @param string $code patched PHP code string of the method |
||
225 | * @param string $ocode original PHP code string of the method |
||
226 | * |
||
227 | * @return array<string|array> list of converted code and children array |
||
228 | */ |
||
229 | 511 | protected static function scanDependency($context, $code, $ocode) { |
|
230 | 511 | $child = array(); |
|
231 | |||
232 | 511 | $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) { |
|
233 | 511 | if (!isset($child[$matches[1]])) { |
|
234 | 511 | $child[$matches[1]] = 0; |
|
235 | } |
||
236 | 511 | $child[$matches[1]]++; |
|
237 | |||
238 | 511 | return "{$context['funcprefix']}{$matches[1]}("; |
|
239 | 511 | }, $code); |
|
240 | |||
241 | // replace the constants |
||
242 | 511 | $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code); |
|
243 | |||
244 | // compress space |
||
245 | 511 | $code = preg_replace('/ /', ' ', $code); |
|
246 | |||
247 | 511 | return array(static::replaceSafeString($context, $code), $child, $ocode); |
|
248 | } |
||
249 | } |
||
250 | |||
251 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.