Namespace.php 5.0 KB

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