Conditions | 26 |
Paths | 26 |
Total Lines | 139 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
83 | public function visit(Visitor\Element $element, &$handle = null, $eldnah = null) |
||
84 | { |
||
85 | /** @var \Hoa\Compiler\Llk\TreeNode $element */ |
||
86 | $id = $element->getId(); |
||
87 | $variable = false !== $eldnah; |
||
88 | |||
89 | switch ($id) { |
||
90 | case '#expression': |
||
91 | $this->root = new Model\Rule(); |
||
92 | $this->root->expression = $element->getChild(0)->accept( |
||
|
|||
93 | $this, |
||
94 | $handle, |
||
95 | $eldnah |
||
96 | ); |
||
97 | |||
98 | return $this->root; |
||
99 | |||
100 | case '#operation': |
||
101 | $children = $element->getChildren(); |
||
102 | $left = $children[0]->accept($this, $handle, $eldnah); |
||
103 | $right = $children[2]->accept($this, $handle, $eldnah); |
||
104 | $name = $children[1]->accept($this, $handle, false); |
||
105 | |||
106 | return $this->root->_operator( |
||
107 | $name, |
||
108 | [$left, $right], |
||
109 | false |
||
110 | ); |
||
111 | |||
112 | case '#variable_access': |
||
113 | $children = $element->getChildren(); |
||
114 | $name = $children[0]->accept($this, $handle, $eldnah); |
||
115 | array_shift($children); |
||
116 | |||
117 | foreach ($children as $child) { |
||
118 | $_child = $child->accept($this, $handle, $eldnah); |
||
119 | |||
120 | switch ($child->getId()) { |
||
121 | case '#attribute_access': |
||
122 | $name->attribute($_child); |
||
123 | |||
124 | break; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return $name; |
||
129 | |||
130 | case '#attribute_access': |
||
131 | return $element->getChild(0)->accept($this, $handle, false); |
||
132 | |||
133 | case '#array_declaration': |
||
134 | $out = []; |
||
135 | |||
136 | foreach ($element->getChildren() as $child) { |
||
137 | $out[] = $child->accept($this, $handle, $eldnah); |
||
138 | } |
||
139 | |||
140 | return $out; |
||
141 | |||
142 | case '#function_call': |
||
143 | $children = $element->getChildren(); |
||
144 | $name = $children[0]->accept($this, $handle, false); |
||
145 | array_shift($children); |
||
146 | |||
147 | $arguments = []; |
||
148 | |||
149 | foreach ($children as $child) { |
||
150 | $arguments[] = $child->accept($this, $handle, $eldnah); |
||
151 | } |
||
152 | |||
153 | return $this->root->_operator( |
||
154 | $name, |
||
155 | $arguments, |
||
156 | true |
||
157 | ); |
||
158 | |||
159 | case '#and': |
||
160 | case '#or': |
||
161 | case '#xor': |
||
162 | $name = substr($id, 1); |
||
163 | $children = $element->getChildren(); |
||
164 | $left = $children[0]->accept($this, $handle, $eldnah); |
||
165 | $right = $children[1]->accept($this, $handle, $eldnah); |
||
166 | |||
167 | return $this->root->operation($name, [$left, $right]); |
||
168 | |||
169 | case '#not': |
||
170 | return $this->root->operation( |
||
171 | 'not', |
||
172 | [$element->getChild(0)->accept($this, $handle, $eldnah)] |
||
173 | ); |
||
174 | |||
175 | case 'token': |
||
176 | $token = $element->getValueToken(); |
||
177 | $value = $element->getValueValue(); |
||
178 | |||
179 | switch ($token) { |
||
180 | case 'identifier': |
||
181 | return true === $variable ? $this->root->variable($value) : $value; |
||
182 | |||
183 | case 'named_parameter': |
||
184 | |||
185 | return new Model\Parameter(substr($value, 1)); |
||
186 | |||
187 | case 'positional_parameter': |
||
188 | $index = $this->nextParameterIndex++; |
||
189 | |||
190 | return new Model\Parameter($index); |
||
191 | |||
192 | case 'true': |
||
193 | return true; |
||
194 | |||
195 | case 'false': |
||
196 | return false; |
||
197 | |||
198 | case 'null': |
||
199 | return null; |
||
200 | |||
201 | case 'float': |
||
202 | return (float) $value; |
||
203 | |||
204 | case 'integer': |
||
205 | return (int) $value; |
||
206 | |||
207 | case 'string': |
||
208 | return str_replace( |
||
209 | '\\'.$value[0], |
||
210 | $value[0], |
||
211 | substr($value, 1, -1) |
||
212 | ); |
||
213 | |||
214 | default: |
||
215 | throw new Ruler\Exception\Interpreter('Token %s is unknown.', 0, $token); |
||
216 | } |
||
217 | |||
218 | default: |
||
219 | throw new Ruler\Exception\Interpreter('Element %s is unknown.', 1, $id); |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.