|
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
|
|
|
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) { |
|
259
|
|
|
return $this->pInfixOp(AssignOp\BitwiseAnd::class, $node->var, ' &= ', $node->expr); |
|
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
|
|
|
$code = ($node->byRef ? '&' : '') |
|
588
|
|
|
. ($node->unpack ? '...' : '') |
|
589
|
|
|
. $this->p($node->value); |
|
590
|
|
|
|
|
591
|
|
|
if(isset($node->value) && $node->value instanceof BinaryOp\Concat){ |
|
592
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
593
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
594
|
|
|
$code = $token; |
|
595
|
|
|
} |
|
596
|
|
|
return (null !== $node->key ? $this->p($node->key) . ' => ' : '') |
|
597
|
|
|
. $code; |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) { |
|
601
|
|
|
return $this->pDereferenceLhs($node->var) |
|
602
|
|
|
. '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']'; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
protected function pExpr_ConstFetch(Expr\ConstFetch $node) { |
|
606
|
|
|
return $this->p($node->name); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) { |
|
610
|
|
|
return $this->pDereferenceLhs($node->class) . '::' . $this->p($node->name); |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) { |
|
614
|
|
|
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name); |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node) { |
|
618
|
|
|
return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) { |
|
622
|
|
|
return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name); |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
protected function pExpr_ShellExec(Expr\ShellExec $node) { |
|
626
|
|
|
return '`' . $this->pEncapsList($node->parts, '`') . '`'; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
protected function pExpr_Closure(Expr\Closure $node) { |
|
630
|
|
|
$code = ($node->static ? 'static ' : '') |
|
631
|
|
|
. 'function ' . ($node->byRef ? '&' : '') |
|
632
|
|
|
. '(' . $this->pCommaSeparated($node->params) . ')' |
|
633
|
|
|
. (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '') |
|
634
|
|
|
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
|
635
|
|
|
. ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
636
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
637
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
638
|
|
|
return $token; |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
protected function pExpr_Match(Expr\Match_ $node) { |
|
642
|
|
|
return 'match (' . $this->p($node->cond) . ') {' |
|
643
|
|
|
. $this->pCommaSeparatedMultiline($node->arms, true) |
|
644
|
|
|
. $this->nl |
|
645
|
|
|
. '}'; |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
protected function pMatchArm(Node\MatchArm $node) { |
|
649
|
|
|
return ($node->conds ? $this->pCommaSeparated($node->conds) : 'default') |
|
650
|
|
|
. ' => ' . $this->p($node->body); |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
protected function pExpr_ArrowFunction(Expr\ArrowFunction $node) { |
|
654
|
|
|
$code = ($node->static ? 'static ' : '') |
|
655
|
|
|
. 'fn' . ($node->byRef ? '&' : '') |
|
656
|
|
|
. '(' . $this->pCommaSeparated($node->params) . ')' |
|
657
|
|
|
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '') |
|
658
|
|
|
. ' => ' |
|
659
|
|
|
. $this->p($node->expr); |
|
660
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
661
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
662
|
|
|
return $token; |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
protected function pExpr_ClosureUse(Expr\ClosureUse $node) { |
|
666
|
|
|
return ($node->byRef ? '&' : '') . $this->p($node->var); |
|
667
|
|
|
} |
|
668
|
|
|
|
|
669
|
|
|
/** |
|
670
|
|
|
* Return new class expression |
|
671
|
|
|
* @param Expr\New_ $node |
|
672
|
|
|
* |
|
673
|
|
|
* @return string |
|
674
|
|
|
*/ |
|
675
|
|
|
protected function pExpr_New(Expr\New_ $node) { |
|
676
|
|
|
if ($node->class instanceof Stmt\Class_) { |
|
677
|
|
|
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; |
|
678
|
|
|
$code = 'new ' . $this->pClassCommon($node->class, $args); |
|
679
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
680
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
681
|
|
|
return $token; |
|
682
|
|
|
} |
|
683
|
|
|
$code = 'new ' . $this->pNewVariable($node->class) |
|
684
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')'; |
|
685
|
|
|
if($this->pNewVariable($node->class) == 'ReverseBlockMerge'){ |
|
686
|
|
|
return $code; |
|
687
|
|
|
} |
|
688
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
689
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
690
|
|
|
return $token; |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
protected function pExpr_Clone(Expr\Clone_ $node) { |
|
694
|
|
|
return 'clone ' . $this->p($node->expr); |
|
695
|
|
|
} |
|
696
|
|
|
|
|
697
|
|
|
protected function pExpr_Ternary(Expr\Ternary $node) { |
|
698
|
|
|
// a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator. |
|
699
|
|
|
// this is okay because the part between ? and : never needs parentheses. |
|
700
|
|
|
return $this->pInfixOp(Expr\Ternary::class, |
|
701
|
|
|
$node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else |
|
702
|
|
|
); |
|
703
|
|
|
} |
|
704
|
|
|
|
|
705
|
|
|
protected function pExpr_Exit(Expr\Exit_ $node) { |
|
706
|
|
|
$kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE); |
|
707
|
|
|
return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die') |
|
708
|
|
|
. (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : ''); |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
|
|
protected function pExpr_Throw(Expr\Throw_ $node) { |
|
712
|
|
|
return 'throw ' . $this->p($node->expr); |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
protected function pExpr_Yield(Expr\Yield_ $node) { |
|
716
|
|
|
if ($node->value === null) { |
|
717
|
|
|
return 'yield'; |
|
718
|
|
|
} else { |
|
719
|
|
|
// this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary |
|
720
|
|
|
return '(yield ' |
|
721
|
|
|
. ($node->key !== null ? $this->p($node->key) . ' => ' : '') |
|
722
|
|
|
. $this->p($node->value) |
|
723
|
|
|
. ')'; |
|
724
|
|
|
} |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
// Declarations |
|
728
|
|
|
|
|
729
|
|
|
protected function pStmt_Namespace(Stmt\Namespace_ $node) { |
|
730
|
|
|
if ($this->canUseSemicolonNamespaces) { |
|
731
|
|
|
return 'namespace ' . $this->p($node->name) . ';' |
|
|
|
|
|
|
732
|
|
|
. $this->nl . $this->pStmts($node->stmts, false); |
|
733
|
|
|
} else { |
|
734
|
|
|
return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '') |
|
735
|
|
|
. ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
736
|
|
|
} |
|
737
|
|
|
} |
|
738
|
|
|
|
|
739
|
|
|
protected function pStmt_Use(Stmt\Use_ $node) { |
|
740
|
|
|
return 'use ' . $this->pUseType($node->type) |
|
741
|
|
|
. $this->pCommaSeparated($node->uses) . ';'; |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
protected function pStmt_GroupUse(Stmt\GroupUse $node) { |
|
745
|
|
|
return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix) |
|
746
|
|
|
. '\{' . $this->pCommaSeparated($node->uses) . '};'; |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
|
|
protected function pStmt_UseUse(Stmt\UseUse $node) { |
|
750
|
|
|
return $this->pUseType($node->type) . $this->p($node->name) |
|
751
|
|
|
. (null !== $node->alias ? ' as ' . $node->alias : ''); |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
|
|
protected function pUseType($type) { |
|
755
|
|
|
return $type === Stmt\Use_::TYPE_FUNCTION ? 'function ' |
|
756
|
|
|
: ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : ''); |
|
757
|
|
|
} |
|
758
|
|
|
|
|
759
|
|
|
protected function pStmt_Interface(Stmt\Interface_ $node) { |
|
760
|
|
|
return 'interface ' . $node->name |
|
761
|
|
|
. (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '') |
|
762
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
protected function pStmt_Class(Stmt\Class_ $node) { |
|
766
|
|
|
return $this->pClassCommon($node, ' ' . $node->name); |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
|
|
protected function pStmt_Trait(Stmt\Trait_ $node) { |
|
770
|
|
|
return 'trait ' . $node->name |
|
771
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
772
|
|
|
} |
|
773
|
|
|
|
|
774
|
|
|
protected function pStmt_TraitUse(Stmt\TraitUse $node) { |
|
775
|
|
|
$class = $this->pCommaSeparated($node->traits) |
|
776
|
|
|
. (empty($node->adaptations)? ';': ' {' . $this->pStmts($node->adaptations) . $this->nl . '}'); |
|
777
|
|
|
return 'use ' . $class; |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) { |
|
781
|
|
|
return $this->p($node->trait) . '::' . $node->method |
|
|
|
|
|
|
782
|
|
|
. ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';'; |
|
783
|
|
|
} |
|
784
|
|
|
|
|
785
|
|
|
protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) { |
|
786
|
|
|
return (null !== $node->trait ? $this->p($node->trait) . '::' : '') |
|
787
|
|
|
. $node->method . ' as' |
|
788
|
|
|
. (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '') |
|
789
|
|
|
. (null !== $node->newName ? ' ' . $node->newName : '') |
|
790
|
|
|
. ';'; |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
protected function pStmt_Property(Stmt\Property $node) { |
|
794
|
|
|
return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) |
|
795
|
|
|
. ($node->type ? $this->p($node->type) . ' ' : '') |
|
796
|
|
|
. $this->pCommaSeparated($node->props) . ';'; |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) { |
|
800
|
|
|
return '$' . $node->name |
|
801
|
|
|
. (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
|
802
|
|
|
} |
|
803
|
|
|
|
|
804
|
|
|
protected function pStmt_ClassMethod(Stmt\ClassMethod $node) { |
|
805
|
|
|
$code = $this->pModifiers($node->flags) |
|
806
|
|
|
. 'function ' . ($node->byRef ? '&' : '') . $node->name |
|
807
|
|
|
. '(' . $this->pMaybeMultiline($node->params) . ')' |
|
808
|
|
|
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
|
809
|
|
|
. (null !== $node->stmts |
|
810
|
|
|
? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' |
|
811
|
|
|
: ';'); |
|
812
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
813
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
814
|
|
|
return $token; |
|
815
|
|
|
} |
|
816
|
|
|
|
|
817
|
|
|
protected function pStmt_ClassConst(Stmt\ClassConst $node) { |
|
818
|
|
|
return $this->pModifiers($node->flags) |
|
819
|
|
|
. 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
|
820
|
|
|
} |
|
821
|
|
|
|
|
822
|
|
|
protected function pStmt_Function(Stmt\Function_ $node) { |
|
823
|
|
|
$code = 'function ' . ($node->byRef ? '&' : '') . $node->name |
|
824
|
|
|
. '(' . $this->pCommaSeparated($node->params) . ')' |
|
825
|
|
|
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
|
826
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
827
|
|
|
$token = '\'__' . md5($code) . '__\''; |
|
828
|
|
|
$this->options['builder']->closures[$token] = $code; |
|
829
|
|
|
return $token; |
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
protected function pStmt_Const(Stmt\Const_ $node) { |
|
833
|
|
|
return 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
|
834
|
|
|
} |
|
835
|
|
|
|
|
836
|
|
|
protected function pStmt_Declare(Stmt\Declare_ $node) { |
|
837
|
|
|
return 'declare (' . $this->pCommaSeparated($node->declares) . ')' |
|
838
|
|
|
. (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';'); |
|
839
|
|
|
} |
|
840
|
|
|
|
|
841
|
|
|
protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) { |
|
842
|
|
|
return $node->key . '=' . $this->p($node->value); |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
// Control flow |
|
846
|
|
|
|
|
847
|
|
|
protected function pStmt_If(Stmt\If_ $node) { |
|
848
|
|
|
return 'if (' . $this->p($node->cond) . ') {' |
|
849
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}' |
|
850
|
|
|
. ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '') |
|
851
|
|
|
. (null !== $node->else ? ' ' . $this->p($node->else) : ''); |
|
852
|
|
|
} |
|
853
|
|
|
|
|
854
|
|
|
protected function pStmt_ElseIf(Stmt\ElseIf_ $node) { |
|
855
|
|
|
return 'elseif (' . $this->p($node->cond) . ') {' |
|
856
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}'; |
|
857
|
|
|
} |
|
858
|
|
|
|
|
859
|
|
|
protected function pStmt_Else(Stmt\Else_ $node) { |
|
860
|
|
|
return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
861
|
|
|
} |
|
862
|
|
|
|
|
863
|
|
|
protected function pStmt_For(Stmt\For_ $node) { |
|
864
|
|
|
return 'for (' |
|
865
|
|
|
. $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '') |
|
866
|
|
|
. $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '') |
|
867
|
|
|
. $this->pCommaSeparated($node->loop) |
|
868
|
|
|
. ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
869
|
|
|
} |
|
870
|
|
|
|
|
871
|
|
|
protected function pStmt_Foreach(Stmt\Foreach_ $node) { |
|
872
|
|
|
return 'foreach (' . $this->p($node->expr) . ' as ' |
|
873
|
|
|
. (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '') |
|
874
|
|
|
. ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {' |
|
875
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}'; |
|
876
|
|
|
} |
|
877
|
|
|
|
|
878
|
|
|
protected function pStmt_While(Stmt\While_ $node) { |
|
879
|
|
|
return 'while (' . $this->p($node->cond) . ') {' |
|
880
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}'; |
|
881
|
|
|
} |
|
882
|
|
|
|
|
883
|
|
|
protected function pStmt_Do(Stmt\Do_ $node) { |
|
884
|
|
|
return 'do {' . $this->pStmts($node->stmts) . $this->nl |
|
885
|
|
|
. '} while (' . $this->p($node->cond) . ');'; |
|
886
|
|
|
} |
|
887
|
|
|
|
|
888
|
|
|
protected function pStmt_Switch(Stmt\Switch_ $node) { |
|
889
|
|
|
return 'switch (' . $this->p($node->cond) . ') {' |
|
890
|
|
|
. $this->pStmts($node->cases) . $this->nl . '}'; |
|
891
|
|
|
} |
|
892
|
|
|
|
|
893
|
|
|
protected function pStmt_Case(Stmt\Case_ $node) { |
|
894
|
|
|
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':' |
|
895
|
|
|
. $this->pStmts($node->stmts); |
|
896
|
|
|
} |
|
897
|
|
|
|
|
898
|
|
|
protected function pStmt_TryCatch(Stmt\TryCatch $node) { |
|
899
|
|
|
return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}' |
|
900
|
|
|
. ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '') |
|
901
|
|
|
. ($node->finally !== null ? ' ' . $this->p($node->finally) : ''); |
|
902
|
|
|
} |
|
903
|
|
|
|
|
904
|
|
|
protected function pStmt_Catch(Stmt\Catch_ $node) { |
|
905
|
|
|
return 'catch (' . $this->pImplode($node->types, '|') |
|
906
|
|
|
. ($node->var !== null ? ' ' . $this->p($node->var) : '') |
|
907
|
|
|
. ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
908
|
|
|
} |
|
909
|
|
|
|
|
910
|
|
|
protected function pStmt_Finally(Stmt\Finally_ $node) { |
|
911
|
|
|
return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
912
|
|
|
} |
|
913
|
|
|
|
|
914
|
|
|
protected function pStmt_Break(Stmt\Break_ $node) { |
|
915
|
|
|
return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
|
916
|
|
|
} |
|
917
|
|
|
|
|
918
|
|
|
protected function pStmt_Continue(Stmt\Continue_ $node) { |
|
919
|
|
|
return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
protected function pStmt_Return(Stmt\Return_ $node) { |
|
923
|
|
|
return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';'; |
|
924
|
|
|
} |
|
925
|
|
|
|
|
926
|
|
|
protected function pStmt_Throw(Stmt\Throw_ $node) { |
|
927
|
|
|
return 'throw ' . $this->p($node->expr) . ';'; |
|
928
|
|
|
} |
|
929
|
|
|
|
|
930
|
|
|
protected function pStmt_Label(Stmt\Label $node) { |
|
931
|
|
|
return $node->name . ':'; |
|
932
|
|
|
} |
|
933
|
|
|
|
|
934
|
|
|
protected function pStmt_Goto(Stmt\Goto_ $node) { |
|
935
|
|
|
return 'goto ' . $node->name . ';'; |
|
936
|
|
|
} |
|
937
|
|
|
|
|
938
|
|
|
// Other |
|
939
|
|
|
|
|
940
|
|
|
protected function pStmt_Expression(Stmt\Expression $node) { |
|
941
|
|
|
return $this->p($node->expr) . ';'; |
|
942
|
|
|
} |
|
943
|
|
|
|
|
944
|
|
|
protected function pStmt_Echo(Stmt\Echo_ $node) { |
|
945
|
|
|
return 'echo ' . $this->pCommaSeparated($node->exprs) . ';'; |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
protected function pStmt_Static(Stmt\Static_ $node) { |
|
949
|
|
|
return 'static ' . $this->pCommaSeparated($node->vars) . ';'; |
|
950
|
|
|
} |
|
951
|
|
|
|
|
952
|
|
|
protected function pStmt_Global(Stmt\Global_ $node) { |
|
953
|
|
|
return 'global ' . $this->pCommaSeparated($node->vars) . ';'; |
|
954
|
|
|
} |
|
955
|
|
|
|
|
956
|
|
|
protected function pStmt_StaticVar(Stmt\StaticVar $node) { |
|
957
|
|
|
return $this->p($node->var) |
|
958
|
|
|
. (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
protected function pStmt_Unset(Stmt\Unset_ $node) { |
|
962
|
|
|
return 'unset(' . $this->pCommaSeparated($node->vars) . ');'; |
|
963
|
|
|
} |
|
964
|
|
|
|
|
965
|
|
|
protected function pStmt_InlineHTML(Stmt\InlineHTML $node) { |
|
966
|
|
|
$newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : ''; |
|
967
|
|
|
return '?>' . $newline . $node->value . '<?php '; |
|
968
|
|
|
} |
|
969
|
|
|
|
|
970
|
|
|
protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) { |
|
971
|
|
|
return '__halt_compiler();' . $node->remaining; |
|
972
|
|
|
} |
|
973
|
|
|
|
|
974
|
|
|
protected function pStmt_Nop(Stmt\Nop $node) { |
|
|
|
|
|
|
975
|
|
|
return ''; |
|
976
|
|
|
} |
|
977
|
|
|
|
|
978
|
|
|
// Helpers |
|
979
|
|
|
|
|
980
|
|
|
protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) { |
|
981
|
|
|
return $this->pModifiers($node->flags) |
|
982
|
|
|
. 'class' . $afterClassToken |
|
983
|
|
|
. (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') |
|
984
|
|
|
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') |
|
985
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
|
986
|
|
|
} |
|
987
|
|
|
|
|
988
|
|
|
protected function pObjectProperty($node) { |
|
989
|
|
|
if ($node instanceof Expr) { |
|
990
|
|
|
return '{' . $this->p($node) . '}'; |
|
991
|
|
|
} else { |
|
992
|
|
|
return $node; |
|
993
|
|
|
} |
|
994
|
|
|
} |
|
995
|
|
|
|
|
996
|
|
|
protected function pEncapsList(array $encapsList, $quote) { |
|
997
|
|
|
$return = ''; |
|
998
|
|
|
foreach ($encapsList as $element) { |
|
999
|
|
|
if ($element instanceof Scalar\EncapsedStringPart) { |
|
1000
|
|
|
$return .= $this->escapeString($element->value, $quote); |
|
1001
|
|
|
} else { |
|
1002
|
|
|
$return .= '{' . $this->p($element) . '}'; |
|
1003
|
|
|
} |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
|
|
return $return; |
|
1007
|
|
|
} |
|
1008
|
|
|
|
|
1009
|
|
|
protected function pSingleQuotedString(string $string) { |
|
1010
|
|
|
return '\'' . addcslashes($string, '\'\\') . '\''; |
|
1011
|
|
|
} |
|
1012
|
|
|
|
|
1013
|
|
|
protected function escapeString($string, $quote) { |
|
1014
|
|
|
if (null === $quote) { |
|
1015
|
|
|
// For doc strings, don't escape newlines |
|
1016
|
|
|
$escaped = addcslashes($string, "\t\f\v$\\"); |
|
1017
|
|
|
} else { |
|
1018
|
|
|
$escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\"); |
|
1019
|
|
|
} |
|
1020
|
|
|
|
|
1021
|
|
|
// Escape other control characters |
|
1022
|
|
|
return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) { |
|
1023
|
|
|
$oct = decoct(ord($matches[1])); |
|
1024
|
|
|
if ($matches[2] !== '') { |
|
1025
|
|
|
// If there is a trailing digit, use the full three character form |
|
1026
|
|
|
return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT); |
|
1027
|
|
|
} |
|
1028
|
|
|
return '\\' . $oct; |
|
1029
|
|
|
}, $escaped); |
|
1030
|
|
|
} |
|
1031
|
|
|
|
|
1032
|
|
|
protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) { |
|
1033
|
|
|
$start = $atStart ? '(?:^|[\r\n])' : '[\r\n]'; |
|
1034
|
|
|
$end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]'; |
|
1035
|
|
|
return false !== strpos($string, $label) |
|
1036
|
|
|
&& preg_match('/' . $start . $label . $end . '/', $string); |
|
1037
|
|
|
} |
|
1038
|
|
|
|
|
1039
|
|
|
protected function encapsedContainsEndLabel(array $parts, $label) { |
|
1040
|
|
|
foreach ($parts as $i => $part) { |
|
1041
|
|
|
$atStart = $i === 0; |
|
1042
|
|
|
$atEnd = $i === count($parts) - 1; |
|
1043
|
|
|
if ($part instanceof Scalar\EncapsedStringPart |
|
1044
|
|
|
&& $this->containsEndLabel($part->value, $label, $atStart, $atEnd) |
|
1045
|
|
|
) { |
|
1046
|
|
|
return true; |
|
1047
|
|
|
} |
|
1048
|
|
|
} |
|
1049
|
|
|
return false; |
|
1050
|
|
|
} |
|
1051
|
|
|
|
|
1052
|
|
|
protected function pDereferenceLhs(Node $node) { |
|
1053
|
|
|
if (!$this->dereferenceLhsRequiresParens($node)) { |
|
1054
|
|
|
return $this->p($node); |
|
1055
|
|
|
} else { |
|
1056
|
|
|
return '(' . $this->p($node) . ')'; |
|
1057
|
|
|
} |
|
1058
|
|
|
} |
|
1059
|
|
|
|
|
1060
|
|
|
protected function pCallLhs(Node $node) { |
|
1061
|
|
|
if (!$this->callLhsRequiresParens($node)) { |
|
1062
|
|
|
return $this->p($node); |
|
1063
|
|
|
} else { |
|
1064
|
|
|
return '(' . $this->p($node) . ')'; |
|
1065
|
|
|
} |
|
1066
|
|
|
} |
|
1067
|
|
|
|
|
1068
|
|
|
protected function pNewVariable(Node $node) { |
|
1069
|
|
|
// TODO: This is not fully accurate. |
|
1070
|
|
|
return $this->pDereferenceLhs($node); |
|
1071
|
|
|
} |
|
1072
|
|
|
|
|
1073
|
|
|
/** |
|
1074
|
|
|
* @param Node[] $nodes |
|
1075
|
|
|
* @return bool |
|
1076
|
|
|
*/ |
|
1077
|
|
|
private function hasNodeWithComments(array $nodes) { |
|
1078
|
|
|
foreach ($nodes as $node) { |
|
1079
|
|
|
if ($node && $node->getComments()) { |
|
1080
|
|
|
return true; |
|
1081
|
|
|
} |
|
1082
|
|
|
} |
|
1083
|
|
|
return false; |
|
1084
|
|
|
} |
|
1085
|
|
|
|
|
1086
|
|
|
private function pMaybeMultiline(array $nodes, $trailingComma = false) { |
|
1087
|
|
|
if (!$this->hasNodeWithComments($nodes)) { |
|
1088
|
|
|
return $this->pCommaSeparated($nodes); |
|
1089
|
|
|
} else { |
|
1090
|
|
|
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl; |
|
1091
|
|
|
} |
|
1092
|
|
|
} |
|
1093
|
|
|
} |
|
1094
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.