/lib/PHPParser/Node/Stmt/Namespace.php

https://github.com/erosb/PHP-Parser · PHP · 114 lines · 66 code · 16 blank · 32 comment · 16 complexity · 8b05b1a4a07219291b8b4521134c624e MD5 · raw file

  1. <?php
  2. /**
  3. * @property null|PHPParser_Node_Name $name Name
  4. * @property PHPParser_Node[] $stmts Statements
  5. */
  6. class PHPParser_Node_Stmt_Namespace extends PHPParser_Node_Stmt
  7. {
  8. /**
  9. * Constructs a namespace node.
  10. *
  11. * @param null|PHPParser_Node_Name $name Name
  12. * @param PHPParser_Node[] $stmts Statements
  13. * @param int $line Line
  14. * @param null|string $docComment Nearest doc comment
  15. */
  16. public function __construct(PHPParser_Node_Name $name = null, $stmts = array(), $line = -1, $docComment = null) {
  17. parent::__construct(
  18. array(
  19. 'name' => $name,
  20. 'stmts' => $stmts,
  21. ),
  22. $line, $docComment
  23. );
  24. if ('self' === $this->name || 'parent' === $this->name) {
  25. throw new PHPParser_Error(sprintf('Cannot use "%s" as namespace name', $this->name), $line);
  26. }
  27. if (null !== $this->stmts) {
  28. foreach ($this->stmts as $stmt) {
  29. if ($stmt instanceof PHPParser_Node_Stmt_Namespace) {
  30. throw new PHPParser_Error('Namespace declarations cannot be nested', $stmt->getLine());
  31. }
  32. }
  33. }
  34. }
  35. public static function postprocess(array $stmts) {
  36. // null = not in namespace, false = semicolon style, true = bracket style
  37. $bracketed = null;
  38. // whether any statements that aren't allowed before a namespace declaration are encountered
  39. // (the only valid statement currently is a declare)
  40. $hasNotAllowedStmts = false;
  41. // offsets for semicolon style namespaces
  42. // (required for transplanting the following statements into their ->stmts property)
  43. $nsOffsets = array();
  44. foreach ($stmts as $i => $stmt) {
  45. if ($stmt instanceof PHPParser_Node_Stmt_Namespace) {
  46. // ->stmts is null if semicolon style is used
  47. $currentBracketed = null !== $stmt->stmts;
  48. // if no namespace statement has been encountered yet
  49. if (!isset($bracketed)) {
  50. // set the namespacing style
  51. $bracketed = $currentBracketed;
  52. // and ensure that it isn't preceded by a not allowed statement
  53. if ($hasNotAllowedStmts) {
  54. throw new PHPParser_Error('Namespace declaration statement has to be the very first statement in the script', $stmt->getLine());
  55. }
  56. // otherwise ensure that the style of the current namespace matches the style of
  57. // namespaceing used before in this document
  58. } elseif ($bracketed !== $currentBracketed) {
  59. throw new PHPParser_Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
  60. }
  61. // for semicolon style namespaces remember the offset
  62. if (!$bracketed) {
  63. $nsOffsets[] = $i;
  64. }
  65. // declare() and __halt_compiler() are the only valid statements outside of namespace declarations
  66. } elseif (!$stmt instanceof PHPParser_Node_Stmt_Declare
  67. && !$stmt instanceof PHPParser_Node_Stmt_HaltCompiler
  68. ) {
  69. if (true === $bracketed) {
  70. throw new PHPParser_Error('No code may exist outside of namespace {}', $stmt->getLine());
  71. }
  72. $hasNotAllowedStmts = true;
  73. }
  74. }
  75. // if bracketed namespaces were used or no namespaces were used at all just return the
  76. // original statements
  77. if (!isset($bracketed) || true === $bracketed) {
  78. return $stmts;
  79. // for semicolon style transplant statements
  80. } else {
  81. // take all statements preceding the first namespace
  82. $newStmts = array_slice($stmts, 0, $nsOffsets[0]);
  83. // iterate over all following namespaces
  84. for ($i = 0, $c = count($nsOffsets); $i < $c; ++$i) {
  85. $nsStmt = $stmts[$nsOffsets[$i]];
  86. // the last namespace takes all statements after it
  87. if ($c === $i + 1) {
  88. $nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1);
  89. // and all the others take all statements between the current and the following one
  90. } else {
  91. $nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1, $nsOffsets[$i + 1] - $nsOffsets[$i] - 1);
  92. }
  93. $newStmts[] = $nsStmt;
  94. }
  95. return $newStmts;
  96. }
  97. }
  98. }