| 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 |
/** @var Node\AttributeGroup[] */ |
| 18 |
protected $attributeGroups = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Creates an interface builder. |
| 22 |
* |
| 23 |
* @param string $name Name of the interface |
| 24 |
*/ |
| 25 |
public function __construct(string $name) { |
| 26 |
$this->name = $name; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Adds a statement. |
| 31 |
* |
| 32 |
* @param Stmt|PhpParser\Builder $stmt The statement to add |
| 33 |
* |
| 34 |
* @return $this The builder instance (for fluid interface) |
| 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 |
* Adds an attribute group. |
| 54 |
* |
| 55 |
* @param Node\Attribute|Node\AttributeGroup $attribute |
| 56 |
* |
| 57 |
* @return $this The builder instance (for fluid interface) |
| 58 |
*/ |
| 59 |
public function addAttribute($attribute) { |
| 60 |
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the built trait node. |
| 67 |
* |
| 68 |
* @return Stmt\Trait_ The built interface node |
| 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 |
|