Total Complexity | 298 |
Total Lines | 1090 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like PhpRender often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhpRender, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PhpRender extends PrettyPrinterAbstract |
||
23 | { |
||
24 | // Special nodes |
||
25 | |||
26 | protected function pParam(Node\Param $node) { |
||
33 | } |
||
34 | |||
35 | protected function pArg(Node\Arg $node) { |
||
36 | $code = ($node->name ? $node->name->toString() . ': ' : '') |
||
37 | . ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') |
||
38 | . $this->p($node->value); |
||
39 | return $code; |
||
40 | } |
||
41 | |||
42 | protected function pConst(Node\Const_ $node) { |
||
43 | return $node->name . ' = ' . $this->p($node->value); |
||
44 | } |
||
45 | |||
46 | protected function pNullableType(Node\NullableType $node) { |
||
47 | return '?' . $this->p($node->type); |
||
48 | } |
||
49 | |||
50 | protected function pUnionType(Node\UnionType $node) { |
||
51 | return $this->pImplode($node->types, '|'); |
||
52 | } |
||
53 | |||
54 | protected function pIdentifier(Node\Identifier $node) { |
||
55 | return $node->name; |
||
56 | } |
||
57 | |||
58 | protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node) { |
||
59 | return '$' . $node->name; |
||
60 | } |
||
61 | |||
62 | // Names |
||
63 | |||
64 | protected function pName(Name $node) { |
||
65 | return implode('\\', $node->parts); |
||
66 | } |
||
67 | |||
68 | protected function pName_FullyQualified(Name\FullyQualified $node) { |
||
69 | return '\\' . implode('\\', $node->parts); |
||
70 | } |
||
71 | |||
72 | protected function pName_Relative(Name\Relative $node) { |
||
73 | return 'namespace\\' . implode('\\', $node->parts); |
||
74 | } |
||
75 | |||
76 | // Magic Constants |
||
77 | |||
78 | protected function pScalar_MagicConst_Class(MagicConst\Class_ $node) { |
||
|
|||
79 | return '__CLASS__'; |
||
80 | } |
||
81 | |||
82 | protected function pScalar_MagicConst_Dir(MagicConst\Dir $node) { |
||
83 | return '__DIR__'; |
||
84 | } |
||
85 | |||
86 | protected function pScalar_MagicConst_File(MagicConst\File $node) { |
||
87 | return '__FILE__'; |
||
88 | } |
||
89 | |||
90 | protected function pScalar_MagicConst_Function(MagicConst\Function_ $node) { |
||
91 | return '__FUNCTION__'; |
||
92 | } |
||
93 | |||
94 | protected function pScalar_MagicConst_Line(MagicConst\Line $node) { |
||
95 | return '__LINE__'; |
||
96 | } |
||
97 | |||
98 | protected function pScalar_MagicConst_Method(MagicConst\Method $node) { |
||
99 | return '__METHOD__'; |
||
100 | } |
||
101 | |||
102 | protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) { |
||
103 | return '__NAMESPACE__'; |
||
104 | } |
||
105 | |||
106 | protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) { |
||
107 | return '__TRAIT__'; |
||
108 | } |
||
109 | |||
110 | // Scalars |
||
111 | |||
112 | protected function pScalar_String(Scalar\String_ $node) { |
||
113 | $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED); |
||
114 | switch ($kind) { |
||
115 | case Scalar\String_::KIND_NOWDOC: |
||
116 | $label = $node->getAttribute('docLabel'); |
||
117 | if ($label && !$this->containsEndLabel($node->value, $label)) { |
||
118 | if ($node->value === '') { |
||
119 | return "<<<'$label'\n$label" . $this->docStringEndToken; |
||
120 | } |
||
121 | |||
122 | return "<<<'$label'\n$node->value\n$label" |
||
123 | . $this->docStringEndToken; |
||
124 | } |
||
125 | /* break missing intentionally */ |
||
126 | case Scalar\String_::KIND_SINGLE_QUOTED: |
||
127 | return $this->pSingleQuotedString($node->value); |
||
128 | case Scalar\String_::KIND_HEREDOC: |
||
129 | $label = $node->getAttribute('docLabel'); |
||
130 | if ($label && !$this->containsEndLabel($node->value, $label)) { |
||
131 | if ($node->value === '') { |
||
132 | return "<<<$label\n$label" . $this->docStringEndToken; |
||
133 | } |
||
134 | |||
135 | $escaped = $this->escapeString($node->value, null); |
||
136 | return "<<<$label\n" . $escaped . "\n$label" |
||
137 | . $this->docStringEndToken; |
||
138 | } |
||
139 | /* break missing intentionally */ |
||
140 | case Scalar\String_::KIND_DOUBLE_QUOTED: |
||
141 | return '"' . $this->escapeString($node->value, '"') . '"'; |
||
142 | } |
||
143 | throw new \Exception('Invalid string kind'); |
||
144 | } |
||
145 | |||
146 | protected function pScalar_Encapsed(Scalar\Encapsed $node) { |
||
147 | if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) { |
||
148 | $label = $node->getAttribute('docLabel'); |
||
149 | if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) { |
||
150 | if (count($node->parts) === 1 |
||
151 | && $node->parts[0] instanceof Scalar\EncapsedStringPart |
||
152 | && $node->parts[0]->value === '' |
||
153 | ) { |
||
154 | return "<<<$label\n$label" . $this->docStringEndToken; |
||
155 | } |
||
156 | |||
157 | return "<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label" |
||
158 | . $this->docStringEndToken; |
||
159 | } |
||
160 | } |
||
161 | return '"' . $this->pEncapsList($node->parts, '"') . '"'; |
||
162 | } |
||
163 | |||
164 | protected function pScalar_LNumber(Scalar\LNumber $node) { |
||
165 | if ($node->value === -\PHP_INT_MAX-1) { |
||
166 | // PHP_INT_MIN cannot be represented as a literal, |
||
167 | // because the sign is not part of the literal |
||
168 | return '(-' . \PHP_INT_MAX . '-1)'; |
||
169 | } |
||
170 | |||
171 | $kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC); |
||
172 | if (Scalar\LNumber::KIND_DEC === $kind) { |
||
173 | return (string) $node->value; |
||
174 | } |
||
175 | |||
176 | if ($node->value < 0) { |
||
177 | $sign = '-'; |
||
178 | $str = (string) -$node->value; |
||
179 | } else { |
||
180 | $sign = ''; |
||
181 | $str = (string) $node->value; |
||
182 | } |
||
183 | switch ($kind) { |
||
184 | case Scalar\LNumber::KIND_BIN: |
||
185 | return $sign . '0b' . base_convert($str, 10, 2); |
||
186 | case Scalar\LNumber::KIND_OCT: |
||
187 | return $sign . '0' . base_convert($str, 10, 8); |
||
188 | case Scalar\LNumber::KIND_HEX: |
||
189 | return $sign . '0x' . base_convert($str, 10, 16); |
||
190 | } |
||
191 | throw new \Exception('Invalid number kind'); |
||
192 | } |
||
193 | |||
194 | protected function pScalar_DNumber(Scalar\DNumber $node) { |
||
195 | if (!is_finite($node->value)) { |
||
196 | if ($node->value === \INF) { |
||
197 | return '\INF'; |
||
198 | } elseif ($node->value === -\INF) { |
||
199 | return '-\INF'; |
||
200 | } else { |
||
201 | return '\NAN'; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | // Try to find a short full-precision representation |
||
206 | $stringValue = sprintf('%.16G', $node->value); |
||
207 | if ($node->value !== (double) $stringValue) { |
||
208 | $stringValue = sprintf('%.17G', $node->value); |
||
209 | } |
||
210 | |||
211 | // %G is locale dependent and there exists no locale-independent alternative. We don't want |
||
212 | // mess with switching locales here, so let's assume that a comma is the only non-standard |
||
213 | // decimal separator we may encounter... |
||
214 | $stringValue = str_replace(',', '.', $stringValue); |
||
215 | |||
216 | // ensure that number is really printed as float |
||
217 | return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue; |
||
218 | } |
||
219 | |||
220 | protected function pScalar_EncapsedStringPart(Scalar\EncapsedStringPart $node) { |
||
221 | throw new \LogicException('Cannot directly print EncapsedStringPart'); |
||
222 | } |
||
223 | |||
224 | // Assignments |
||
225 | |||
226 | protected function pExpr_Assign(Expr\Assign $node) { |
||
227 | return $this->pInfixOp(Expr\Assign::class, $node->var, ' = ', $node->expr); |
||
228 | } |
||
229 | |||
230 | protected function pExpr_AssignRef(Expr\AssignRef $node) { |
||
231 | return $this->pInfixOp(Expr\AssignRef::class, $node->var, ' =& ', $node->expr); |
||
232 | } |
||
233 | |||
234 | protected function pExpr_AssignOp_Plus(AssignOp\Plus $node) { |
||
235 | return $this->pInfixOp(AssignOp\Plus::class, $node->var, ' += ', $node->expr); |
||
236 | } |
||
237 | |||
238 | protected function pExpr_AssignOp_Minus(AssignOp\Minus $node) { |
||
239 | return $this->pInfixOp(AssignOp\Minus::class, $node->var, ' -= ', $node->expr); |
||
240 | } |
||
241 | |||
242 | protected function pExpr_AssignOp_Mul(AssignOp\Mul $node) { |
||
243 | return $this->pInfixOp(AssignOp\Mul::class, $node->var, ' *= ', $node->expr); |
||
244 | } |
||
245 | |||
246 | protected function pExpr_AssignOp_Div(AssignOp\Div $node) { |
||
247 | return $this->pInfixOp(AssignOp\Div::class, $node->var, ' /= ', $node->expr); |
||
248 | } |
||
249 | |||
250 | protected function pExpr_AssignOp_Concat(AssignOp\Concat $node) { |
||
251 | return $this->pInfixOp(AssignOp\Concat::class, $node->var, ' .= ', $node->expr); |
||
252 | } |
||
253 | |||
254 | protected function pExpr_AssignOp_Mod(AssignOp\Mod $node) { |
||
255 | return $this->pInfixOp(AssignOp\Mod::class, $node->var, ' %= ', $node->expr); |
||
256 | } |
||
257 | |||
258 | protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) { |
||
260 | } |
||
261 | |||
262 | protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) { |
||
263 | return $this->pInfixOp(AssignOp\BitwiseOr::class, $node->var, ' |= ', $node->expr); |
||
264 | } |
||
265 | |||
266 | protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) { |
||
267 | return $this->pInfixOp(AssignOp\BitwiseXor::class, $node->var, ' ^= ', $node->expr); |
||
268 | } |
||
269 | |||
270 | protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) { |
||
271 | return $this->pInfixOp(AssignOp\ShiftLeft::class, $node->var, ' <<= ', $node->expr); |
||
272 | } |
||
273 | |||
274 | protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) { |
||
275 | return $this->pInfixOp(AssignOp\ShiftRight::class, $node->var, ' >>= ', $node->expr); |
||
276 | } |
||
277 | |||
278 | protected function pExpr_AssignOp_Pow(AssignOp\Pow $node) { |
||
279 | return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr); |
||
280 | } |
||
281 | |||
282 | protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node) { |
||
283 | return $this->pInfixOp(AssignOp\Coalesce::class, $node->var, ' ??= ', $node->expr); |
||
284 | } |
||
285 | |||
286 | // Binary expressions |
||
287 | |||
288 | protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) { |
||
289 | return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right); |
||
290 | } |
||
291 | |||
292 | protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) { |
||
293 | return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right); |
||
294 | } |
||
295 | |||
296 | protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) { |
||
297 | return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right); |
||
298 | } |
||
299 | |||
300 | protected function pExpr_BinaryOp_Div(BinaryOp\Div $node) { |
||
301 | return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right); |
||
302 | } |
||
303 | |||
304 | protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) { |
||
305 | return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right); |
||
306 | } |
||
307 | |||
308 | protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) { |
||
309 | return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right); |
||
310 | } |
||
311 | |||
312 | protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) { |
||
313 | return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right); |
||
314 | } |
||
315 | |||
316 | protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) { |
||
317 | return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right); |
||
318 | } |
||
319 | |||
320 | protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) { |
||
321 | return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right); |
||
322 | } |
||
323 | |||
324 | protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) { |
||
325 | return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right); |
||
326 | } |
||
327 | |||
328 | protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) { |
||
329 | return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right); |
||
330 | } |
||
331 | |||
332 | protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) { |
||
333 | return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right); |
||
334 | } |
||
335 | |||
336 | protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) { |
||
337 | return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right); |
||
338 | } |
||
339 | |||
340 | protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) { |
||
341 | return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right); |
||
342 | } |
||
343 | |||
344 | protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) { |
||
345 | return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right); |
||
346 | } |
||
347 | |||
348 | protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) { |
||
349 | return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right); |
||
350 | } |
||
351 | |||
352 | protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) { |
||
353 | return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right); |
||
354 | } |
||
355 | |||
356 | protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) { |
||
357 | return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right); |
||
358 | } |
||
359 | |||
360 | protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) { |
||
361 | return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right); |
||
362 | } |
||
363 | |||
364 | protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) { |
||
365 | return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right); |
||
366 | } |
||
367 | |||
368 | protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) { |
||
369 | return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right); |
||
370 | } |
||
371 | |||
372 | protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) { |
||
373 | return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right); |
||
374 | } |
||
375 | |||
376 | protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) { |
||
377 | return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right); |
||
378 | } |
||
379 | |||
380 | protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) { |
||
381 | return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right); |
||
382 | } |
||
383 | |||
384 | protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) { |
||
385 | return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right); |
||
386 | } |
||
387 | |||
388 | protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) { |
||
389 | return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right); |
||
390 | } |
||
391 | |||
392 | protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) { |
||
393 | return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right); |
||
394 | } |
||
395 | |||
396 | protected function pExpr_Instanceof(Expr\Instanceof_ $node) { |
||
397 | [$precedence, $associativity] = $this->precedenceMap[Expr\Instanceof_::class]; |
||
398 | return $this->pPrec($node->expr, $precedence, $associativity, -1) |
||
399 | . ' instanceof ' |
||
400 | . $this->pNewVariable($node->class); |
||
401 | } |
||
402 | |||
403 | // Unary expressions |
||
404 | |||
405 | protected function pExpr_BooleanNot(Expr\BooleanNot $node) { |
||
406 | return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr); |
||
407 | } |
||
408 | |||
409 | protected function pExpr_BitwiseNot(Expr\BitwiseNot $node) { |
||
410 | return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr); |
||
411 | } |
||
412 | |||
413 | protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) { |
||
414 | if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) { |
||
415 | // Enforce -(-$expr) instead of --$expr |
||
416 | return '-(' . $this->p($node->expr) . ')'; |
||
417 | } |
||
418 | return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr); |
||
419 | } |
||
420 | |||
421 | protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) { |
||
422 | if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) { |
||
423 | // Enforce +(+$expr) instead of ++$expr |
||
424 | return '+(' . $this->p($node->expr) . ')'; |
||
425 | } |
||
426 | return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr); |
||
427 | } |
||
428 | |||
429 | protected function pExpr_PreInc(Expr\PreInc $node) { |
||
430 | return $this->pPrefixOp(Expr\PreInc::class, '++', $node->var); |
||
431 | } |
||
432 | |||
433 | protected function pExpr_PreDec(Expr\PreDec $node) { |
||
434 | return $this->pPrefixOp(Expr\PreDec::class, '--', $node->var); |
||
435 | } |
||
436 | |||
437 | protected function pExpr_PostInc(Expr\PostInc $node) { |
||
438 | return $this->pPostfixOp(Expr\PostInc::class, $node->var, '++'); |
||
439 | } |
||
440 | |||
441 | protected function pExpr_PostDec(Expr\PostDec $node) { |
||
442 | return $this->pPostfixOp(Expr\PostDec::class, $node->var, '--'); |
||
443 | } |
||
444 | |||
445 | protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) { |
||
446 | return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr); |
||
447 | } |
||
448 | |||
449 | protected function pExpr_YieldFrom(Expr\YieldFrom $node) { |
||
450 | return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr); |
||
451 | } |
||
452 | |||
453 | protected function pExpr_Print(Expr\Print_ $node) { |
||
454 | return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr); |
||
455 | } |
||
456 | |||
457 | // Casts |
||
458 | |||
459 | protected function pExpr_Cast_Int(Cast\Int_ $node) { |
||
460 | return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr); |
||
461 | } |
||
462 | |||
463 | protected function pExpr_Cast_Double(Cast\Double $node) { |
||
464 | $kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE); |
||
465 | if ($kind === Cast\Double::KIND_DOUBLE) { |
||
466 | $cast = '(double)'; |
||
467 | } elseif ($kind === Cast\Double::KIND_FLOAT) { |
||
468 | $cast = '(float)'; |
||
469 | } elseif ($kind === Cast\Double::KIND_REAL) { |
||
470 | $cast = '(real)'; |
||
471 | } |
||
472 | return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr); |
||
473 | } |
||
474 | |||
475 | protected function pExpr_Cast_String(Cast\String_ $node) { |
||
476 | return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr); |
||
477 | } |
||
478 | |||
479 | protected function pExpr_Cast_Array(Cast\Array_ $node) { |
||
480 | return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr); |
||
481 | } |
||
482 | |||
483 | protected function pExpr_Cast_Object(Cast\Object_ $node) { |
||
484 | return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr); |
||
485 | } |
||
486 | |||
487 | protected function pExpr_Cast_Bool(Cast\Bool_ $node) { |
||
488 | return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr); |
||
489 | } |
||
490 | |||
491 | protected function pExpr_Cast_Unset(Cast\Unset_ $node) { |
||
492 | return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr); |
||
493 | } |
||
494 | |||
495 | // Function calls and similar constructs |
||
496 | |||
497 | protected function pExpr_FuncCall(Expr\FuncCall $node) { |
||
498 | return $this->pCallLhs($node->name) |
||
499 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
500 | } |
||
501 | |||
502 | protected function pExpr_MethodCall(Expr\MethodCall $node) { |
||
503 | $code = $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name) |
||
504 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
505 | $token = '\'__' . md5($code) . '__\''; |
||
506 | $this->options['builder']->closures[$token] = $code; |
||
507 | return $token; |
||
508 | } |
||
509 | |||
510 | protected function pExpr_NullsafeMethodCall(Expr\NullsafeMethodCall $node) { |
||
511 | return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name) |
||
512 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
513 | } |
||
514 | |||
515 | protected function pExpr_StaticCall(Expr\StaticCall $node) { |
||
516 | $code = $this->pDereferenceLhs($node->class) . '::' |
||
517 | . ($node->name instanceof Expr |
||
518 | ? ($node->name instanceof Expr\Variable |
||
519 | ? $this->p($node->name) |
||
520 | : '{' . $this->p($node->name) . '}') |
||
521 | : $node->name) |
||
522 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
523 | $token = '\'__' . md5($code) . '__\''; |
||
524 | $this->options['builder']->closures[$token] = $code; |
||
525 | return $token; |
||
526 | } |
||
527 | |||
528 | protected function pExpr_Empty(Expr\Empty_ $node) { |
||
529 | return 'empty(' . $this->p($node->expr) . ')'; |
||
530 | } |
||
531 | |||
532 | protected function pExpr_Isset(Expr\Isset_ $node) { |
||
533 | return 'isset(' . $this->pCommaSeparated($node->vars) . ')'; |
||
534 | } |
||
535 | |||
536 | protected function pExpr_Eval(Expr\Eval_ $node) { |
||
537 | $code = 'eval(' . $this->p($node->expr) . ')'; |
||
538 | $token = '\'__' . md5($code) . '__\''; |
||
539 | $this->options['builder']->closures[$token] = $code; |
||
540 | return $token; |
||
541 | } |
||
542 | |||
543 | protected function pExpr_Include(Expr\Include_ $node) { |
||
544 | static $map = [ |
||
545 | Expr\Include_::TYPE_INCLUDE => 'include', |
||
546 | Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once', |
||
547 | Expr\Include_::TYPE_REQUIRE => 'require', |
||
548 | Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once', |
||
549 | ]; |
||
550 | |||
551 | $code = $map[$node->type] . ' ' . $this->p($node->expr); |
||
552 | $token = '\'__' . md5($code) . '__\''; |
||
553 | $this->options['builder']->closures[$token] = $code; |
||
554 | |||
555 | return $token; |
||
556 | } |
||
557 | |||
558 | protected function pExpr_List(Expr\List_ $node) { |
||
559 | return 'list(' . $this->pCommaSeparated($node->items) . ')'; |
||
560 | } |
||
561 | |||
562 | // Other |
||
563 | |||
564 | protected function pExpr_Error(Expr\Error $node) { |
||
565 | throw new \LogicException('Cannot pretty-print AST with Error nodes'); |
||
566 | } |
||
567 | |||
568 | protected function pExpr_Variable(Expr\Variable $node) { |
||
569 | if ($node->name instanceof Expr) { |
||
570 | return '${' . $this->p($node->name) . '}'; |
||
571 | } else { |
||
572 | return '$' . $node->name; |
||
573 | } |
||
574 | } |
||
575 | |||
576 | protected function pExpr_Array(Expr\Array_ $node) { |
||
577 | $syntax = $node->getAttribute('kind', |
||
578 | $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG); |
||
579 | if ($syntax === Expr\Array_::KIND_SHORT) { |
||
580 | return '[' . $this->pMaybeMultiline($node->items, true) . ']'; |
||
581 | } else { |
||
582 | return 'array(' . $this->pMaybeMultiline($node->items, true) . ')'; |
||
583 | } |
||
584 | } |
||
585 | |||
586 | protected function pExpr_ArrayItem(Expr\ArrayItem $node) { |
||
587 | return (null !== $node->key ? $this->p($node->key) . ' => ' : '') |
||
588 | . ($node->byRef ? '&' : '') |
||
589 | . ($node->unpack ? '...' : '') |
||
590 | . $this->p($node->value); |
||
591 | } |
||
592 | |||
593 | protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) { |
||
594 | return $this->pDereferenceLhs($node->var) |
||
595 | . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']'; |
||
596 | } |
||
597 | |||
598 | protected function pExpr_ConstFetch(Expr\ConstFetch $node) { |
||
599 | return $this->p($node->name); |
||
600 | } |
||
601 | |||
602 | protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) { |
||
603 | return $this->pDereferenceLhs($node->class) . '::' . $this->p($node->name); |
||
604 | } |
||
605 | |||
606 | protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) { |
||
607 | return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name); |
||
608 | } |
||
609 | |||
610 | protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node) { |
||
611 | return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name); |
||
612 | } |
||
613 | |||
614 | protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) { |
||
615 | return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name); |
||
616 | } |
||
617 | |||
618 | protected function pExpr_ShellExec(Expr\ShellExec $node) { |
||
619 | return '`' . $this->pEncapsList($node->parts, '`') . '`'; |
||
620 | } |
||
621 | |||
622 | protected function pExpr_Closure(Expr\Closure $node) { |
||
623 | $code = ($node->static ? 'static ' : '') |
||
624 | . 'function ' . ($node->byRef ? '&' : '') |
||
625 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
626 | . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '') |
||
627 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
628 | . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
629 | $token = '\'__' . md5($code) . '__\''; |
||
630 | $this->options['builder']->closures[$token] = $code; |
||
631 | return $token; |
||
632 | } |
||
633 | |||
634 | protected function pExpr_Match(Expr\Match_ $node) { |
||
635 | return 'match (' . $this->p($node->cond) . ') {' |
||
636 | . $this->pCommaSeparatedMultiline($node->arms, true) |
||
637 | . $this->nl |
||
638 | . '}'; |
||
639 | } |
||
640 | |||
641 | protected function pMatchArm(Node\MatchArm $node) { |
||
642 | return ($node->conds ? $this->pCommaSeparated($node->conds) : 'default') |
||
643 | . ' => ' . $this->p($node->body); |
||
644 | } |
||
645 | |||
646 | protected function pExpr_ArrowFunction(Expr\ArrowFunction $node) { |
||
647 | $code = ($node->static ? 'static ' : '') |
||
648 | . 'fn' . ($node->byRef ? '&' : '') |
||
649 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
650 | . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '') |
||
651 | . ' => ' |
||
652 | . $this->p($node->expr); |
||
653 | $token = '\'__' . md5($code) . '__\''; |
||
654 | $this->options['builder']->closures[$token] = $code; |
||
655 | return $token; |
||
656 | } |
||
657 | |||
658 | protected function pExpr_ClosureUse(Expr\ClosureUse $node) { |
||
659 | return ($node->byRef ? '&' : '') . $this->p($node->var); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Return new class expression |
||
664 | * @param Expr\New_ $node |
||
665 | * |
||
666 | * @return string |
||
667 | */ |
||
668 | protected function pExpr_New(Expr\New_ $node) { |
||
669 | if ($node->class instanceof Stmt\Class_) { |
||
670 | $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; |
||
671 | $code = 'new ' . $this->pClassCommon($node->class, $args); |
||
672 | $token = '\'__' . md5($code) . '__\''; |
||
673 | $this->options['builder']->closures[$token] = $code; |
||
674 | return $token; |
||
675 | } |
||
676 | $code = 'new ' . $this->pNewVariable($node->class) |
||
677 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
678 | if($this->pNewVariable($node->class) == 'ReverseBlockMerge'){ |
||
679 | return $code; |
||
680 | } |
||
681 | $token = '\'__' . md5($code) . '__\''; |
||
682 | $this->options['builder']->closures[$token] = $code; |
||
683 | return $token; |
||
684 | } |
||
685 | |||
686 | protected function pExpr_Clone(Expr\Clone_ $node) { |
||
687 | return 'clone ' . $this->p($node->expr); |
||
688 | } |
||
689 | |||
690 | protected function pExpr_Ternary(Expr\Ternary $node) { |
||
691 | // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator. |
||
692 | // this is okay because the part between ? and : never needs parentheses. |
||
693 | return $this->pInfixOp(Expr\Ternary::class, |
||
694 | $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else |
||
695 | ); |
||
696 | } |
||
697 | |||
698 | protected function pExpr_Exit(Expr\Exit_ $node) { |
||
699 | $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE); |
||
700 | return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die') |
||
701 | . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : ''); |
||
702 | } |
||
703 | |||
704 | protected function pExpr_Throw(Expr\Throw_ $node) { |
||
705 | return 'throw ' . $this->p($node->expr); |
||
706 | } |
||
707 | |||
708 | protected function pExpr_Yield(Expr\Yield_ $node) { |
||
709 | if ($node->value === null) { |
||
710 | return 'yield'; |
||
711 | } else { |
||
712 | // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary |
||
713 | return '(yield ' |
||
714 | . ($node->key !== null ? $this->p($node->key) . ' => ' : '') |
||
715 | . $this->p($node->value) |
||
716 | . ')'; |
||
717 | } |
||
718 | } |
||
719 | |||
720 | // Declarations |
||
721 | |||
722 | protected function pStmt_Namespace(Stmt\Namespace_ $node) { |
||
723 | if ($this->canUseSemicolonNamespaces) { |
||
724 | return 'namespace ' . $this->p($node->name) . ';' |
||
725 | . $this->nl . $this->pStmts($node->stmts, false); |
||
726 | } else { |
||
727 | return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '') |
||
728 | . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
729 | } |
||
730 | } |
||
731 | |||
732 | protected function pStmt_Use(Stmt\Use_ $node) { |
||
733 | return 'use ' . $this->pUseType($node->type) |
||
734 | . $this->pCommaSeparated($node->uses) . ';'; |
||
735 | } |
||
736 | |||
737 | protected function pStmt_GroupUse(Stmt\GroupUse $node) { |
||
738 | return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix) |
||
739 | . '\{' . $this->pCommaSeparated($node->uses) . '};'; |
||
740 | } |
||
741 | |||
742 | protected function pStmt_UseUse(Stmt\UseUse $node) { |
||
743 | return $this->pUseType($node->type) . $this->p($node->name) |
||
744 | . (null !== $node->alias ? ' as ' . $node->alias : ''); |
||
745 | } |
||
746 | |||
747 | protected function pUseType($type) { |
||
748 | return $type === Stmt\Use_::TYPE_FUNCTION ? 'function ' |
||
749 | : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : ''); |
||
750 | } |
||
751 | |||
752 | protected function pStmt_Interface(Stmt\Interface_ $node) { |
||
753 | return 'interface ' . $node->name |
||
754 | . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '') |
||
755 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
756 | } |
||
757 | |||
758 | protected function pStmt_Class(Stmt\Class_ $node) { |
||
759 | return $this->pClassCommon($node, ' ' . $node->name); |
||
760 | } |
||
761 | |||
762 | protected function pStmt_Trait(Stmt\Trait_ $node) { |
||
763 | return 'trait ' . $node->name |
||
764 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
765 | } |
||
766 | |||
767 | protected function pStmt_TraitUse(Stmt\TraitUse $node) { |
||
768 | $class = $this->pCommaSeparated($node->traits) |
||
769 | . (empty($node->adaptations)? ';': ' {' . $this->pStmts($node->adaptations) . $this->nl . '}'); |
||
770 | return 'use ' . $class; |
||
771 | } |
||
772 | |||
773 | protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) { |
||
774 | return $this->p($node->trait) . '::' . $node->method |
||
775 | . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';'; |
||
776 | } |
||
777 | |||
778 | protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) { |
||
779 | return (null !== $node->trait ? $this->p($node->trait) . '::' : '') |
||
780 | . $node->method . ' as' |
||
781 | . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '') |
||
782 | . (null !== $node->newName ? ' ' . $node->newName : '') |
||
783 | . ';'; |
||
784 | } |
||
785 | |||
786 | protected function pStmt_Property(Stmt\Property $node) { |
||
787 | return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) |
||
788 | . ($node->type ? $this->p($node->type) . ' ' : '') |
||
789 | . $this->pCommaSeparated($node->props) . ';'; |
||
790 | } |
||
791 | |||
792 | protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) { |
||
793 | return '$' . $node->name |
||
794 | . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
||
795 | } |
||
796 | |||
797 | protected function pStmt_ClassMethod(Stmt\ClassMethod $node) { |
||
798 | $code = $this->pModifiers($node->flags) |
||
799 | . 'function ' . ($node->byRef ? '&' : '') . $node->name |
||
800 | . '(' . $this->pMaybeMultiline($node->params) . ')' |
||
801 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
802 | . (null !== $node->stmts |
||
803 | ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' |
||
804 | : ';'); |
||
805 | $token = '\'__' . md5($code) . '__\''; |
||
806 | $this->options['builder']->closures[$token] = $code; |
||
807 | return $token; |
||
808 | } |
||
809 | |||
810 | protected function pStmt_ClassConst(Stmt\ClassConst $node) { |
||
811 | return $this->pModifiers($node->flags) |
||
812 | . 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
||
813 | } |
||
814 | |||
815 | protected function pStmt_Function(Stmt\Function_ $node) { |
||
816 | $code = 'function ' . ($node->byRef ? '&' : '') . $node->name |
||
817 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
818 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
819 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
820 | $token = '\'__' . md5($code) . '__\''; |
||
821 | $this->options['builder']->closures[$token] = $code; |
||
822 | return $token; |
||
823 | } |
||
824 | |||
825 | protected function pStmt_Const(Stmt\Const_ $node) { |
||
826 | return 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
||
827 | } |
||
828 | |||
829 | protected function pStmt_Declare(Stmt\Declare_ $node) { |
||
830 | return 'declare (' . $this->pCommaSeparated($node->declares) . ')' |
||
831 | . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';'); |
||
832 | } |
||
833 | |||
834 | protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) { |
||
835 | return $node->key . '=' . $this->p($node->value); |
||
836 | } |
||
837 | |||
838 | // Control flow |
||
839 | |||
840 | protected function pStmt_If(Stmt\If_ $node) { |
||
841 | return 'if (' . $this->p($node->cond) . ') {' |
||
842 | . $this->pStmts($node->stmts) . $this->nl . '}' |
||
843 | . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '') |
||
844 | . (null !== $node->else ? ' ' . $this->p($node->else) : ''); |
||
845 | } |
||
846 | |||
847 | protected function pStmt_ElseIf(Stmt\ElseIf_ $node) { |
||
848 | return 'elseif (' . $this->p($node->cond) . ') {' |
||
849 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
850 | } |
||
851 | |||
852 | protected function pStmt_Else(Stmt\Else_ $node) { |
||
853 | return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
854 | } |
||
855 | |||
856 | protected function pStmt_For(Stmt\For_ $node) { |
||
857 | return 'for (' |
||
858 | . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '') |
||
859 | . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '') |
||
860 | . $this->pCommaSeparated($node->loop) |
||
861 | . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
862 | } |
||
863 | |||
864 | protected function pStmt_Foreach(Stmt\Foreach_ $node) { |
||
865 | return 'foreach (' . $this->p($node->expr) . ' as ' |
||
866 | . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '') |
||
867 | . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {' |
||
868 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
869 | } |
||
870 | |||
871 | protected function pStmt_While(Stmt\While_ $node) { |
||
872 | return 'while (' . $this->p($node->cond) . ') {' |
||
873 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
874 | } |
||
875 | |||
876 | protected function pStmt_Do(Stmt\Do_ $node) { |
||
877 | return 'do {' . $this->pStmts($node->stmts) . $this->nl |
||
878 | . '} while (' . $this->p($node->cond) . ');'; |
||
879 | } |
||
880 | |||
881 | protected function pStmt_Switch(Stmt\Switch_ $node) { |
||
882 | return 'switch (' . $this->p($node->cond) . ') {' |
||
883 | . $this->pStmts($node->cases) . $this->nl . '}'; |
||
884 | } |
||
885 | |||
886 | protected function pStmt_Case(Stmt\Case_ $node) { |
||
887 | return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':' |
||
888 | . $this->pStmts($node->stmts); |
||
889 | } |
||
890 | |||
891 | protected function pStmt_TryCatch(Stmt\TryCatch $node) { |
||
892 | return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}' |
||
893 | . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '') |
||
894 | . ($node->finally !== null ? ' ' . $this->p($node->finally) : ''); |
||
895 | } |
||
896 | |||
897 | protected function pStmt_Catch(Stmt\Catch_ $node) { |
||
898 | return 'catch (' . $this->pImplode($node->types, '|') |
||
899 | . ($node->var !== null ? ' ' . $this->p($node->var) : '') |
||
900 | . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
901 | } |
||
902 | |||
903 | protected function pStmt_Finally(Stmt\Finally_ $node) { |
||
904 | return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
905 | } |
||
906 | |||
907 | protected function pStmt_Break(Stmt\Break_ $node) { |
||
908 | return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
||
909 | } |
||
910 | |||
911 | protected function pStmt_Continue(Stmt\Continue_ $node) { |
||
912 | return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
||
913 | } |
||
914 | |||
915 | protected function pStmt_Return(Stmt\Return_ $node) { |
||
916 | return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';'; |
||
917 | } |
||
918 | |||
919 | protected function pStmt_Throw(Stmt\Throw_ $node) { |
||
920 | return 'throw ' . $this->p($node->expr) . ';'; |
||
921 | } |
||
922 | |||
923 | protected function pStmt_Label(Stmt\Label $node) { |
||
924 | return $node->name . ':'; |
||
925 | } |
||
926 | |||
927 | protected function pStmt_Goto(Stmt\Goto_ $node) { |
||
928 | return 'goto ' . $node->name . ';'; |
||
929 | } |
||
930 | |||
931 | // Other |
||
932 | |||
933 | protected function pStmt_Expression(Stmt\Expression $node) { |
||
934 | return $this->p($node->expr) . ';'; |
||
935 | } |
||
936 | |||
937 | protected function pStmt_Echo(Stmt\Echo_ $node) { |
||
938 | return 'echo ' . $this->pCommaSeparated($node->exprs) . ';'; |
||
939 | } |
||
940 | |||
941 | protected function pStmt_Static(Stmt\Static_ $node) { |
||
942 | return 'static ' . $this->pCommaSeparated($node->vars) . ';'; |
||
943 | } |
||
944 | |||
945 | protected function pStmt_Global(Stmt\Global_ $node) { |
||
946 | return 'global ' . $this->pCommaSeparated($node->vars) . ';'; |
||
947 | } |
||
948 | |||
949 | protected function pStmt_StaticVar(Stmt\StaticVar $node) { |
||
950 | return $this->p($node->var) |
||
951 | . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
||
952 | } |
||
953 | |||
954 | protected function pStmt_Unset(Stmt\Unset_ $node) { |
||
955 | return 'unset(' . $this->pCommaSeparated($node->vars) . ');'; |
||
956 | } |
||
957 | |||
958 | protected function pStmt_InlineHTML(Stmt\InlineHTML $node) { |
||
959 | $newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : ''; |
||
960 | return '?>' . $newline . $node->value . '<?php '; |
||
961 | } |
||
962 | |||
963 | protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) { |
||
964 | return '__halt_compiler();' . $node->remaining; |
||
965 | } |
||
966 | |||
967 | protected function pStmt_Nop(Stmt\Nop $node) { |
||
968 | return ''; |
||
969 | } |
||
970 | |||
971 | // Helpers |
||
972 | |||
973 | protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) { |
||
974 | return $this->pModifiers($node->flags) |
||
975 | . 'class' . $afterClassToken |
||
976 | . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') |
||
977 | . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') |
||
978 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
979 | } |
||
980 | |||
981 | protected function pObjectProperty($node) { |
||
982 | if ($node instanceof Expr) { |
||
983 | return '{' . $this->p($node) . '}'; |
||
984 | } else { |
||
985 | return $node; |
||
986 | } |
||
987 | } |
||
988 | |||
989 | protected function pEncapsList(array $encapsList, $quote) { |
||
990 | $return = ''; |
||
991 | foreach ($encapsList as $element) { |
||
992 | if ($element instanceof Scalar\EncapsedStringPart) { |
||
993 | $return .= $this->escapeString($element->value, $quote); |
||
994 | } else { |
||
995 | $return .= '{' . $this->p($element) . '}'; |
||
996 | } |
||
997 | } |
||
998 | |||
999 | return $return; |
||
1000 | } |
||
1001 | |||
1002 | protected function pSingleQuotedString(string $string) { |
||
1003 | return '\'' . addcslashes($string, '\'\\') . '\''; |
||
1004 | } |
||
1005 | |||
1006 | protected function escapeString($string, $quote) { |
||
1007 | if (null === $quote) { |
||
1008 | // For doc strings, don't escape newlines |
||
1009 | $escaped = addcslashes($string, "\t\f\v$\\"); |
||
1010 | } else { |
||
1011 | $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\"); |
||
1012 | } |
||
1013 | |||
1014 | // Escape other control characters |
||
1015 | return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) { |
||
1016 | $oct = decoct(ord($matches[1])); |
||
1017 | if ($matches[2] !== '') { |
||
1018 | // If there is a trailing digit, use the full three character form |
||
1019 | return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT); |
||
1020 | } |
||
1021 | return '\\' . $oct; |
||
1022 | }, $escaped); |
||
1023 | } |
||
1024 | |||
1025 | protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) { |
||
1026 | $start = $atStart ? '(?:^|[\r\n])' : '[\r\n]'; |
||
1027 | $end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]'; |
||
1028 | return false !== strpos($string, $label) |
||
1029 | && preg_match('/' . $start . $label . $end . '/', $string); |
||
1030 | } |
||
1031 | |||
1032 | protected function encapsedContainsEndLabel(array $parts, $label) { |
||
1033 | foreach ($parts as $i => $part) { |
||
1034 | $atStart = $i === 0; |
||
1035 | $atEnd = $i === count($parts) - 1; |
||
1036 | if ($part instanceof Scalar\EncapsedStringPart |
||
1037 | && $this->containsEndLabel($part->value, $label, $atStart, $atEnd) |
||
1038 | ) { |
||
1039 | return true; |
||
1040 | } |
||
1041 | } |
||
1042 | return false; |
||
1043 | } |
||
1044 | |||
1045 | protected function pDereferenceLhs(Node $node) { |
||
1046 | if (!$this->dereferenceLhsRequiresParens($node)) { |
||
1047 | return $this->p($node); |
||
1048 | } else { |
||
1049 | return '(' . $this->p($node) . ')'; |
||
1050 | } |
||
1051 | } |
||
1052 | |||
1053 | protected function pCallLhs(Node $node) { |
||
1054 | if (!$this->callLhsRequiresParens($node)) { |
||
1055 | return $this->p($node); |
||
1056 | } else { |
||
1057 | return '(' . $this->p($node) . ')'; |
||
1058 | } |
||
1059 | } |
||
1060 | |||
1061 | protected function pNewVariable(Node $node) { |
||
1062 | // TODO: This is not fully accurate. |
||
1063 | return $this->pDereferenceLhs($node); |
||
1064 | } |
||
1065 | |||
1066 | /** |
||
1067 | * @param Node[] $nodes |
||
1068 | * @return bool |
||
1069 | */ |
||
1070 | private function hasNodeWithComments(array $nodes) { |
||
1077 | } |
||
1078 | |||
1079 | private function pMaybeMultiline(array $nodes, $trailingComma = false) { |
||
1080 | if (!$this->hasNodeWithComments($nodes)) { |
||
1081 | return $this->pCommaSeparated($nodes); |
||
1082 | } else { |
||
1083 | return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl; |
||
1084 | } |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * Pretty prints an array of nodes and implodes the printed values. |
||
1089 | * |
||
1090 | * @param Node[] $nodes Array of Nodes to be printed |
||
1091 | * @param string $glue Character to implode with |
||
1092 | * |
||
1093 | * @return string Imploded pretty printed nodes |
||
1094 | */ |
||
1095 | protected function pImplode(array $nodes, string $glue = '') : string { |
||
1096 | $pNodes = []; |
||
1097 | foreach ($nodes as $node) { |
||
1098 | if (null === $node) { |
||
1099 | $pNodes[] = ''; |
||
1100 | } else { |
||
1101 | $code = $this->p($node); |
||
1102 | if(isset($node->value) && $node->value instanceof BinaryOp\Concat){ |
||
1103 | $token = '\'__' . md5($code) . '__\''; |
||
1104 | $this->options['builder']->closures[$token] = $code; |
||
1105 | $code = $token; |
||
1106 | } |
||
1107 | $pNodes[] = $code; |
||
1108 | } |
||
1109 | } |
||
1110 | |||
1111 | return implode($glue, $pNodes); |
||
1112 | } |
||
1113 | } |
||
1114 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.