Standard::pStmt_HaltCompiler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhpParser\PrettyPrinter;
4
5
use PhpParser\PrettyPrinterAbstract;
6
use PhpParser\Node;
7
use PhpParser\Node\Scalar;
8
use PhpParser\Node\Scalar\MagicConst;
9
use PhpParser\Node\Expr;
10
use PhpParser\Node\Expr\AssignOp;
11
use PhpParser\Node\Expr\BinaryOp;
12
use PhpParser\Node\Expr\Cast;
13
use PhpParser\Node\Stmt;
14
use PhpParser\Node\Name;
15
16
class Standard extends PrettyPrinterAbstract
17
{
18
    // Special nodes
19
20
    public function pParam(Node\Param $node) {
21
        return ($node->type ? $this->pType($node->type) . ' ' : '')
22
             . ($node->byRef ? '&' : '')
23
             . ($node->variadic ? '...' : '')
24
             . '$' . $node->name
25
             . ($node->default ? ' = ' . $this->p($node->default) : '');
26
    }
27
28
    public function pArg(Node\Arg $node) {
29
        return ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') . $this->p($node->value);
30
    }
31
32
    public function pConst(Node\Const_ $node) {
33
        return $node->name . ' = ' . $this->p($node->value);
34
    }
35
36
    // Names
37
38
    public function pName(Name $node) {
39
        return implode('\\', $node->parts);
40
    }
41
42
    public function pName_FullyQualified(Name\FullyQualified $node) {
43
        return '\\' . implode('\\', $node->parts);
44
    }
45
46
    public function pName_Relative(Name\Relative $node) {
47
        return 'namespace\\' . implode('\\', $node->parts);
48
    }
49
50
    // Magic Constants
51
52
    public function pScalar_MagicConst_Class(MagicConst\Class_ $node) {
53
        return '__CLASS__';
54
    }
55
56
    public function pScalar_MagicConst_Dir(MagicConst\Dir $node) {
57
        return '__DIR__';
58
    }
59
60
    public function pScalar_MagicConst_File(MagicConst\File $node) {
61
        return '__FILE__';
62
    }
63
64
    public function pScalar_MagicConst_Function(MagicConst\Function_ $node) {
65
        return '__FUNCTION__';
66
    }
67
68
    public function pScalar_MagicConst_Line(MagicConst\Line $node) {
69
        return '__LINE__';
70
    }
71
72
    public function pScalar_MagicConst_Method(MagicConst\Method $node) {
73
        return '__METHOD__';
74
    }
75
76
    public function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) {
77
        return '__NAMESPACE__';
78
    }
79
80
    public function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) {
81
        return '__TRAIT__';
82
    }
83
84
    // Scalars
85
86
    public function pScalar_String(Scalar\String_ $node) {
87
        return '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
88
    }
89
90
    public function pScalar_Encapsed(Scalar\Encapsed $node) {
91
        return '"' . $this->pEncapsList($node->parts, '"') . '"';
92
    }
93
94
    public function pScalar_LNumber(Scalar\LNumber $node) {
95
        return (string) $node->value;
96
    }
97
98
    public function pScalar_DNumber(Scalar\DNumber $node) {
99
        $stringValue = sprintf('%.16G', $node->value);
100
        if ($node->value !== (double) $stringValue) {
101
            $stringValue = sprintf('%.17G', $node->value);
102
        }
103
104
        // ensure that number is really printed as float
105
        return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
106
    }
107
108
    // Assignments
109
110
    public function pExpr_Assign(Expr\Assign $node) {
111
        return $this->pInfixOp('Expr_Assign', $node->var, ' = ', $node->expr);
112
    }
113
114
    public function pExpr_AssignRef(Expr\AssignRef $node) {
115
        return $this->pInfixOp('Expr_AssignRef', $node->var, ' =& ', $node->expr);
116
    }
117
118
    public function pExpr_AssignOp_Plus(AssignOp\Plus $node) {
119
        return $this->pInfixOp('Expr_AssignOp_Plus', $node->var, ' += ', $node->expr);
120
    }
121
122
    public function pExpr_AssignOp_Minus(AssignOp\Minus $node) {
123
        return $this->pInfixOp('Expr_AssignOp_Minus', $node->var, ' -= ', $node->expr);
124
    }
