PrettyPrinter.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. interface PrettyPrinter {
  5. /**
  6. * Pretty prints an array of statements.
  7. *
  8. * @param Node[] $stmts Array of statements
  9. *
  10. * @return string Pretty printed statements
  11. */
  12. public function prettyPrint(array $stmts): string;
  13. /**
  14. * Pretty prints an expression.
  15. *
  16. * @param Expr $node Expression node
  17. *
  18. * @return string Pretty printed node
  19. */
  20. public function prettyPrintExpr(Expr $node): string;
  21. /**
  22. * Pretty prints a file of statements (includes the opening <?php tag if it is required).
  23. *
  24. * @param Node[] $stmts Array of statements
  25. *
  26. * @return string Pretty printed statements
  27. */
  28. public function prettyPrintFile(array $stmts): string;
  29. /**
  30. * Perform a format-preserving pretty print of an AST.
  31. *
  32. * The format preservation is best effort. For some changes to the AST the formatting will not
  33. * be preserved (at least not locally).
  34. *
  35. * In order to use this method a number of prerequisites must be satisfied:
  36. * * The startTokenPos and endTokenPos attributes in the lexer must be enabled.
  37. * * The CloningVisitor must be run on the AST prior to modification.
  38. * * The original tokens must be provided, using the getTokens() method on the lexer.
  39. *
  40. * @param Node[] $stmts Modified AST with links to original AST
  41. * @param Node[] $origStmts Original AST with token offset information
  42. * @param Token[] $origTokens Tokens of the original code
  43. */
  44. public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens): string;
  45. }