| Methods |
public
|
__construct(array $options = [])
Creates a pretty printer instance using the given options.
Creates a pretty printer instance using the given options.
Supported options:
- bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
syntax, if the node does not specify a format.
Parameters
| $options |
Dictionary of formatting options
|
|
#
|
protected
|
resetState()
Reset pretty printing state.
Reset pretty printing state.
|
#
|
protected
|
setIndentLevel(int $level)
Set indentation level
Parameters
| $level |
Level in number of spaces
|
Overriden by
|
#
|
protected
|
indent()
Increase indentation level.
Increase indentation level.
|
#
|
protected
|
outdent()
Decrease indentation level.
Decrease indentation level.
|
#
|
public
|
prettyPrint(Node[] $stmts): string
Pretty prints an array of statements.
Pretty prints an array of statements.
Parameters
| $stmts |
Array of statements
|
Returns
Pretty printed statements
|
#
|
public
|
prettyPrintExpr(Expr $node): string
Pretty prints an expression.
Pretty prints an expression.
Parameters
Returns
|
#
|
public
|
prettyPrintFile(Node[] $stmts): string
Pretty prints a file of statements (includes the opening <?php tag if it is required).
Pretty prints a file of statements (includes the opening <?php tag if it is required).
Parameters
| $stmts |
Array of statements
|
Returns
Pretty printed statements
|
#
|
protected
|
preprocessNodes(Node[] $nodes)
Preprocesses the top-level nodes to initialize pretty printer state.
Preprocesses the top-level nodes to initialize pretty printer state.
Parameters
|
#
|
protected
|
handleMagicTokens(string $str): string
Handles (and removes) no-indent and doc-string-end tokens.
Handles (and removes) no-indent and doc-string-end tokens.
|
#
|
protected
|
pStmts(Node[] $nodes, bool $indent = true): string
Pretty prints an array of nodes (statements) and indents them optionally.
Pretty prints an array of nodes (statements) and indents them optionally.
Parameters
| $nodes |
Array of nodes
|
| $indent |
Whether to indent the printed nodes
|
Returns
Pretty printed statements
|
#
|
protected
|
pInfixOp(string $class, Node $leftNode, string $operatorString, Node $rightNode): string
Pretty-print an infix operation while taking precedence into account.
Pretty-print an infix operation while taking precedence into account.
Parameters
| $class |
Node class of operator
|
| $leftNode |
Left-hand side node
|
| $operatorString |
String representation of the operator
|
| $rightNode |
Right-hand side node
|
Returns
Pretty printed infix operation
|
#
|
protected
|
pPrefixOp(string $class, string $operatorString, Node $node): string
Pretty-print a prefix operation while taking precedence into account.
Pretty-print a prefix operation while taking precedence into account.
Parameters
| $class |
Node class of operator
|
| $operatorString |
String representation of the operator
|
| $node |
Node
|
Returns
Pretty printed prefix operation
|
#
|
protected
|
pPostfixOp(string $class, Node $node, string $operatorString): string
Pretty-print a postfix operation while taking precedence into account.
Pretty-print a postfix operation while taking precedence into account.
Parameters
| $class |
Node class of operator
|
| $node |
Node
|
| $operatorString |
String representation of the operator
|
Returns
Pretty printed postfix operation
|
#
|
protected
|
pPrec(Node $node, int $parentPrecedence, int $parentAssociativity, int $childPosition): string
Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
Parameters
| $node |
Node to pretty print
|
| $parentPrecedence |
Precedence of the parent operator
|
| $parentAssociativity |
Associativity of parent operator
(-1 is left, 0 is nonassoc, 1 is right)
|
| $childPosition |
Position of the node relative to the operator
(-1 is left, 1 is right)
|
Returns
|
#
|
protected
|
pImplode(Node[] $nodes, string $glue = ''): string
Pretty prints an array of nodes and implodes the printed values.
Pretty prints an array of nodes and implodes the printed values.
Parameters
| $nodes |
Array of Nodes to be printed
|
| $glue |
Character to implode with
|
Returns
Imploded pretty printed nodes
|
#
|
protected
|
pCommaSeparated(Node[] $nodes): string
Pretty prints an array of nodes and implodes the printed values with commas.
Pretty prints an array of nodes and implodes the printed values with commas.
Parameters
| $nodes |
Array of Nodes to be printed
|
Returns
Comma separated pretty printed nodes
|
#
|
protected
|
pCommaSeparatedMultiline(Node[] $nodes, bool $trailingComma): string
Pretty prints a comma-separated list of nodes in multiline style, including comments.
Pretty prints a comma-separated list of nodes in multiline style, including comments.
The result includes a leading newline and one level of indentation (same as pStmts).
Parameters
| $nodes |
Array of Nodes to be printed
|
| $trailingComma |
Whether to use a trailing comma
|
Returns
Comma separated pretty printed nodes in multiline style
|
#
|
protected
|
pComments(Comment[] $comments): string
Prints reformatted text of the passed comments.
Prints reformatted text of the passed comments.
Parameters
| $comments |
List of comments
|
Returns
Reformatted text of comments
|
#
|
public
|
printFormatPreserving(Node[] $stmts, Node[] $origStmts, array $origTokens): string
Perform a format-preserving pretty print of an AST.
Perform a format-preserving pretty print of an AST.
The format preservation is best effort. For some changes to the AST the formatting will not
be preserved (at least not locally).
In order to use this method a number of prerequisites must be satisfied:
- The startTokenPos and endTokenPos attributes in the lexer must be enabled.
- The CloningVisitor must be run on the AST prior to modification.
- The original tokens must be provided, using the getTokens() method on the lexer.
Parameters
| $stmts |
Modified AST with links to original AST
|
| $origStmts |
Original AST with token offset information
|
| $origTokens |
Tokens of the original code
|
|
#
|
protected
|
pFallback(Node $node)
|
#
|
protected
|
p(Node $node, bool $parentFormatPreserved = false): string
Pretty prints a node.
Pretty prints a node.
This method also handles formatting preservation for nodes.
Parameters
| $node |
Node to be pretty printed
|
| $parentFormatPreserved |
Whether parent node has preserved formatting
|
Returns
|
#
|
protected
|
pArray(
array $nodes,
array $origNodes,
int &$pos,
int $indentAdjustment,
string $parentNodeType,
string $subNodeName,
null|int $fixup,
): null|string
Perform a format-preserving pretty print of an array.
Perform a format-preserving pretty print of an array.
Parameters
| $nodes |
New nodes
|
| $origNodes |
Original nodes
|
| $pos |
Current token position (updated by reference)
|
| $indentAdjustment |
Adjustment for indentation
|
| $parentNodeType |
Type of the containing node.
|
| $subNodeName |
Name of array subnode.
|
| $fixup |
Fixup information for array item nodes
|
Returns
Result of pretty print or null if cannot preserve formatting
|
#
|
protected
|
pFixup(int $fixup, Node $subNode, string|null $parentClass, int $subStartPos, int $subEndPos): string
Print node with fixups.
Print node with fixups.
Fixups here refer to the addition of extra parentheses, braces or other characters, that
are required to preserve program semantics in a certain context (e.g. to maintain precedence
or because only certain expressions are allowed in certain places).
Parameters
| $fixup |
Fixup type
|
| $subNode |
Subnode to print
|
| $parentClass |
Class of parent node
|
| $subStartPos |
Original start pos of subnode
|
| $subEndPos |
Original end pos of subnode
|
Returns
Result of fixed-up print of subnode
|
#
|
protected
|
safeAppend(string &$str, string $append)
Appends to a string, ensuring whitespace between label characters.
Appends to a string, ensuring whitespace between label characters.
Example: "echo" and "$x" result in "echo$x", but "echo" and "x" result in "echo x".
Without safeAppend the result would be "echox", which does not preserve semantics.
|
#
|
protected
|
callLhsRequiresParens(Node $node): bool
Determines whether the LHS of a call must be wrapped in parenthesis.
Determines whether the LHS of a call must be wrapped in parenthesis.
Parameters
Returns
Whether parentheses are required
|
#
|
protected
|
dereferenceLhsRequiresParens(Node $node): bool
Determines whether the LHS of an array/object operation must be wrapped in parentheses.
Determines whether the LHS of an array/object operation must be wrapped in parentheses.
Parameters
| $node |
LHS of dereferencing operation
|
Returns
Whether parentheses are required
|
#
|
protected
|
staticDereferenceLhsRequiresParens(Node $node): bool
Determines whether the LHS of a static operation must be wrapped in parentheses.
Determines whether the LHS of a static operation must be wrapped in parentheses.
Parameters
| $node |
LHS of dereferencing operation
|
Returns
Whether parentheses are required
|
#
|
protected
|
newOperandRequiresParens(Node $node): bool
Determines whether an expression used in "new" or "instanceof" requires parentheses.
Determines whether an expression used in "new" or "instanceof" requires parentheses.
Parameters
| $node |
New or instanceof operand
|
Returns
Whether parentheses are required
|
#
|
protected
|
pModifiers(int $modifiers): string
Print modifiers, including trailing whitespace.
Print modifiers, including trailing whitespace.
Parameters
| $modifiers |
Modifier mask to print
|
Returns
|
#
|
protected
|
isMultiline((Node|null)[] $nodes): bool
Determine whether a list of nodes uses multiline formatting.
Determine whether a list of nodes uses multiline formatting.
Parameters
Returns
Whether multiline formatting is used
|
#
|
protected
|
initializeLabelCharMap()
Lazily initializes label char map.
Lazily initializes label char map.
The label char map determines whether a certain character may occur in a label.
|
#
|
protected
|
initializeNodeListDiffer()
Lazily initializes node list differ.
Lazily initializes node list differ.
The node list differ is used to determine differences between two array subnodes.
|
#
|
protected
|
initializeFixupMap()
Lazily initializes fixup map.
Lazily initializes fixup map.
The fixup map is used to determine whether a certain subnode of a certain node may require
some kind of "fixup" operation, e.g. the addition of parenthesis or braces.
|
#
|
protected
|
initializeRemovalMap()
Lazily initializes the removal map.
Lazily initializes the removal map.
The removal map is used to determine which additional tokens should be removed when a
certain node is replaced by null.
|
#
|
protected
|
initializeInsertionMap()
|
#
|
protected
|
initializeListInsertionMap()
|
#
|
protected
|
initializeEmptyListInsertionMap()
|
#
|
protected
|
initializeModifierChangeMap()
|
#
|
| Properties |
protected
|
|
$precedenceMap = [
Pow::class => [0, 1],
BitwiseNot::class => [10, 1],
PreInc::class => [10, 1],
PreDec::class => [10, 1],
PostInc::class => [10, -1],
PostDec::class => [10, -1],
UnaryPlus::class => [10, 1],
UnaryMinus::class => [10, 1],
Int_::class => [10, 1],
Double::class => [10, 1],
String_::class => [10, 1],
Array_::class => [10, 1],
Object_::class => [10, 1],
Bool_::class => [10, 1],
Unset_::class => [10, 1],
ErrorSuppress::class => [10, 1],
Instanceof_::class => [20, 0],
BooleanNot::class => [30, 1],
Mul::class => [40, -1],
Div::class => [40, -1],
Mod::class => [40, -1],
Plus::class => [50, -1],
Minus::class => [50, -1],
Concat::class => [50, -1],
ShiftLeft::class => [60, -1],
ShiftRight::class => [60, -1],
Smaller::class => [70, 0],
SmallerOrEqual::class => [70, 0],
Greater::class => [70, 0],
GreaterOrEqual::class => [70, 0],
Equal::class => [80, 0],
NotEqual::class => [80, 0],
Identical::class => [80, 0],
NotIdentical::class => [80, 0],
Spaceship::class => [80, 0],
BitwiseAnd::class => [90, -1],
BitwiseXor::class => [100, -1],
BitwiseOr::class => [110, -1],
BooleanAnd::class => [120, -1],
BooleanOr::class => [130, -1],
Coalesce::class => [140, 1],
Ternary::class => [150, 0],
Assign::class => [160, 1],
AssignRef::class => [160, 1],
Plus::class => [160, 1],
Minus::class => [160, 1],
Mul::class => [160, 1],
Div::class => [160, 1],
Concat::class => [160, 1],
Mod::class => [160, 1],
BitwiseAnd::class => [160, 1],
BitwiseOr::class => [160, 1],
BitwiseXor::class => [160, 1],
ShiftLeft::class => [160, 1],
ShiftRight::class => [160, 1],
Pow::class => [160, 1],
Coalesce::class => [160, 1],
YieldFrom::class => [165, 1],
Print_::class => [168, 1],
LogicalAnd::class => [170, -1],
LogicalXor::class => [180, -1],
LogicalOr::class => [190, -1],
Include_::class => [200, -1],
]
|
#
|
protected
|
int
|
$indentLevel
Current indentation level.
Current indentation level.
|
#
|
protected
|
string
|
$nl
Newline including current indentation.
Newline including current indentation.
|
#
|
protected
|
string
|
$docStringEndToken
Token placed at end of doc string to ensure it is followed by a newline.
Token placed at end of doc string to ensure it is followed by a newline.
|
#
|
protected
|
bool
|
$canUseSemicolonNamespaces
Whether semicolon namespaces can be used (i.e. no global namespace is used)
Whether semicolon namespaces can be used (i.e. no global namespace is used)
|
#
|
protected
|
array
|
$options
Pretty printer options
|
#
|
protected
|
TokenStream
|
$origTokens
Original tokens for use in format-preserving pretty print
Original tokens for use in format-preserving pretty print
|
#
|
protected
|
Differ
|
$nodeListDiffer
Differ for node lists
|
#
|
protected
|
bool[]
|
$labelCharMap
Map determining whether a certain character is a label character
Map determining whether a certain character is a label character
|
#
|
protected
|
int[][]
|
$fixupMap
Map from token classes and subnode names to FIXUP_* constants. This is used
during format-preserving…
Map from token classes and subnode names to FIXUP_* constants. This is used
during format-preserving prints to place additional parens/braces if necessary.
|
#
|
protected
|
int[][]
|
$removalMap
Map from "{$node->getType()}->{$subNode}" to ['left' => $l, 'right' => $r],
where $l and $r specify the…
Map from "{$node->getType()}->{$subNode}" to ['left' => $l, 'right' => $r],
where $l and $r specify the token type that needs to be stripped when removing
this node.
|
#
|
protected
|
mixed[]
|
$insertionMap
Map from "{$node->getType()}->{$subNode}" to [$find, $beforeToken, $extraLeft, $extraRight].
$find is an…
Map from "{$node->getType()}->{$subNode}" to [$find, $beforeToken, $extraLeft, $extraRight].
$find is an optional token after which the insertion occurs. $extraLeft/Right
are optionally added before/after the main insertions.
|
#
|
protected
|
string[]
|
$listInsertionMap
Map From "{$node->getType()}->{$subNode}" to string that should be inserted
between elements of this list…
Map From "{$node->getType()}->{$subNode}" to string that should be inserted
between elements of this list subnode.
|
#
|
protected
|
|
$emptyListInsertionMap
|
#
|
protected
|
int[]
|
$modifierChangeMap
Map from "{$node->getType()}->{$subNode}" to token before which the modifiers
should be reprinted.
Map from "{$node->getType()}->{$subNode}" to token before which the modifiers
should be reprinted.
|
#
|