125
126
    public function pExpr_AssignOp_Mul(AssignOp\Mul $node) {
127
        return $this->pInfixOp('Expr_AssignOp_Mul', $node->var, ' *= ', $node->expr);
128
    }
129
130
    public function pExpr_AssignOp_Div(AssignOp\Div $node) {
131
        return $this->pInfixOp('Expr_AssignOp_Div', $node->var, ' /= ', $node->expr);
132
    }
133
134
    public function pExpr_AssignOp_Concat(AssignOp\Concat $node) {
135
        return $this->pInfixOp('Expr_AssignOp_Concat', $node->var, ' .= ', $node->expr);
136
    }
137
138
    public function pExpr_AssignOp_Mod(AssignOp\Mod $node) {
139
        return $this->pInfixOp('Expr_AssignOp_Mod', $node->var, ' %= ', $node->expr);
140
    }
141
142
    public function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) {
143
        return $this->pInfixOp('Expr_AssignOp_BitwiseAnd', $node->var, ' &= ', $node->expr);
144
    }
145
146
    public function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) {
147
        return $this->pInfixOp('Expr_AssignOp_BitwiseOr', $node->var, ' |= ', $node->expr);
148
    }
149
150
    public function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) {
151
        return $this->pInfixOp('Expr_AssignOp_BitwiseXor', $node->var, ' ^= ', $node->expr);
152
    }
153
154
    public function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) {
155
        return $this->pInfixOp('Expr_AssignOp_ShiftLeft', $node->var, ' <<= ', $node->expr);
156
    }
157
158
    public function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) {
159
        return $this->pInfixOp('Expr_AssignOp_ShiftRight', $node->var, ' >>= ', $node->expr);
160
    }
161
162
    public function pExpr_AssignOp_Pow(AssignOp\Pow $node) {
163
        return $this->pInfixOp('Expr_AssignOp_Pow', $node->var, ' **= ', $node->expr);
164
    }
165
166
    // Binary expressions
167
168
    public function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) {
169
        return $this->pInfixOp('Expr_BinaryOp_Plus', $node->left, ' + ', $node->right);
170
    }
171
172
    public function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) {
173
        return $this->pInfixOp('Expr_BinaryOp_Minus', $node->left, ' - ', $node->right);
174
    }
175
176
    public function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) {
177
        return $this->pInfixOp('Expr_BinaryOp_Mul', $node->left, ' * ', $node->right);
178
    }
179
180
    public function pExpr_BinaryOp_Div(BinaryOp\Div $node) {
181
        return $this->pInfixOp('Expr_BinaryOp_Div', $node->left, ' / ', $node->right);
182
    }
183
184
    public function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) {
185
        return $this->pInfixOp('Expr_BinaryOp_Concat', $node->left, ' . ', $node->right);
186
    }
187
188
    public function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) {
189
        return $this->pInfixOp('Expr_BinaryOp_Mod', $node->left, ' % ', $node->right);
190
    }
191
192
    public function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) {
193
        return $this->pInfixOp('Expr_BinaryOp_BooleanAnd', $node->left, ' && ', $node->right);
194
    }
195
196
    public function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) {
197
        return $this->pInfixOp('Expr_BinaryOp_BooleanOr', $node->left, ' || ', $node->right);
198
    }
199
200
    public function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) {
201
        return $this->pInfixOp('Expr_BinaryOp_BitwiseAnd', $node->left, ' & ', $node->right);
202
    }
203
204
    public function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) {
205
        return $this->pInfixOp('Expr_BinaryOp_BitwiseOr', $node->left, ' | ', $node->right);
206
    }
207
208
    public function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) {
209
        return $this->pInfixOp('Expr_BinaryOp_BitwiseXor', $node->left, ' ^ ', $node->right);
210
    }
211
212
    public function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) {
213
        return $this->pInfixOp('Expr_BinaryOp_ShiftLeft', $node->left, ' << ', $node->right);
214
    }
215
216
    public function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) {
217
        return $this->pInfixOp('Expr_BinaryOp_ShiftRight', $node->left, ' >> ', $node->right);
218
    }
219
220
    public function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) {
221
        return $this->pInfixOp('Expr_BinaryOp_Pow', $node->left, ' ** ', $node->right);
222
    }
223
224
    public function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) {
225
        return $this->pInfixOp('Expr_BinaryOp_LogicalAnd', $node->left, ' and ', $node->right);
226
    }
