1: <?php
2: namespace Pharborist\Objects;
3:
4: use Pharborist\NodeCollection;
5: use Pharborist\TokenNode;
6: use Pharborist\Parser;
7: use Pharborist\DocCommentTrait;
8: use Pharborist\CommaListNode;
9: use Pharborist\Token;
10:
11: /**
12: * A class member list declaration, e.g. `protected $foo, $bar;` Even if you define
13: * a single member per declaration, it's still considered a list.
14: */
15: class ClassMemberListNode extends ClassStatementNode {
16: use DocCommentTrait;
17: use VisibilityTrait;
18:
19: /**
20: * @var TokenNode
21: */
22: protected $static;
23:
24: /**
25: * @var CommaListNode
26: */
27: protected $members;
28:
29: /**
30: * @param string $property
31: * Property name.
32: * @return ClassMemberListNode
33: */
34: public static function create($property) {
35: /** @var ClassNode $class_node */
36: $class_node = Parser::parseSnippet("class Property {private \${$property};}");
37: $property = $class_node->getStatements()[0]->remove();
38: return $property;
39: }
40:
41: /**
42: * Remove the visibility modifier.
43: */
44: protected function removeVisibility() {
45: throw new \BadMethodCallException("Can not remove visibility from class property.");
46: }
47:
48: /**
49: * @return boolean
50: */
51: public function isStatic() {
52: return isset($this->static);
53: }
54:
55: /**
56: * @return TokenNode
57: */
58: public function getStatic() {
59: return $this->static;
60: }
61:
62: /**
63: * @param boolean $is_static
64: *
65: * @return $this
66: */
67: public function setStatic($is_static) {
68: if ($is_static) {
69: if (!isset($this->static)) {
70: $this->static = Token::_static();
71: $this->visibility->after([Token::space(), $this->static]);
72: }
73: }
74: else {
75: if (isset($this->static)) {
76: // Remove whitespace after static keyword.
77: $this->static->next()->remove();
78: // Remove static keyword.
79: $this->static->remove();
80: }
81: }
82: return $this;
83: }
84:
85: /**
86: * @return CommaListNode
87: */
88: public function getMemberList() {
89: return $this->members;
90: }
91:
92: /**
93: * @return NodeCollection|ClassMemberNode[]
94: */
95: public function getMembers() {
96: return $this->members->getItems();
97: }
98:
99: /**
100: * Adds this property list to a class, detaching it from its current
101: * parent.
102: *
103: * @param ClassNode $class
104: * The target class.
105: *
106: * @return $this
107: */
108: public function addTo(ClassNode $class) {
109: $class->appendProperty($this->remove());
110: return $this;
111: }
112:
113: /**
114: * Creates a clone of this property list and adds it to a class.
115: *
116: * @param ClassNode $class
117: * The target class.
118: *
119: * @return static
120: * The cloned property list.
121: */
122: public function cloneInto(ClassNode $class) {
123: $clone = clone $this;
124: $class->appendProperty($this);
125: return $clone;
126: }
127: }
128: