Copied!
Abstract
Constants
public PhpParser\PrettyPrinterAbstract ::FIXUP_BRACED_NAME = 4
public PhpParser\PrettyPrinterAbstract ::FIXUP_CALL_LHS = 2
public PhpParser\PrettyPrinterAbstract ::FIXUP_DEREF_LHS = 3
public PhpParser\PrettyPrinterAbstract ::FIXUP_ENCAPSED = 6
public PhpParser\PrettyPrinterAbstract ::FIXUP_PREC_LEFT = 0
public PhpParser\PrettyPrinterAbstract ::FIXUP_PREC_RIGHT = 1
public PhpParser\PrettyPrinterAbstract ::FIXUP_VAR_BRACED_NAME = 5
Methods
public __construct ( array $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.
    
  • param array $options Dictionary of formatting options
public prettyPrint ( array $stmts ) : string
 

Pretty prints an array of statements.

  • param PhpParser\Node [] $stmts Array of statements
  • return string Pretty printed statements
public prettyPrintExpr ( PhpParser\Node\Expr $node ) : string
 

Pretty prints an expression.

  • param Expr $node Expression node
  • return string Pretty printed node
public prettyPrintFile ( array $stmts ) : string
 

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

  • param PhpParser\Node [] $stmts Array of statements
  • return string Pretty printed statements
public printFormatPreserving ( array $stmts , array $origStmts , array $origTokens ) : string
 

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.
  • param PhpParser\Node [] $stmts Modified AST with links to original AST
  • param PhpParser\Node [] $origStmts Original AST with token offset information
  • param array $origTokens Tokens of the original code
  • return string
Properties
protected $canUseSemicolonNamespaces
 
  • var bool Whether semicolon namespaces can be used (i.e. no global namespace is used)
protected $docStringEndToken
 
  • var string Token placed at end of doc string to ensure it is followed by a newline.
protected $emptyListInsertionMap
protected $fixupMap
 
  • var int [] 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 $indentLevel
 
  • var int Current indentation level.
protected $insertionMap
 
  • var array 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 $labelCharMap
 
  • var bool [] Map determining whether a certain character is a label character
protected $listInsertionMap
 
  • var string [] Map From "{$node->getType()}->{$subNode}" to string that should be inserted
    between elements of this list subnode.
protected $modifierChangeMap
 
  • var int [] Map from "{$node->getType()}->{$subNode}" to token before which the modifiers
    should be reprinted.
protected $nl
 
  • var string Newline including current indentation.
protected $nodeListDiffer
 
protected $options
 
  • var array Pretty printer options
protected $origTokens
 
  • var TokenStream Original tokens for use in format-preserving pretty print
protected $precedenceMap
protected $removalMap
 
  • var int [] 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
protected callLhsRequiresParens ( PhpParser\Node $node ) : bool
 

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

  • param PhpParser\Node $node LHS of a call
  • return bool Whether parentheses are required
protected dereferenceLhsRequiresParens ( PhpParser\Node $node ) : bool
 

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

  • param PhpParser\Node $node LHS of dereferencing operation
  • return bool Whether parentheses are required
protected handleMagicTokens ( string $str ) : string
 

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

  • param string $str
  • return string
protected indent ()
 

Increase indentation level.

protected initializeEmptyListInsertionMap ()
protected initializeFixupMap ()
 

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 initializeInsertionMap ()
protected initializeLabelCharMap ()
 

Lazily initializes label char map.

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

protected initializeListInsertionMap ()
protected initializeModifierChangeMap ()
protected initializeNodeListDiffer ()
 

Lazily initializes node list differ.

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

protected initializeRemovalMap ()
 

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 isMultiline ( array $nodes ) : bool
 

Determine whether a list of nodes uses multiline formatting.

  • param (\Node | null) [] $nodes Node list
  • return bool Whether multiline formatting is used
protected outdent ()
 

Decrease indentation level.

protected p ( PhpParser\Node $node , $parentFormatPreserved = false ) : string
 

Pretty prints a node.

This method also handles formatting preservation for nodes.

  • param PhpParser\Node $node Node to be pretty printed
  • param bool $parentFormatPreserved Whether parent node has preserved formatting
  • return string Pretty printed node
protected pArray ( array $nodes , array $origNodes , int $pos , int $indentAdjustment , string $parentNodeType , string $subNodeName , $fixup )
 

Perform a format-preserving pretty print of an array.

  • param array $nodes New nodes
  • param array $origNodes Original nodes
  • param int $pos Current token position (updated by reference)
  • param int $indentAdjustment Adjustment for indentation
  • param string $parentNodeType Type of the containing node.
  • param string $subNodeName Name of array subnode.
  • param null | int $fixup Fixup information for array item nodes
  • return null | string Result of pretty print or null if cannot preserve formatting
protected pCommaSeparated ( array $nodes ) : string
 

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

  • param PhpParser\Node [] $nodes Array of Nodes to be printed
  • return string Comma separated pretty printed nodes
protected pCommaSeparatedMultiline ( array $nodes , bool $trailingComma ) : string
 

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).

  • param PhpParser\Node [] $nodes Array of Nodes to be printed
  • param bool $trailingComma Whether to use a trailing comma
  • return string Comma separated pretty printed nodes in multiline style
