| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Builder; |
| 4: | |
| 5: | use PhpParser; |
| 6: | use PhpParser\BuilderHelpers; |
| 7: | use PhpParser\Node; |
| 8: | use PhpParser\Node\Stmt; |
| 9: | |
| 10: | class Trait_ extends Declaration |
| 11: | { |
| 12: | protected $name; |
| 13: | protected $uses = []; |
| 14: | protected $properties = []; |
| 15: | protected $methods = []; |
| 16: | |
| 17: | |
| 18: | protected $attributeGroups = []; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public function __construct(string $name) { |
| 26: | $this->name = $name; |
| 27: | } |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public function addStmt($stmt) { |
| 37: | $stmt = BuilderHelpers::normalizeNode($stmt); |
| 38: | |
| 39: | if ($stmt instanceof Stmt\Property) { |
| 40: | $this->properties[] = $stmt; |
| 41: | } elseif ($stmt instanceof Stmt\ClassMethod) { |
| 42: | $this->methods[] = $stmt; |
| 43: | } elseif ($stmt instanceof Stmt\TraitUse) { |
| 44: | $this->uses[] = $stmt; |
| 45: | } else { |
| 46: | throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); |
| 47: | } |
| 48: | |
| 49: | return $this; |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public function addAttribute($attribute) { |
| 60: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 61: | |
| 62: | return $this; |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function getNode() : PhpParser\Node { |
| 71: | return new Stmt\Trait_( |
| 72: | $this->name, [ |
| 73: | 'stmts' => array_merge($this->uses, $this->properties, $this->methods), |
| 74: | 'attrGroups' => $this->attributeGroups, |
| 75: | ], $this->attributes |
| 76: | ); |
| 77: | } |
| 78: | } |
| 79: | |