6a9ee2622b92ffbfba2c39cf9f6ba633d7c46fc0
[yaffs-website] / vendor / nikic / php-parser / UPGRADE-1.0.md
1 Upgrading from PHP-Parser 0.9 to 1.0
2 ====================================
3
4 ### PHP version requirements
5
6 PHP-Parser now requires PHP 5.3 or newer to run. It is however still possible to *parse* PHP 5.2 source code, while
7 running on a newer version.
8
9 ### Move to namespaced names
10
11 The library has been moved to use namespaces with the `PhpParser` vendor prefix. However, the old names using
12 underscores are still available as aliases, as such most code should continue running on the new version without
13 further changes.
14
15 Old (still works, but discouraged):
16
17 ```php
18 $parser = new \PHPParser_Parser(new \PHPParser_Lexer_Emulative);
19 $prettyPrinter = new \PHPParser_PrettyPrinter_Default;
20 ```
21
22 New:
23
24 ```php
25 $parser = new \PhpParser\Parser(new PhpParser\Lexer\Emulative);
26 $prettyPrinter = new \PhpParser\PrettyPrinter\Standard;
27 ```
28
29 Note that the `PHPParser` prefix was changed to `PhpParser`. While PHP class names are technically case-insensitive,
30 the autoloader will not be able to load `PHPParser\Parser` or other case variants.
31
32 Due to conflicts with reserved keywords, some class names now end with an underscore, e.g. `PHPParser_Node_Stmt_Class`
33 is now `PhpParser\Node\Stmt\Class_`. (But as usual, the old name is still available.)
34
35 ### Changes to `Node::getType()`
36
37 The `Node::getType()` method continues to return names using underscores instead of namespace separators and also does
38 not contain the trailing underscore that may be present in the class name. As such its output will not change in many
39 cases.
40
41 However, some node classes have been moved to a different namespace or renamed, which will result in a different
42 `Node::getType()` output:
43
44 ```
45 Expr_AssignBitwiseAnd => Expr_AssignOp_BitwiseAnd
46 Expr_AssignBitwiseOr  => Expr_AssignOp_BitwiseOr
47 Expr_AssignBitwiseXor => Expr_AssignOp_BitwiseXor
48 Expr_AssignConcat     => Expr_AssignOp_Concat
49 Expr_AssignDiv        => Expr_AssignOp_Div
50 Expr_AssignMinus      => Expr_AssignOp_Minus
51 Expr_AssignMod        => Expr_AssignOp_Mod
52 Expr_AssignMul        => Expr_AssignOp_Mul
53 Expr_AssignPlus       => Expr_AssignOp_Plus
54 Expr_AssignShiftLeft  => Expr_AssignOp_ShiftLeft
55 Expr_AssignShiftRight => Expr_AssignOp_ShiftRight
56
57 Expr_BitwiseAnd       => Expr_BinaryOp_BitwiseAnd
58 Expr_BitwiseOr        => Expr_BinaryOp_BitwiseOr
59 Expr_BitwiseXor       => Expr_BinaryOp_BitwiseXor
60 Expr_BooleanAnd       => Expr_BinaryOp_BooleanAnd
61 Expr_BooleanOr        => Expr_BinaryOp_BooleanOr
62 Expr_Concat           => Expr_BinaryOp_Concat
63 Expr_Div              => Expr_BinaryOp_Div
64 Expr_Equal            => Expr_BinaryOp_Equal
65 Expr_Greater          => Expr_BinaryOp_Greater
66 Expr_GreaterOrEqual   => Expr_BinaryOp_GreaterOrEqual
67 Expr_Identical        => Expr_BinaryOp_Identical
68 Expr_LogicalAnd       => Expr_BinaryOp_LogicalAnd
69 Expr_LogicalOr        => Expr_BinaryOp_LogicalOr
70 Expr_LogicalXor       => Expr_BinaryOp_LogicalXor
71 Expr_Minus            => Expr_BinaryOp_Minus
72 Expr_Mod              => Expr_BinaryOp_Mod
73 Expr_Mul              => Expr_BinaryOp_Mul
74 Expr_NotEqual         => Expr_BinaryOp_NotEqual
75 Expr_NotIdentical     => Expr_BinaryOp_NotIdentical
76 Expr_Plus             => Expr_BinaryOp_Plus
77 Expr_ShiftLeft        => Expr_BinaryOp_ShiftLeft
78 Expr_ShiftRight       => Expr_BinaryOp_ShiftRight
79 Expr_Smaller          => Expr_BinaryOp_Smaller
80 Expr_SmallerOrEqual   => Expr_BinaryOp_SmallerOrEqual
81
82 Scalar_ClassConst     => Scalar_MagicConst_Class
83 Scalar_DirConst       => Scalar_MagicConst_Dir
84 Scalar_FileConst      => Scalar_MagicConst_File
85 Scalar_FuncConst      => Scalar_MagicConst_Function
86 Scalar_LineConst      => Scalar_MagicConst_Line
87 Scalar_MethodConst    => Scalar_MagicConst_Method
88 Scalar_NSConst        => Scalar_MagicConst_Namespace
89 Scalar_TraitConst     => Scalar_MagicConst_Trait
90 ```
91
92 These changes may affect custom pretty printers and code comparing the return value of `Node::getType()` to specific
93 strings.
94
95 ### Miscellaneous
96
97   * The classes `Template` and `TemplateLoader` have been removed. You should use some other [code generation][code_gen]
98     project built on top of PHP-Parser instead.
99
100   * The `PrettyPrinterAbstract::pStmts()` method now emits a leading newline if the statement list is not empty.
101     Custom pretty printers should remove the explicit newline before `pStmts()` calls.
102
103     Old:
104
105     ```php
106     public function pStmt_Trait(PHPParser_Node_Stmt_Trait $node) {
107         return 'trait ' . $node->name
108              . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
109     }
110     ```
111
112     New:
113
114     ```php
115     public function pStmt_Trait(Stmt\Trait_ $node) {
116         return 'trait ' . $node->name
117              . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
118     }
119     ```
120
121   [code_gen]: https://github.com/nikic/PHP-Parser/wiki/Projects-using-the-PHP-Parser#code-generation