227
228
    public function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) {
229
        return $this->pInfixOp('Expr_BinaryOp_LogicalOr', $node->left, ' or ', $node->right);
230
    }
231
232
    public function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) {
233
        return $this->pInfixOp('Expr_BinaryOp_LogicalXor', $node->left, ' xor ', $node->right);
234
    }
235
236
    public function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) {
237
        return $this->pInfixOp('Expr_BinaryOp_Equal', $node->left, ' == ', $node->right);
238
    }
239
240
    public function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) {
241
        return $this->pInfixOp('Expr_BinaryOp_NotEqual', $node->left, ' != ', $node->right);
242
    }
243
244
    public function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) {
245
        return $this->pInfixOp('Expr_BinaryOp_Identical', $node->left, ' === ', $node->right);
246
    }
247
248
    public function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) {
249
        return $this->pInfixOp('Expr_BinaryOp_NotIdentical', $node->left, ' !== ', $node->right);
250
    }
251
252
    public function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) {
253
        return $this->pInfixOp('Expr_BinaryOp_Spaceship', $node->left, ' <=> ', $node->right);
254
    }
255
256
    public function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) {
257
        return $this->pInfixOp('Expr_BinaryOp_Greater', $node->left, ' > ', $node->right);
258
    }
259
260
    public function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) {
261
        return $this->pInfixOp('Expr_BinaryOp_GreaterOrEqual', $node->left, ' >= ', $node->right);
262
    }
263
264
    public function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) {
265
        return $this->pInfixOp('Expr_BinaryOp_Smaller', $node->left, ' < ', $node->right);
266
    }
267
268
    public function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) {
269
        return $this->pInfixOp('Expr_BinaryOp_SmallerOrEqual', $node->left, ' <= ', $node->right);
270
    }
271
272
    public function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) {
273
        return $this->pInfixOp('Expr_BinaryOp_Coalesce', $node->left, ' ?? ', $node->right);
274
    }
275
276
    public function pExpr_Instanceof(Expr\Instanceof_ $node) {
277
        return $this->pInfixOp('Expr_Instanceof', $node->expr, ' instanceof ', $node->class);
278
    }
279
280
    // Unary expressions
281
282
    public function pExpr_BooleanNot(Expr\BooleanNot $node) {
283
        return $this->pPrefixOp('Expr_BooleanNot', '!', $node->expr);
284
    }
285
286
    public function pExpr_BitwiseNot(Expr\BitwiseNot $node) {
287
        return $this->pPrefixOp('Expr_BitwiseNot', '~', $node->expr);
288
    }
289
290
    public function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
291
        return $this->pPrefixOp('Expr_UnaryMinus', '-', $node->expr);
292
    }
293
294
    public function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
295
        return $this->pPrefixOp('Expr_UnaryPlus', '+', $node->expr);
296
    }
297
298
    public function pExpr_PreInc(Expr\PreInc $node) {
299
        return $this->pPrefixOp('Expr_PreInc', '++', $node->var);
300
    }
301
302
    public function pExpr_PreDec(Expr\PreDec $node) {
303
        return $this->pPrefixOp('Expr_PreDec', '--', $node->var);
304
    }
305
306
    public function pExpr_PostInc(Expr\PostInc $node) {
307
        return $this->pPostfixOp('Expr_PostInc', $node->var, '++');
308
    }
309
310
    public function pExpr_PostDec(Expr\PostDec $node) {
311
        return $this->pPostfixOp('Expr_PostDec', $node->var, '--');
312
    }
313
314
    public function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) {
315
        return $this->pPrefixOp('Expr_ErrorSuppress', '@', $node->expr);
316
    }
317
318
    public function pExpr_YieldFrom(Expr\YieldFrom $node) {
319
        return $this->pPrefixOp('Expr_YieldFrom', 'yield from ', $node->expr);
320
    }
321
322
    public function pExpr_Print(Expr\Print_ $node) {
323
        return $this->pPrefixOp('Expr_Print', 'print ', $node->expr);
324
    }
325
326
    // Casts
327
328
    public function pExpr_Cast_Int(Cast\Int_ $node) {
329
        return $this->pPrefixOp('Expr_Cast_Int', '(int) ', $node->expr);
330
    }
331
332
    public function pExpr_Cast_Double(Cast\Double $node) {
333
        return $this->pPrefixOp('Expr_Cast_Double', '(double) ', $node->expr);
334
    }
