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.
    
public prettyPrint ( array $stmts ) : string
 

Pretty prints an array of statements.

  • return string Pretty printed statements
public prettyPrintExpr ( PhpParser\Node\Expr $node ) : string
 

Pretty prints an expression.

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

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

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

  • return bool Whether parentheses are required
protected handleMagicTokens ( string $str ) : string
 

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

  • 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 returned when a
certain node is replaced by null.

protected isMultiline ( array $nodes ) : bool
 

Determine whether a list of nodes uses multiline formatting.

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

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

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

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

  • return string Comma separated pretty printed nodes in multiline style
protected pComments ( array $comments ) : string
 

Prints reformatted text of the passed 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).

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

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

  • return string Pretty printed infix operation
protected pModifiers ( int $modifiers )
 

Print modifiers, including trailing whitespace.

  • return string Printed modifiers
protected pPostfixOp ( string $class , PhpParser\Node $node , string $operatorString ) : string
 

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

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

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

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

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

protected setIndentLevel ( int $level )
 

Set indentation level

© 2020 Bruce Wells
Search Namespaces \ Classes
Configuration Numbers (0-9.) only