菜谱项目

Trait_.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node\Stmt;
  5. class Trait_ extends Declaration
  6. {
  7. protected $name;
  8. protected $uses = array();
  9. protected $properties = array();
  10. protected $methods = array();
  11. /**
  12. * Creates an interface builder.
  13. *
  14. * @param string $name Name of the interface
  15. */
  16. public function __construct($name) {
  17. $this->name = $name;
  18. }
  19. /**
  20. * Adds a statement.
  21. *
  22. * @param Stmt|PhpParser\Builder $stmt The statement to add
  23. *
  24. * @return $this The builder instance (for fluid interface)
  25. */
  26. public function addStmt($stmt) {
  27. $stmt = $this->normalizeNode($stmt);
  28. if ($stmt instanceof Stmt\Property) {
  29. $this->properties[] = $stmt;
  30. } else if ($stmt instanceof Stmt\ClassMethod) {
  31. $this->methods[] = $stmt;
  32. } else if ($stmt instanceof Stmt\TraitUse) {
  33. $this->uses[] = $stmt;
  34. } else {
  35. throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
  36. }
  37. return $this;
  38. }
  39. /**
  40. * Returns the built trait node.
  41. *
  42. * @return Stmt\Trait_ The built interface node
  43. */
  44. public function getNode() {
  45. return new Stmt\Trait_(
  46. $this->name, array(
  47. 'stmts' => array_merge($this->uses, $this->properties, $this->methods)
  48. ), $this->attributes
  49. );
  50. }
  51. }