Documentation

PrettyPrinterAbstract
in package

Table of Contents

FIXUP_BRACED_NAME  = 4
FIXUP_CALL_LHS  = 2
FIXUP_DEREF_LHS  = 3
FIXUP_ENCAPSED  = 6
FIXUP_NEW  = 7
FIXUP_PREC_LEFT  = 0
FIXUP_PREC_RIGHT  = 1
FIXUP_STATIC_DEREF_LHS  = 8
FIXUP_VAR_BRACED_NAME  = 5
$canUseSemicolonNamespaces  : bool
$docStringEndToken  : string
$emptyListInsertionMap  : mixed
$fixupMap  : array<string|int, array<string|int, int>>
$indentLevel  : int
$insertionMap  : array<string|int, mixed>
$labelCharMap  : array<string|int, bool>
$listInsertionMap  : array<string|int, string>
$modifierChangeMap  : array<string|int, int>
$nl  : string
$nodeListDiffer  : Differ
$options  : array<string|int, mixed>
$origTokens  : TokenStream
$precedenceMap  : mixed
$removalMap  : array<string|int, array<string|int, int>>
__construct()  : mixed
Creates a pretty printer instance using the given options.
prettyPrint()  : string
Pretty prints an array of statements.
prettyPrintExpr()  : string
Pretty prints an expression.
prettyPrintFile()  : string
Pretty prints a file of statements (includes the opening <?php tag if it is required).
printFormatPreserving()  : string
Perform a format-preserving pretty print of an AST.
callLhsRequiresParens()  : bool
Determines whether the LHS of a call must be wrapped in parenthesis.
dereferenceLhsRequiresParens()  : bool
Determines whether the LHS of an array/object operation must be wrapped in parentheses.
handleMagicTokens()  : string
Handles (and removes) no-indent and doc-string-end tokens.
indent()  : mixed
Increase indentation level.
initializeEmptyListInsertionMap()  : mixed
initializeFixupMap()  : mixed
Lazily initializes fixup map.
initializeInsertionMap()  : mixed
initializeLabelCharMap()  : mixed
Lazily initializes label char map.
initializeListInsertionMap()  : mixed
initializeModifierChangeMap()  : mixed
initializeNodeListDiffer()  : mixed
Lazily initializes node list differ.
initializeRemovalMap()  : mixed
Lazily initializes the removal map.
isMultiline()  : bool
Determine whether a list of nodes uses multiline formatting.
newOperandRequiresParens()  : bool
Determines whether an expression used in "new" or "instanceof" requires parentheses.
outdent()  : mixed
Decrease indentation level.
p()  : string
Pretty prints a node.
pArray()  : null|string
Perform a format-preserving pretty print of an array.
pCommaSeparated()  : string
Pretty prints an array of nodes and implodes the printed values with commas.
pCommaSeparatedMultiline()  : string
Pretty prints a comma-separated list of nodes in multiline style, including comments.
pComments()  : string
Prints reformatted text of the passed comments.
pFallback()  : mixed
pFixup()  : string
Print node with fixups.
pImplode()  : string
Pretty prints an array of nodes and implodes the printed values.
pInfixOp()  : string
Pretty-print an infix operation while taking precedence into account.
pModifiers()  : string
Print modifiers, including trailing whitespace.
pPostfixOp()  : string
Pretty-print a postfix operation while taking precedence into account.
pPrec()  : string
Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
pPrefixOp()  : string
Pretty-print a prefix operation while taking precedence into account.
preprocessNodes()  : mixed
Preprocesses the top-level nodes to initialize pretty printer state.
pStmts()  : string
Pretty prints an array of nodes (statements) and indents them optionally.
resetState()  : mixed
Reset pretty printing state.
safeAppend()  : mixed
Appends to a string, ensuring whitespace between label characters.
setIndentLevel()  : mixed
Set indentation level
staticDereferenceLhsRequiresParens()  : bool
Determines whether the LHS of a static operation must be wrapped in parentheses.

Constants

Properties

$canUseSemicolonNamespaces

protected bool $canUseSemicolonNamespaces

Whether semicolon namespaces can be used (i.e. no global namespace is used)

$docStringEndToken

protected string $docStringEndToken

Token placed at end of doc string to ensure it is followed by a newline.

$fixupMap

protected array<string|int, array<string|int, int>> $fixupMap

Map from token classes and subnode names to FIXUP_* constants. This is used during format-preserving prints to place additional parens/braces if necessary.

$insertionMap

protected array<string|int, mixed> $insertionMap

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.

$labelCharMap

protected array<string|int, bool> $labelCharMap

Map determining whether a certain character is a label character

$listInsertionMap

protected array<string|int, string> $listInsertionMap

Map From "{$node->getType()}->{$subNode}" to string that should be inserted between elements of this list subnode.

