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