Trait_.php 1.5 KB

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