335
336
    public function pExpr_Cast_String(Cast\String_ $node) {
337
        return $this->pPrefixOp('Expr_Cast_String', '(string) ', $node->expr);
338
    }
339
340
    public function pExpr_Cast_Array(Cast\Array_ $node) {
341
        return $this->pPrefixOp('Expr_Cast_Array', '(array) ', $node->expr);
342
    }
343
344
    public function pExpr_Cast_Object(Cast\Object_ $node) {
345
        return $this->pPrefixOp('Expr_Cast_Object', '(object) ', $node->expr);
346
    }
347
348
    public function pExpr_Cast_Bool(Cast\Bool_ $node) {
349
        return $this->pPrefixOp('Expr_Cast_Bool', '(bool) ', $node->expr);
350
    }
351
352
    public function pExpr_Cast_Unset(Cast\Unset_ $node) {
353
        return $this->pPrefixOp('Expr_Cast_Unset', '(unset) ', $node->expr);
354
    }
355
356
    // Function calls and similar constructs
357
358
    public function pExpr_FuncCall(Expr\FuncCall $node) {
359
        return $this->pCallLhs($node->name)
360
             . '(' . $this->pCommaSeparated($node->args) . ')';
361
    }
362
363
    public function pExpr_MethodCall(Expr\MethodCall $node) {
364
        return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
365
             . '(' . $this->pCommaSeparated($node->args) . ')';
366
    }
367
368
    public function pExpr_StaticCall(Expr\StaticCall $node) {
369
        return $this->pDereferenceLhs($node->class) . '::'
370
             . ($node->name instanceof Expr
371
                ? ($node->name instanceof Expr\Variable
372
                   ? $this->p($node->name)
373
                   : '{' . $this->p($node->name) . '}')
374
                : $node->name)
375
             . '(' . $this->pCommaSeparated($node->args) . ')';
376
    }
377
378
    public function pExpr_Empty(Expr\Empty_ $node) {
379
        return 'empty(' . $this->p($node->expr) . ')';
380
    }
381
382
    public function pExpr_Isset(Expr\Isset_ $node) {
383
        return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
384
    }
385
386
    public function pExpr_Eval(Expr\Eval_ $node) {
387
        return 'eval(' . $this->p($node->expr) . ')';
388
    }
389
390
    public function pExpr_Include(Expr\Include_ $node) {
391
        static $map = array(
392
            Expr\Include_::TYPE_INCLUDE      => 'include',
393
            Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
394
            Expr\Include_::TYPE_REQUIRE      => 'require',
395
            Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
396
        );
397
398
        return $map[$node->type] . ' ' . $this->p($node->expr);
399
    }
400
401
    public function pExpr_List(Expr\List_ $node) {
402
        $pList = array();
403
        foreach ($node->vars as $var) {
404
            if (null === $var) {
405
                $pList[] = '';
406
            } else {
407
                $pList[] = $this->p($var);
408
            }
409
        }
410
411
        return 'list(' . implode(', ', $pList) . ')';
412
    }
413
414
    // Other
415
416
    public function pExpr_Variable(Expr\Variable $node) {
417
        if ($node->name instanceof Expr) {
418
            return '${' . $this->p($node->name) . '}';
419
        } else {
420
            return '$' . $node->name;
421
        }
422
    }
423
424
    public function pExpr_Array(Expr\Array_ $node) {
425
        if ($this->options['shortArraySyntax']) {
426
            return '[' . $this->pCommaSeparated($node->items) . ']';
427
        } else {
428
            return 'array(' . $this->pCommaSeparated($node->items) . ')';
429
        }
430
    }
431
432
    public function pExpr_ArrayItem(Expr\ArrayItem $node) {
433
        return (null !== $node->key ? $this->p($node->key) . ' => ' : '')
434
             . ($node->byRef ? '&' : '') . $this->p($node->value);
435
    }
436
437
    public function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) {
438
        return $this->pDereferenceLhs($node->var)
439
             . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
440
    }
441
442
    public function pExpr_ConstFetch(Expr\ConstFetch $node) {
443
        return $this->p($node->name);
444
    }
445
446
    public function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) {
447
        return $this->p($node->class) . '::' . $node->name;
448
    }
449
450
    public function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
451
        return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
452
    }
453
454
    public function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) {
455
        return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
456
    }
457
458
    public function pExpr_ShellExec(Expr\ShellExec $node) {
459
        return '`' . $this->pEncapsList($node->parts, '`') . '`';
460
    }