$modifierChangeMap

protected array<string|int, int> $modifierChangeMap

Map from "{$node->getType()}->{$subNode}" to token before which the modifiers should be reprinted.

$origTokens

protected TokenStream $origTokens

Original tokens for use in format-preserving pretty print

$precedenceMap

protected mixed $precedenceMap = [ // [precedence, associativity] // where for precedence -1 is %left, 0 is %nonassoc and 1 is %right PhpParserNodeExprBinaryOpPow::class => [0, 1], PhpParserNodeExprBitwiseNot::class => [10, 1], PhpParserNodeExprPreInc::class => [10, 1], PhpParserNodeExprPreDec::class => [10, 1], PhpParserNodeExprPostInc::class => [10, -1], PhpParserNodeExprPostDec::class => [10, -1], PhpParserNodeExprUnaryPlus::class => [10, 1], PhpParserNodeExprUnaryMinus::class => [10, 1], PhpParserNodeExprCastInt_::class => [10, 1], PhpParserNodeExprCastDouble::class => [10, 1], PhpParserNodeExprCastString_::class => [10, 1], PhpParserNodeExprCastArray_::class => [10, 1], PhpParserNodeExprCastObject_::class => [10, 1], PhpParserNodeExprCastBool_::class => [10, 1], PhpParserNodeExprCastUnset_::class => [10, 1], PhpParserNodeExprErrorSuppress::class => [10, 1], PhpParserNodeExprInstanceof_::class => [20, 0], PhpParserNodeExprBooleanNot::class => [30, 1], PhpParserNodeExprBinaryOpMul::class => [40, -1], PhpParserNodeExprBinaryOpDiv::class => [40, -1], PhpParserNodeExprBinaryOpMod::class => [40, -1], PhpParserNodeExprBinaryOpPlus::class => [50, -1], PhpParserNodeExprBinaryOpMinus::class => [50, -1], PhpParserNodeExprBinaryOpConcat::class => [50, -1], PhpParserNodeExprBinaryOpShiftLeft::class => [60, -1], PhpParserNodeExprBinaryOpShiftRight::class => [60, -1], PhpParserNodeExprBinaryOpSmaller::class => [70, 0], PhpParserNodeExprBinaryOpSmallerOrEqual::class => [70, 0], PhpParserNodeExprBinaryOpGreater::class => [70, 0], PhpParserNodeExprBinaryOpGreaterOrEqual::class => [70, 0], PhpParserNodeExprBinaryOpEqual::class => [80, 0], PhpParserNodeExprBinaryOpNotEqual::class => [80, 0], PhpParserNodeExprBinaryOpIdentical::class => [80, 0], PhpParserNodeExprBinaryOpNotIdentical::class => [80, 0], PhpParserNodeExprBinaryOpSpaceship::class => [80, 0], PhpParserNodeExprBinaryOpBitwiseAnd::class => [90, -1], PhpParserNodeExprBinaryOpBitwiseXor::class => [100, -1], PhpParserNodeExprBinaryOpBitwiseOr::class => [110, -1], PhpParserNodeExprBinaryOpBooleanAnd::class => [120, -1], PhpParserNodeExprBinaryOpBooleanOr::class => [130, -1], PhpParserNodeExprBinaryOpCoalesce::class => [140, 1], PhpParserNodeExprTernary::class => [150, 0], // parser uses %left for assignments, but they really behave as %right PhpParserNodeExprAssign::class => [160, 1], PhpParserNodeExprAssignRef::class => [160, 1], PhpParserNodeExprAssignOpPlus::class => [160, 1], PhpParserNodeExprAssignOpMinus::class => [160, 1], PhpParserNodeExprAssignOpMul::class => [160, 1], PhpParserNodeExprAssignOpDiv::class => [160, 1], PhpParserNodeExprAssignOpConcat::class => [160, 1], PhpParserNodeExprAssignOpMod::class => [160, 1], PhpParserNodeExprAssignOpBitwiseAnd::class => [160, 1], PhpParserNodeExprAssignOpBitwiseOr::class => [160, 1], PhpParserNodeExprAssignOpBitwiseXor::class => [160, 1], PhpParserNodeExprAssignOpShiftLeft::class => [160, 1], PhpParserNodeExprAssignOpShiftRight::class => [160, 1], PhpParserNodeExprAssignOpPow::class => [160, 1], PhpParserNodeExprAssignOpCoalesce::class => [160, 1], PhpParserNodeExprYieldFrom::class => [165, 1], PhpParserNodeExprPrint_::class => [168, 1], PhpParserNodeExprBinaryOpLogicalAnd::class => [170, -1], PhpParserNodeExprBinaryOpLogicalXor::class => [180, -1], PhpParserNodeExprBinaryOpLogicalOr::class => [190, -1], PhpParserNodeExprInclude_::class => [200, -1], ]

$removalMap

protected array<string|int, array<string|int, int>> $removalMap

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.

Methods

__construct()

Creates a pretty printer instance using the given options.

public __construct([array<string|int, mixed> $options = [] ]) : mixed

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 : array<string|int, mixed> = []

Dictionary of formatting options

Return values
mixed

prettyPrint()

Pretty prints an array of statements.

public prettyPrint(array<string|int, Node$stmts) : string
Parameters
$stmts : array<string|int, Node>

Array of statements

Return values
string

Pretty printed statements

prettyPrintExpr()

Pretty prints an expression.

public prettyPrintExpr(Expr $node) : string
Parameters
$node : Expr

Expression node

Return values
string

Pretty printed node

prettyPrintFile()

Pretty prints a file of statements (includes the opening <?php tag if it is required).

public prettyPrintFile(array<string|int, Node$stmts) : string
Parameters
$stmts : array<string|int, Node>

Array of statements

Return values
string

Pretty printed statements

printFormatPreserving()

Perform a format-preserving pretty print of an AST.

public printFormatPreserving(array<string|int, Node$stmts, array<string|int, Node$origStmts, array<string|int, mixed> $origTokens) : string

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 : array<string|int, Node>

Modified AST with links to original AST

$origStmts : array<string|int, Node>

Original AST with token offset information

$origTokens : array<string|int, mixed>

Tokens of the original code

Return values
string

callLhsRequiresParens()

Determines whether the LHS of a call must be wrapped in parenthesis.

protected callLhsRequiresParens(Node $node) : bool
Parameters
$node : Node

LHS of a call

Return values
bool

Whether parentheses are required

dereferenceLhsRequiresParens()

Determines whether the LHS of an array/object operation must be wrapped in parentheses.

protected dereferenceLhsRequiresParens(Node $node) : bool
Parameters
$node : Node

LHS of dereferencing operation

Return values
bool

Whether parentheses are required

handleMagicTokens()

Handles (and removes) no-indent and doc-string-end tokens.

protected handleMagicTokens(string $str) : string
Parameters
$str : string
Return values
string

initializeEmptyListInsertionMap()

protected initializeEmptyListInsertionMap() : mixed
Return values
mixed

initializeFixupMap()

Lazily initializes fixup map.

protected initializeFixupMap() : mixed

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.

Return values
mixed

initializeInsertionMap()

protected initializeInsertionMap() : mixed
Return values
mixed

initializeLabelCharMap()

Lazily initializes label char map.

protected initializeLabelCharMap() : mixed

The label char map determines whether a certain character may occur in a label.

Return values
mixed

initializeListInsertionMap()

protected initializeListInsertionMap() : mixed
Return values
mixed

initializeModifierChangeMap()

protected initializeModifierChangeMap() : mixed
Return values
mixed

initializeNodeListDiffer()

Lazily initializes node list differ.

protected initializeNodeListDiffer() : mixed

The node list differ is used to determine differences between two array subnodes.

Return values
mixed

initializeRemovalMap()

Lazily initializes the removal map.

protected initializeRemovalMap() : mixed

The removal map is used to determine which additional tokens should be removed when a certain node is replaced by null.

Return values
mixed

isMultiline()

Determine whether a list of nodes uses multiline formatting.

protected isMultiline(array<string|int, \PhpParser\Node|null> $nodes) : bool
Parameters
$nodes : array<string|int, \PhpParser\Node|null>

Node list

Return values
bool

Whether multiline formatting is used

newOperandRequiresParens()

Determines whether an expression used in "new" or "instanceof" requires parentheses.

protected newOperandRequiresParens(Node $node) : bool
Parameters
$node : Node

New or instanceof operand

Return values
bool

Whether parentheses are required

p()

Pretty prints a node.

protected p(Node $node[, bool $parentFormatPreserved = false ]) : string

This method also handles formatting preservation for nodes.

Parameters
$node : Node

Node to be pretty printed

$parentFormatPreserved : bool = false

Whether parent node has preserved formatting

Return values
string

Pretty printed node

pArray()

Perform a format-preserving pretty print of an array.

protected pArray(array<string|int, mixed> $nodes, array<string|int, mixed> $origNodes, int &$pos, int $indentAdjustment, string $parentNodeType, string $subNodeName, null|int $fixup) : null|string
Parameters
$nodes : array<string|int, mixed>

New nodes

$origNodes : array<string|int, mixed>

Original nodes

$pos : int

Current token position (updated by reference)

$indentAdjustment : int

Adjustment for indentation

$parentNodeType : string

Type of the containing node.

$subNodeName : string

Name of array subnode.

$fixup : null|int

Fixup information for array item nodes

Return values
null|string

Result of pretty print or null if cannot preserve formatting

pCommaSeparated()

Pretty prints an array of nodes and implodes the printed values with commas.

protected pCommaSeparated(array<string|int, Node$nodes) : string
Parameters
$nodes : array<string|int, Node>

Array of Nodes to be printed

Return values
string

Comma separated pretty printed nodes

pCommaSeparatedMultiline()

Pretty prints a comma-separated list of nodes in multiline style, including comments.

protected pCommaSeparatedMultiline(array<string|int, Node$nodes, bool $trailingComma) : string

The result includes a leading newline and one level of indentation (same as pStmts).

Parameters
$nodes : array<string|int, Node>

Array of Nodes to be printed

$trailingComma : bool

Whether to use a trailing comma

Return values
string

Comma separated pretty printed nodes in multiline style

pComments()

Prints reformatted text of the passed comments.

protected pComments(array<string|int, Comment$comments) : string
Parameters
$comments : array<string|int, Comment>

List of comments

Return values
string

Reformatted text of comments

pFixup()

Print node with fixups.

protected pFixup(int $fixup, Node $subNode, string|null $parentClass, int $subStartPos, int $subEndPos) : string

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 : int

Fixup type

$subNode : Node

Subnode to print

$parentClass : string|null

Class of parent node

$subStartPos : int

Original start pos of subnode

$subEndPos : int

Original end pos of subnode

Return values
string

Result of fixed-up print of subnode

pImplode()

Pretty prints an array of nodes and implodes the printed values.

protected pImplode(array<string|int, Node$nodes[, string $glue = '' ]) : string
Parameters
$nodes : array<string|int, Node>

Array of Nodes to be printed

$glue : string = ''

Character to implode with

Return values
string

Imploded pretty printed nodes

pInfixOp()

Pretty-print an infix operation while taking precedence into account.

protected pInfixOp(string $class, Node $leftNode, string $operatorString, Node $rightNode) : string
Parameters
$class : string

Node class of operator

$leftNode : Node

Left-hand side node

$operatorString : string

String representation of the operator

$rightNode : Node

Right-hand side node

Return values
string

Pretty printed infix operation

pModifiers()

Print modifiers, including trailing whitespace.

protected pModifiers(int $modifiers) : string
Parameters
$modifiers : int

Modifier mask to print

Return values
string

Printed modifiers

pPostfixOp()

Pretty-print a postfix operation while taking precedence into account.

protected pPostfixOp(string $class, Node $node, string $operatorString) : string
Parameters
$class : string

Node class of operator

$node : Node

Node

$operatorString : string

String representation of the operator

Return values
string

Pretty printed postfix operation

pPrec()

Prints an expression node with the least amount of parentheses necessary to preserve the meaning.

protected pPrec(Node $node, int $parentPrecedence, int $parentAssociativity, int $childPosition) : string
Parameters
$node : Node

Node to pretty print

$parentPrecedence : int

Precedence of the parent operator

$parentAssociativity : int

Associativity of parent operator (-1 is left, 0 is nonassoc, 1 is right)

$childPosition : int

Position of the node relative to the operator (-1 is left, 1 is right)

Return values
string

The pretty printed node

pPrefixOp()

Pretty-print a prefix operation while taking precedence into account.

protected pPrefixOp(string $class, string $operatorString, Node $node) : string
Parameters
$class : string

Node class of operator

$operatorString : string

String representation of the operator

$node : Node

Node

Return values
string

Pretty printed prefix operation

preprocessNodes()

Preprocesses the top-level nodes to initialize pretty printer state.

protected preprocessNodes(array<string|int, Node$nodes) : mixed
Parameters
$nodes : array<string|int, Node>

Array of nodes

Return values
mixed

pStmts()

Pretty prints an array of nodes (statements) and indents them optionally.

protected pStmts(array<string|int, Node$nodes[, bool $indent = true ]) : string
Parameters
$nodes : array<string|int, Node>

Array of nodes

$indent : bool = true

Whether to indent the printed nodes

Return values
string

Pretty printed statements

resetState()

Reset pretty printing state.

protected resetState() : mixed
Return values
mixed

safeAppend()

Appends to a string, ensuring whitespace between label characters.

protected safeAppend(string &$str, string $append) : mixed

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.

Parameters
$str : string
$append : string
Return values
mixed

setIndentLevel()

Set indentation level

protected setIndentLevel(int $level) : mixed
Parameters
$level : int

Level in number of spaces

Return values
mixed

staticDereferenceLhsRequiresParens()

Determines whether the LHS of a static operation must be wrapped in parentheses.

protected staticDereferenceLhsRequiresParens(Node $node) : bool
Parameters
$node : Node

LHS of dereferencing operation

Return values
bool

Whether parentheses are required

Search results