123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php declare(strict_types=1);
- namespace PhpParser\Builder;
- use PhpParser;
- use PhpParser\BuilderHelpers;
- use PhpParser\Node;
- use PhpParser\Node\Stmt;
- class Trait_ extends Declaration
- {
- protected $name;
- protected $uses = [];
- protected $properties = [];
- protected $methods = [];
-
- protected $attributeGroups = [];
-
- public function __construct(string $name) {
- $this->name = $name;
- }
-
- public function addStmt($stmt) {
- $stmt = BuilderHelpers::normalizeNode($stmt);
- if ($stmt instanceof Stmt\Property) {
- $this->properties[] = $stmt;
- } elseif ($stmt instanceof Stmt\ClassMethod) {
- $this->methods[] = $stmt;
- } elseif ($stmt instanceof Stmt\TraitUse) {
- $this->uses[] = $stmt;
- } else {
- throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
- }
- return $this;
- }
-
- public function addAttribute($attribute) {
- $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
- return $this;
- }
-
- public function getNode() : PhpParser\Node {
- return new Stmt\Trait_(
- $this->name, [
- 'stmts' => array_merge($this->uses, $this->properties, $this->methods),
- 'attrGroups' => $this->attributeGroups,
- ], $this->attributes
- );
- }
- }
|