Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Node\Expr;
|
Chris@0
|
6 use PhpParser\Node\Stmt;
|
Chris@0
|
7
|
Chris@0
|
8 abstract class PrettyPrinterAbstract
|
Chris@0
|
9 {
|
Chris@0
|
10 protected $precedenceMap = array(
|
Chris@0
|
11 // [precedence, associativity] where for the latter -1 is %left, 0 is %nonassoc and 1 is %right
|
Chris@0
|
12 'Expr_BinaryOp_Pow' => array( 0, 1),
|
Chris@0
|
13 'Expr_BitwiseNot' => array( 10, 1),
|
Chris@0
|
14 'Expr_PreInc' => array( 10, 1),
|
Chris@0
|
15 'Expr_PreDec' => array( 10, 1),
|
Chris@0
|
16 'Expr_PostInc' => array( 10, -1),
|
Chris@0
|
17 'Expr_PostDec' => array( 10, -1),
|
Chris@0
|
18 'Expr_UnaryPlus' => array( 10, 1),
|
Chris@0
|
19 'Expr_UnaryMinus' => array( 10, 1),
|
Chris@0
|
20 'Expr_Cast_Int' => array( 10, 1),
|
Chris@0
|
21 'Expr_Cast_Double' => array( 10, 1),
|
Chris@0
|
22 'Expr_Cast_String' => array( 10, 1),
|
Chris@0
|
23 'Expr_Cast_Array' => array( 10, 1),
|
Chris@0
|
24 'Expr_Cast_Object' => array( 10, 1),
|
Chris@0
|
25 'Expr_Cast_Bool' => array( 10, 1),
|
Chris@0
|
26 'Expr_Cast_Unset' => array( 10, 1),
|
Chris@0
|
27 'Expr_ErrorSuppress' => array( 10, 1),
|
Chris@0
|
28 'Expr_Instanceof' => array( 20, 0),
|
Chris@0
|
29 'Expr_BooleanNot' => array( 30, 1),
|
Chris@0
|
30 'Expr_BinaryOp_Mul' => array( 40, -1),
|
Chris@0
|
31 'Expr_BinaryOp_Div' => array( 40, -1),
|
Chris@0
|
32 'Expr_BinaryOp_Mod' => array( 40, -1),
|
Chris@0
|
33 'Expr_BinaryOp_Plus' => array( 50, -1),
|
Chris@0
|
34 'Expr_BinaryOp_Minus' => array( 50, -1),
|
Chris@0
|
35 'Expr_BinaryOp_Concat' => array( 50, -1),
|
Chris@0
|
36 'Expr_BinaryOp_ShiftLeft' => array( 60, -1),
|
Chris@0
|
37 'Expr_BinaryOp_ShiftRight' => array( 60, -1),
|
Chris@0
|
38 'Expr_BinaryOp_Smaller' => array( 70, 0),
|
Chris@0
|
39 'Expr_BinaryOp_SmallerOrEqual' => array( 70, 0),
|
Chris@0
|
40 'Expr_BinaryOp_Greater' => array( 70, 0),
|
Chris@0
|
41 'Expr_BinaryOp_GreaterOrEqual' => array( 70, 0),
|
Chris@0
|
42 'Expr_BinaryOp_Equal' => array( 80, 0),
|
Chris@0
|
43 'Expr_BinaryOp_NotEqual' => array( 80, 0),
|
Chris@0
|
44 'Expr_BinaryOp_Identical' => array( 80, 0),
|
Chris@0
|
45 'Expr_BinaryOp_NotIdentical' => array( 80, 0),
|
Chris@0
|
46 'Expr_BinaryOp_Spaceship' => array( 80, 0),
|
Chris@0
|
47 'Expr_BinaryOp_BitwiseAnd' => array( 90, -1),
|
Chris@0
|
48 'Expr_BinaryOp_BitwiseXor' => array(100, -1),
|
Chris@0
|
49 'Expr_BinaryOp_BitwiseOr' => array(110, -1),
|
Chris@0
|
50 'Expr_BinaryOp_BooleanAnd' => array(120, -1),
|
Chris@0
|
51 'Expr_BinaryOp_BooleanOr' => array(130, -1),
|
Chris@0
|
52 'Expr_BinaryOp_Coalesce' => array(140, 1),
|
Chris@0
|
53 'Expr_Ternary' => array(150, -1),
|
Chris@0
|
54 // parser uses %left for assignments, but they really behave as %right
|
Chris@0
|
55 'Expr_Assign' => array(160, 1),
|
Chris@0
|
56 'Expr_AssignRef' => array(160, 1),
|
Chris@0
|
57 'Expr_AssignOp_Plus' => array(160, 1),
|
Chris@0
|
58 'Expr_AssignOp_Minus' => array(160, 1),
|
Chris@0
|
59 'Expr_AssignOp_Mul' => array(160, 1),
|
Chris@0
|
60 'Expr_AssignOp_Div' => array(160, 1),
|
Chris@0
|
61 'Expr_AssignOp_Concat' => array(160, 1),
|
Chris@0
|
62 'Expr_AssignOp_Mod' => array(160, 1),
|
Chris@0
|
63 'Expr_AssignOp_BitwiseAnd' => array(160, 1),
|
Chris@0
|
64 'Expr_AssignOp_BitwiseOr' => array(160, 1),
|
Chris@0
|
65 'Expr_AssignOp_BitwiseXor' => array(160, 1),
|
Chris@0
|
66 'Expr_AssignOp_ShiftLeft' => array(160, 1),
|
Chris@0
|
67 'Expr_AssignOp_ShiftRight' => array(160, 1),
|
Chris@0
|
68 'Expr_AssignOp_Pow' => array(160, 1),
|
Chris@0
|
69 'Expr_YieldFrom' => array(165, 1),
|
Chris@0
|
70 'Expr_Print' => array(168, 1),
|
Chris@0
|
71 'Expr_BinaryOp_LogicalAnd' => array(170, -1),
|
Chris@0
|
72 'Expr_BinaryOp_LogicalXor' => array(180, -1),
|
Chris@0
|
73 'Expr_BinaryOp_LogicalOr' => array(190, -1),
|
Chris@0
|
74 'Expr_Include' => array(200, -1),
|
Chris@0
|
75 );
|
Chris@0
|
76
|
Chris@0
|
77 protected $noIndentToken;
|
Chris@0
|
78 protected $docStringEndToken;
|
Chris@0
|
79 protected $canUseSemicolonNamespaces;
|
Chris@0
|
80 protected $options;
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Creates a pretty printer instance using the given options.
|
Chris@0
|
84 *
|
Chris@0
|
85 * Supported options:
|
Chris@0
|
86 * * bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
|
Chris@0
|
87 * syntax, if the node does not specify a format.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param array $options Dictionary of formatting options
|
Chris@0
|
90 */
|
Chris@0
|
91 public function __construct(array $options = []) {
|
Chris@0
|
92 $this->noIndentToken = '_NO_INDENT_' . mt_rand();
|
Chris@0
|
93 $this->docStringEndToken = '_DOC_STRING_END_' . mt_rand();
|
Chris@0
|
94
|
Chris@0
|
95 $defaultOptions = ['shortArraySyntax' => false];
|
Chris@0
|
96 $this->options = $options + $defaultOptions;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Pretty prints an array of statements.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @param Node[] $stmts Array of statements
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return string Pretty printed statements
|
Chris@0
|
105 */
|
Chris@0
|
106 public function prettyPrint(array $stmts) {
|
Chris@0
|
107 $this->preprocessNodes($stmts);
|
Chris@0
|
108
|
Chris@0
|
109 return ltrim($this->handleMagicTokens($this->pStmts($stmts, false)));
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Pretty prints an expression.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @param Expr $node Expression node
|
Chris@0
|
116 *
|
Chris@0
|
117 * @return string Pretty printed node
|
Chris@0
|
118 */
|
Chris@0
|
119 public function prettyPrintExpr(Expr $node) {
|
Chris@0
|
120 return $this->handleMagicTokens($this->p($node));
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Pretty prints a file of statements (includes the opening <?php tag if it is required).
|
Chris@0
|
125 *
|
Chris@0
|
126 * @param Node[] $stmts Array of statements
|
Chris@0
|
127 *
|
Chris@0
|
128 * @return string Pretty printed statements
|
Chris@0
|
129 */
|
Chris@0
|
130 public function prettyPrintFile(array $stmts) {
|
Chris@0
|
131 if (!$stmts) {
|
Chris@0
|
132 return "<?php\n\n";
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 $p = "<?php\n\n" . $this->prettyPrint($stmts);
|
Chris@0
|
136
|
Chris@0
|
137 if ($stmts[0] instanceof Stmt\InlineHTML) {
|
Chris@0
|
138 $p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
|
Chris@0
|
139 }
|
Chris@0
|
140 if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
|
Chris@0
|
141 $p = preg_replace('/<\?php$/', '', rtrim($p));
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 return $p;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Preprocesses the top-level nodes to initialize pretty printer state.
|
Chris@0
|
149 *
|
Chris@0
|
150 * @param Node[] $nodes Array of nodes
|
Chris@0
|
151 */
|
Chris@0
|
152 protected function preprocessNodes(array $nodes) {
|
Chris@0
|
153 /* We can use semicolon-namespaces unless there is a global namespace declaration */
|
Chris@0
|
154 $this->canUseSemicolonNamespaces = true;
|
Chris@0
|
155 foreach ($nodes as $node) {
|
Chris@0
|
156 if ($node instanceof Stmt\Namespace_ && null === $node->name) {
|
Chris@0
|
157 $this->canUseSemicolonNamespaces = false;
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 protected function handleMagicTokens($str) {
|
Chris@0
|
163 // Drop no-indent tokens
|
Chris@0
|
164 $str = str_replace($this->noIndentToken, '', $str);
|
Chris@0
|
165
|
Chris@0
|
166 // Replace doc-string-end tokens with nothing or a newline
|
Chris@0
|
167 $str = str_replace($this->docStringEndToken . ";\n", ";\n", $str);
|
Chris@0
|
168 $str = str_replace($this->docStringEndToken, "\n", $str);
|
Chris@0
|
169
|
Chris@0
|
170 return $str;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * Pretty prints an array of nodes (statements) and indents them optionally.
|
Chris@0
|
175 *
|
Chris@0
|
176 * @param Node[] $nodes Array of nodes
|
Chris@0
|
177 * @param bool $indent Whether to indent the printed nodes
|
Chris@0
|
178 *
|
Chris@0
|
179 * @return string Pretty printed statements
|
Chris@0
|
180 */
|
Chris@0
|
181 protected function pStmts(array $nodes, $indent = true) {
|
Chris@0
|
182 $result = '';
|
Chris@0
|
183 foreach ($nodes as $node) {
|
Chris@0
|
184 $comments = $node->getAttribute('comments', array());
|
Chris@0
|
185 if ($comments) {
|
Chris@0
|
186 $result .= "\n" . $this->pComments($comments);
|
Chris@0
|
187 if ($node instanceof Stmt\Nop) {
|
Chris@0
|
188 continue;
|
Chris@0
|
189 }
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 if ($indent) {
|
Chris@0
|
196 return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
Chris@0
|
197 } else {
|
Chris@0
|
198 return $result;
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Pretty prints a node.
|
Chris@0
|
204 *
|
Chris@0
|
205 * @param Node $node Node to be pretty printed
|
Chris@0
|
206 *
|
Chris@0
|
207 * @return string Pretty printed node
|
Chris@0
|
208 */
|
Chris@0
|
209 protected function p(Node $node) {
|
Chris@0
|
210 return $this->{'p' . $node->getType()}($node);
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 protected function pInfixOp($type, Node $leftNode, $operatorString, Node $rightNode) {
|
Chris@0
|
214 list($precedence, $associativity) = $this->precedenceMap[$type];
|
Chris@0
|
215
|
Chris@0
|
216 return $this->pPrec($leftNode, $precedence, $associativity, -1)
|
Chris@0
|
217 . $operatorString
|
Chris@0
|
218 . $this->pPrec($rightNode, $precedence, $associativity, 1);
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 protected function pPrefixOp($type, $operatorString, Node $node) {
|
Chris@0
|
222 list($precedence, $associativity) = $this->precedenceMap[$type];
|
Chris@0
|
223 return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 protected function pPostfixOp($type, Node $node, $operatorString) {
|
Chris@0
|
227 list($precedence, $associativity) = $this->precedenceMap[$type];
|
Chris@0
|
228 return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 /**
|
Chris@0
|
232 * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
|
Chris@0
|
233 *
|
Chris@0
|
234 * @param Node $node Node to pretty print
|
Chris@0
|
235 * @param int $parentPrecedence Precedence of the parent operator
|
Chris@0
|
236 * @param int $parentAssociativity Associativity of parent operator
|
Chris@0
|
237 * (-1 is left, 0 is nonassoc, 1 is right)
|
Chris@0
|
238 * @param int $childPosition Position of the node relative to the operator
|
Chris@0
|
239 * (-1 is left, 1 is right)
|
Chris@0
|
240 *
|
Chris@0
|
241 * @return string The pretty printed node
|
Chris@0
|
242 */
|
Chris@0
|
243 protected function pPrec(Node $node, $parentPrecedence, $parentAssociativity, $childPosition) {
|
Chris@0
|
244 $type = $node->getType();
|
Chris@0
|
245 if (isset($this->precedenceMap[$type])) {
|
Chris@0
|
246 $childPrecedence = $this->precedenceMap[$type][0];
|
Chris@0
|
247 if ($childPrecedence > $parentPrecedence
|
Chris@0
|
248 || ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
|
Chris@0
|
249 ) {
|
Chris@0
|
250 return '(' . $this->p($node) . ')';
|
Chris@0
|
251 }
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 return $this->p($node);
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Pretty prints an array of nodes and implodes the printed values.
|
Chris@0
|
259 *
|
Chris@0
|
260 * @param Node[] $nodes Array of Nodes to be printed
|
Chris@0
|
261 * @param string $glue Character to implode with
|
Chris@0
|
262 *
|
Chris@0
|
263 * @return string Imploded pretty printed nodes
|
Chris@0
|
264 */
|
Chris@0
|
265 protected function pImplode(array $nodes, $glue = '') {
|
Chris@0
|
266 $pNodes = array();
|
Chris@0
|
267 foreach ($nodes as $node) {
|
Chris@0
|
268 if (null === $node) {
|
Chris@0
|
269 $pNodes[] = '';
|
Chris@0
|
270 } else {
|
Chris@0
|
271 $pNodes[] = $this->p($node);
|
Chris@0
|
272 }
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 return implode($glue, $pNodes);
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 /**
|
Chris@0
|
279 * Pretty prints an array of nodes and implodes the printed values with commas.
|
Chris@0
|
280 *
|
Chris@0
|
281 * @param Node[] $nodes Array of Nodes to be printed
|
Chris@0
|
282 *
|
Chris@0
|
283 * @return string Comma separated pretty printed nodes
|
Chris@0
|
284 */
|
Chris@0
|
285 protected function pCommaSeparated(array $nodes) {
|
Chris@0
|
286 return $this->pImplode($nodes, ', ');
|
Chris@0
|
287 }
|
Chris@0
|
288
|
Chris@0
|
289 /**
|
Chris@0
|
290 * Pretty prints a comma-separated list of nodes in multiline style, including comments.
|
Chris@0
|
291 *
|
Chris@0
|
292 * The result includes a leading newline and one level of indentation (same as pStmts).
|
Chris@0
|
293 *
|
Chris@0
|
294 * @param Node[] $nodes Array of Nodes to be printed
|
Chris@0
|
295 * @param bool $trailingComma Whether to use a trailing comma
|
Chris@0
|
296 *
|
Chris@0
|
297 * @return string Comma separated pretty printed nodes in multiline style
|
Chris@0
|
298 */
|
Chris@0
|
299 protected function pCommaSeparatedMultiline(array $nodes, $trailingComma) {
|
Chris@0
|
300 $result = '';
|
Chris@0
|
301 $lastIdx = count($nodes) - 1;
|
Chris@0
|
302 foreach ($nodes as $idx => $node) {
|
Chris@0
|
303 if ($node !== null) {
|
Chris@0
|
304 $comments = $node->getAttribute('comments', array());
|
Chris@0
|
305 if ($comments) {
|
Chris@0
|
306 $result .= "\n" . $this->pComments($comments);
|
Chris@0
|
307 }
|
Chris@0
|
308
|
Chris@0
|
309 $result .= "\n" . $this->p($node);
|
Chris@0
|
310 } else {
|
Chris@0
|
311 $result .= "\n";
|
Chris@0
|
312 }
|
Chris@0
|
313 if ($trailingComma || $idx !== $lastIdx) {
|
Chris@0
|
314 $result .= ',';
|
Chris@0
|
315 }
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 /**
|
Chris@0
|
322 * Signals the pretty printer that a string shall not be indented.
|
Chris@0
|
323 *
|
Chris@0
|
324 * @param string $string Not to be indented string
|
Chris@0
|
325 *
|
Chris@0
|
326 * @return string String marked with $this->noIndentToken's.
|
Chris@0
|
327 */
|
Chris@0
|
328 protected function pNoIndent($string) {
|
Chris@0
|
329 return str_replace("\n", "\n" . $this->noIndentToken, $string);
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * Prints reformatted text of the passed comments.
|
Chris@0
|
334 *
|
Chris@0
|
335 * @param Comment[] $comments List of comments
|
Chris@0
|
336 *
|
Chris@0
|
337 * @return string Reformatted text of comments
|
Chris@0
|
338 */
|
Chris@0
|
339 protected function pComments(array $comments) {
|
Chris@0
|
340 $formattedComments = [];
|
Chris@0
|
341
|
Chris@0
|
342 foreach ($comments as $comment) {
|
Chris@0
|
343 $formattedComments[] = $comment->getReformattedText();
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 return implode("\n", $formattedComments);
|
Chris@0
|
347 }
|
Chris@0
|
348 }
|