461
462
    public function pExpr_Closure(Expr\Closure $node) {
463
        return ($node->static ? 'static ' : '')
464
             . 'function ' . ($node->byRef ? '&' : '')
465
             . '(' . $this->pCommaSeparated($node->params) . ')'
466
             . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')': '')
467
             . (null !== $node->returnType ? ' : ' . $this->pType($node->returnType) : '')
468
             . ' {' . $this->pStmts($node->stmts) . "\n" . '}';
469
    }
470
471
    public function pExpr_ClosureUse(Expr\ClosureUse $node) {
472
        return ($node->byRef ? '&' : '') . '$' . $node->var;
473
    }
474
475
    public function pExpr_New(Expr\New_ $node) {
476
        if ($node->class instanceof Stmt\Class_) {
477
            $args = $node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '';
478
            return 'new ' . $this->pClassCommon($node->class, $args);
479
        }
480
        return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
481
    }
482
483
    public function pExpr_Clone(Expr\Clone_ $node) {
484
        return 'clone ' . $this->p($node->expr);
485
    }
486
487
    public function pExpr_Ternary(Expr\Ternary $node) {
488
        // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
489
        // this is okay because the part between ? and : never needs parentheses.
490
        return $this->pInfixOp('Expr_Ternary',
491
            $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else
492
        );
493
    }
494
495
    public function pExpr_Exit(Expr\Exit_ $node) {
496
        return 'die' . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
497
    }
498
499
    public function pExpr_Yield(Expr\Yield_ $node) {
500
        if ($node->value === null) {
501
            return 'yield';
502
        } else {
503
            // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary
504
            return '(yield '
505
                 . ($node->key !== null ? $this->p($node->key) . ' => ' : '')
506
                 . $this->p($node->value)
507
                 . ')';
508
        }
509
    }
510
511
    // Declarations
512
513
    public function pStmt_Namespace(Stmt\Namespace_ $node) {
514
        if ($this->canUseSemicolonNamespaces) {
515
            return 'namespace ' . $this->p($node->name) . ';' . "\n" . $this->pStmts($node->stmts, false);
516
        } else {
517
            return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
518
                 . ' {' . $this->pStmts($node->stmts) . "\n" . '}';
519
        }
520
    }
521
522
    public function pStmt_Use(Stmt\Use_ $node) {
523
        return 'use ' . $this->pUseType($node->type)
524
             . $this->pCommaSeparated($node->uses) . ';';
525
    }
526
527
    public function pStmt_GroupUse(Stmt\GroupUse $node) {
528
        return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
529
             . '\{' . $this->pCommaSeparated($node->uses) . '};';
530
    }
531
532
    public function pStmt_UseUse(Stmt\UseUse $node) {
533
        return $this->pUseType($node->type) . $this->p($node->name)
534
             . ($node->name->getLast() !== $node->alias ? ' as ' . $node->alias : '');
535
    }
536
537
    private function pUseType($type) {
538
        return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
539
            : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
540
    }
541
542
    public function pStmt_Interface(Stmt\Interface_ $node) {
543
        return 'interface ' . $node->name
544
             . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
545
             . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
546
    }
547
548
    public function pStmt_Class(Stmt\Class_ $node) {
549
        return $this->pClassCommon($node, ' ' . $node->name);
550
    }
551
552
    public function pStmt_Trait(Stmt\Trait_ $node) {
553
        return 'trait ' . $node->name
554
             . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
555
    }
556
557
    public function pStmt_TraitUse(Stmt\TraitUse $node) {
558
        return 'use ' . $this->pCommaSeparated($node->traits)
559
             . (empty($node->adaptations)
560
                ? ';'
561
                : ' {' . $this->pStmts($node->adaptations) . "\n" . '}');
562
    }
563
564
    public function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) {
565
        return $this->p($node->trait) . '::' . $node->method
566
             . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
567
    }
568
569
    public function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) {
570
        return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
571
             . $node->method . ' as'
572
             . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
573
             . (null !== $node->newName     ? ' ' . $node->newName                        : '')
574
             . ';';
575
    }
576
577
    public function pStmt_Property(Stmt\Property $node) {
578
        return (0 === $node->type ? 'var ' : $this->pModifiers($node->type)) . $this->pCommaSeparated($node->props) . ';';
579
    }
580
581
    public function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