protected pComments ( array $comments ) : string
 

Prints reformatted text of the passed comments.

  • param PhpParser\Comment [] $comments List of comments
  • return string Reformatted text of comments
protected pFallback ( PhpParser\Node $node )
protected pFixup ( int $fixup , PhpParser\Node $subNode , $parentClass , int $subStartPos , int $subEndPos ) : string
 

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).

  • param int $fixup Fixup type
  • param PhpParser\Node $subNode Subnode to print
  • param string | null $parentClass Class of parent node
  • param int $subStartPos Original start pos of subnode
  • param int $subEndPos Original end pos of subnode
  • return string Result of fixed-up print of subnode
protected pImplode ( array $nodes , string $glue = '' ) : string
 

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

  • param PhpParser\Node [] $nodes Array of Nodes to be printed
  • param string $glue Character to implode with
  • return string Imploded pretty printed nodes
protected pInfixOp ( string $class , PhpParser\Node $leftNode , string $operatorString , PhpParser\Node $rightNode ) : string
 

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

  • param string $class Node class of operator
  • param PhpParser\Node $leftNode Left-hand side node
  • param string $operatorString String representation of the operator
  • param PhpParser\Node $rightNode Right-hand side node
  • return string Pretty printed infix operation
protected pModifiers ( int $modifiers )
 

Print modifiers, including trailing whitespace.

  • param int $modifiers Modifier mask to print
  • return string Printed modifiers
protected pPostfixOp ( string $class , PhpParser\Node $node , string $operatorString ) : string
 

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

  • param string $class Node class of operator
  • param string $operatorString String representation of the operator
  • param PhpParser\Node $node Node
  • return string Pretty printed postfix operation
protected pPrec ( PhpParser\Node $node , int $parentPrecedence , int $parentAssociativity , int $childPosition ) : string
 

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

  • param PhpParser\Node $node Node to pretty print
  • param int $parentPrecedence Precedence of the parent operator
  • param int $parentAssociativity Associativity of parent operator
    (-1 is left, 0 is nonassoc, 1 is right)
  • param int $childPosition Position of the node relative to the operator
    (-1 is left, 1 is right)
  • return string The pretty printed node
protected pPrefixOp ( string $class , string $operatorString , PhpParser\Node $node ) : string
 

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

  • param string $class Node class of operator
  • param string $operatorString String representation of the operator
  • param PhpParser\Node $node Node
  • return string Pretty printed prefix operation
protected preprocessNodes ( array $nodes )
 

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

protected pStmts ( array $nodes , bool $indent = true ) : string
 

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

  • param PhpParser\Node [] $nodes Array of nodes
  • param bool $indent Whether to indent the printed nodes
  • return string Pretty printed statements
protected resetState ()
 

Reset pretty printing state.

protected safeAppend ( string $str , string $append )
 

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.

  • param string $str
  • param string $append
protected setIndentLevel ( int $level )
 

Set indentation level

  • param int $level Level in number of spaces
© 2022 Bruce Wells
Search Namespaces \ Classes
Configuration