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