582
        return '$' . $node->name
583
             . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
584
    }
585
586
    public function pStmt_ClassMethod(Stmt\ClassMethod $node) {
587
        return $this->pModifiers($node->type)
588
             . 'function ' . ($node->byRef ? '&' : '') . $node->name
589
             . '(' . $this->pCommaSeparated($node->params) . ')'
590
             . (null !== $node->returnType ? ' : ' . $this->pType($node->returnType) : '')
591
             . (null !== $node->stmts
592
                ? "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}'
593
                : ';');
594
    }
595
596
    public function pStmt_ClassConst(Stmt\ClassConst $node) {
597
        return 'const ' . $this->pCommaSeparated($node->consts) . ';';
598
    }
599
600
    public function pStmt_Function(Stmt\Function_ $node) {
601
        return 'function ' . ($node->byRef ? '&' : '') . $node->name
602
             . '(' . $this->pCommaSeparated($node->params) . ')'
603
             . (null !== $node->returnType ? ' : ' . $this->pType($node->returnType) : '')
604
             . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
605
    }
606
607
    public function pStmt_Const(Stmt\Const_ $node) {
608
        return 'const ' . $this->pCommaSeparated($node->consts) . ';';
609
    }
610
611
    public function pStmt_Declare(Stmt\Declare_ $node) {
612
        return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
613
             . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . "\n" . '}' : ';');
614
    }
615
616
    public function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
617
        return $node->key . '=' . $this->p($node->value);
618
    }
619
620
    // Control flow
621
622
    public function pStmt_If(Stmt\If_ $node) {
623
        return 'if (' . $this->p($node->cond) . ') {'
624
             . $this->pStmts($node->stmts) . "\n" . '}'
625
             . $this->pImplode($node->elseifs)
626
             . (null !== $node->else ? $this->p($node->else) : '');
627
    }
628
629
    public function pStmt_ElseIf(Stmt\ElseIf_ $node) {
630
        return ' elseif (' . $this->p($node->cond) . ') {'
631
             . $this->pStmts($node->stmts) . "\n" . '}';
632
    }
633
634
    public function pStmt_Else(Stmt\Else_ $node) {
635
        return ' else {' . $this->pStmts($node->stmts) . "\n" . '}';
636
    }
637
638 View Code Duplication
    public function pStmt_For(Stmt\For_ $node) {
639
        return 'for ('
640
             . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
641
             . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
642
             . $this->pCommaSeparated($node->loop)
643
             . ') {' . $this->pStmts($node->stmts) . "\n" . '}';
644
    }
645
646
    public function pStmt_Foreach(Stmt\Foreach_ $node) {
647
        return 'foreach (' . $this->p($node->expr) . ' as '
648
             . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
649
             . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
650
             . $this->pStmts($node->stmts) . "\n" . '}';
651
    }
652
653
    public function pStmt_While(Stmt\While_ $node) {
654
        return 'while (' . $this->p($node->cond) . ') {'
655
             . $this->pStmts($node->stmts) . "\n" . '}';
656
    }
657
658
    public function pStmt_Do(Stmt\Do_ $node) {
659
        return 'do {' . $this->pStmts($node->stmts) . "\n"
660
             . '} while (' . $this->p($node->cond) . ');';
661
    }
662
663
    public function pStmt_Switch(Stmt\Switch_ $node) {
664
        return 'switch (' . $this->p($node->cond) . ') {'
665
             . $this->pStmts($node->cases) . "\n" . '}';
666
    }
667
668
    public function pStmt_Case(Stmt\Case_ $node) {
669
        return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
670
             . $this->pStmts($node->stmts);
671
    }
672
673
    public function pStmt_TryCatch(Stmt\TryCatch $node) {
674
        return 'try {' . $this->pStmts($node->stmts) . "\n" . '}'
675
             . $this->pImplode($node->catches)
676
             . ($node->finallyStmts !== null
677
                ? ' finally {' . $this->pStmts($node->finallyStmts) . "\n" . '}'
678
                : '');
679
    }
680
681
    public function pStmt_Catch(Stmt\Catch_ $node) {
682
        return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
683
             . $this->pStmts($node->stmts) . "\n" . '}';
684
    }
685
686
    public function pStmt_Break(Stmt\Break_ $node) {
687
        return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
688
    }
689
690
    public function pStmt_Continue(Stmt\Continue_ $node) {
691
        return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
692
    }
