Namespace_.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node;
  4. use PhpParser\Error;
  5. class Namespace_ extends Node\Stmt
  6. {
  7. /** @var null|Node\Name Name */
  8. public $name;
  9. /** @var Node[] Statements */
  10. public $stmts;
  11. protected static $specialNames = array(
  12. 'self' => true,
  13. 'parent' => true,
  14. 'static' => true,
  15. );
  16. /**
  17. * Constructs a namespace node.
  18. *
  19. * @param null|Node\Name $name Name
  20. * @param null|Node[] $stmts Statements
  21. * @param array $attributes Additional attributes
  22. */
  23. public function __construct(Node\Name $name = null, $stmts = array(), array $attributes = array()) {
  24. parent::__construct(null, $attributes);
  25. $this->name = $name;
  26. $this->stmts = $stmts;
  27. if (isset(self::$specialNames[strtolower($this->name)])) {
  28. throw new Error(
  29. sprintf('Cannot use \'%s\' as namespace name', $this->name),
  30. $this->name->getAttributes()
  31. );
  32. }
  33. if (null !== $this->stmts) {
  34. foreach ($this->stmts as $stmt) {
  35. if ($stmt instanceof self) {
  36. throw new Error('Namespace declarations cannot be nested', $stmt->getAttributes());
  37. }
  38. }
  39. }
  40. }
  41. public function getSubNodeNames() {
  42. return array('name', 'stmts');
  43. }
  44. }