693
694
    public function pStmt_Return(Stmt\Return_ $node) {
695
        return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
696
    }
697
698
    public function pStmt_Throw(Stmt\Throw_ $node) {
699
        return 'throw ' . $this->p($node->expr) . ';';
700
    }
701
702
    public function pStmt_Label(Stmt\Label $node) {
703
        return $node->name . ':';
704
    }
705
706
    public function pStmt_Goto(Stmt\Goto_ $node) {
707
        return 'goto ' . $node->name . ';';
708
    }
709
710
    // Other
711
712
    public function pStmt_Echo(Stmt\Echo_ $node) {
713
        return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
714
    }
715
716
    public function pStmt_Static(Stmt\Static_ $node) {
717
        return 'static ' . $this->pCommaSeparated($node->vars) . ';';
718
    }
719
720
    public function pStmt_Global(Stmt\Global_ $node) {
721
        return 'global ' . $this->pCommaSeparated($node->vars) . ';';
722
    }
723
724
    public function pStmt_StaticVar(Stmt\StaticVar $node) {
725
        return '$' . $node->name
726
             . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
727
    }
728
729
    public function pStmt_Unset(Stmt\Unset_ $node) {
730
        return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
731
    }
732
733
    public function pStmt_InlineHTML(Stmt\InlineHTML $node) {
734
        return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
735
    }
736
737
    public function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {
738
        return '__halt_compiler();' . $node->remaining;
739
    }
740
741
    // Helpers
742
743
    protected function pType($node) {
744
        return is_string($node) ? $node : $this->p($node);
745
    }
746
747 View Code Duplication
    protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) {
748
        return $this->pModifiers($node->type)
749
        . 'class' . $afterClassToken
750
        . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
751
        . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
752
        . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
753
    }
754
755
    protected function pObjectProperty($node) {
756
        if ($node instanceof Expr) {
757
            return '{' . $this->p($node) . '}';
758
        } else {
759
            return $node;
760
        }
761
    }
762
763
    protected function pModifiers($modifiers) {
764
        return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC    ? 'public '    : '')
765
             . ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '')
766
             . ($modifiers & Stmt\Class_::MODIFIER_PRIVATE   ? 'private '   : '')
767
             . ($modifiers & Stmt\Class_::MODIFIER_STATIC    ? 'static '    : '')
768
             . ($modifiers & Stmt\Class_::MODIFIER_ABSTRACT  ? 'abstract '  : '')
769
             . ($modifiers & Stmt\Class_::MODIFIER_FINAL     ? 'final '     : '');
770
    }
771
772
    protected function pEncapsList(array $encapsList, $quote) {
773
        $return = '';
774
        foreach ($encapsList as $element) {
775
            if ($element instanceof Scalar\EncapsedStringPart) {
776
                $return .= addcslashes($element->value, "\n\r\t\f\v$" . $quote . "\\");
777
            } else {
778
                $return .= '{' . $this->p($element) . '}';
779
            }
780
        }
781
782
        return $return;
783
    }
784
785
    protected function pDereferenceLhs(Node $node) {
786
        if ($node instanceof Expr\Variable
787
            || $node instanceof Name
788
            || $node instanceof Expr\ArrayDimFetch
789
            || $node instanceof Expr\PropertyFetch
790
            || $node instanceof Expr\StaticPropertyFetch
791
            || $node instanceof Expr\FuncCall
792
            || $node instanceof Expr\MethodCall
793
            || $node instanceof Expr\StaticCall
794
            || $node instanceof Expr\Array_
795
            || $node instanceof Scalar\String_
796
            || $node instanceof Expr\ConstFetch
797
            || $node instanceof Expr\ClassConstFetch
798
        ) {
799
            return $this->p($node);
800
        } else  {
801
            return '(' . $this->p($node) . ')';
802
        }
803
    }
804
805
    protected function pCallLhs(Node $node) {
806
        if ($node instanceof Name
807
            || $node instanceof Expr\Variable
808
            || $node instanceof Expr\ArrayDimFetch
809
            || $node instanceof Expr\FuncCall
810
            || $node instanceof Expr\MethodCall
811
            || $node instanceof Expr\StaticCall
812
            || $node instanceof Expr\Array_
813
        ) {
814
            return $this->p($node);
815
        } else  {
816
            return '(' . $this->p($node) . ')';
817
        }
818
    }
819
}
820