Conditions | 49 |
Paths | 65 |
Total Lines | 151 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
100 | public function __call($name, $args) |
||
101 | { |
||
102 | if (strlen($name) > 3) { |
||
103 | $properties = $this->listObjectProperties(); |
||
104 | |||
105 | $op = substr($name, 0, 3); |
||
106 | $prop = strtolower($name[3]) . substr($name, 4); |
||
107 | if ($op === 'set' && isset($properties[$prop])) { |
||
108 | if ($this->$prop instanceof Collection) { |
||
109 | throw UnexpectedValueException::collectionCannotBeReplaced($this, $prop); |
||
110 | } |
||
111 | |||
112 | $this->$prop = $args[0]; |
||
113 | |||
114 | return $this; |
||
115 | |||
116 | } elseif ($op === 'get' && isset($properties[$prop])) { |
||
117 | if ($this->$prop instanceof Collection) { |
||
118 | return $this->convertCollection($prop, $args); |
||
119 | |||
120 | } else { |
||
121 | return $this->$prop; |
||
122 | } |
||
123 | |||
124 | } else { // collections |
||
125 | if ($op === 'add') { |
||
126 | if (isset($properties[$prop . 's'])) { |
||
127 | if (!$this->{$prop . 's'} instanceof Collection) { |
||
128 | throw UnexpectedValueException::notACollection($this, $prop . 's'); |
||
129 | } |
||
130 | |||
131 | $this->{$prop . 's'}->add($args[0]); |
||
132 | |||
133 | return $this; |
||
134 | |||
135 | } elseif (substr($prop, -1) === 'y' && isset($properties[$prop = substr($prop, 0, -1) . 'ies'])) { |
||
136 | if (!$this->$prop instanceof Collection) { |
||
137 | throw UnexpectedValueException::notACollection($this, $prop); |
||
138 | } |
||
139 | |||
140 | $this->$prop->add($args[0]); |
||
141 | |||
142 | return $this; |
||
143 | |||
144 | } elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) { |
||
145 | if (!$this->$prop instanceof Collection) { |
||
146 | throw UnexpectedValueException::notACollection($this, $prop); |
||
147 | } |
||
148 | |||
149 | $this->$prop->add($args[0]); |
||
150 | |||
151 | return $this; |
||
152 | |||
153 | } elseif (isset($properties[$prop])) { |
||
154 | throw UnexpectedValueException::notACollection($this, $prop); |
||
155 | } |
||
156 | |||
157 | } elseif ($op === 'has') { |
||
158 | if (isset($properties[$prop . 's'])) { |
||
159 | if (!$this->{$prop . 's'} instanceof Collection) { |
||
160 | throw UnexpectedValueException::notACollection($this, $prop . 's'); |
||
161 | } |
||
162 | |||
163 | return $this->{$prop . 's'}->contains($args[0]); |
||
164 | |||
165 | } elseif (substr($prop, -1) === 'y' && isset($properties[$prop = substr($prop, 0, -1) . 'ies'])) { |
||
166 | if (!$this->$prop instanceof Collection) { |
||
167 | throw UnexpectedValueException::notACollection($this, $prop); |
||
168 | } |
||
169 | |||
170 | return $this->$prop->contains($args[0]); |
||
171 | |||
172 | } elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) { |
||
173 | if (!$this->$prop instanceof Collection) { |
||
174 | throw UnexpectedValueException::notACollection($this, $prop); |
||
175 | } |
||
176 | |||
177 | return $this->$prop->contains($args[0]); |
||
178 | |||
179 | } elseif (isset($properties[$prop])) { |
||
180 | throw UnexpectedValueException::notACollection($this, $prop); |
||
181 | } |
||
182 | |||
183 | } elseif (strlen($name) > 6 && ($op = substr($name, 0, 6)) === 'remove') { |
||
184 | $prop = strtolower($name[6]) . substr($name, 7); |
||
185 | |||
186 | if (isset($properties[$prop . 's'])) { |
||
187 | if (!$this->{$prop . 's'} instanceof Collection) { |
||
188 | throw UnexpectedValueException::notACollection($this, $prop . 's'); |
||
189 | } |
||
190 | |||
191 | $this->{$prop . 's'}->removeElement($args[0]); |
||
192 | |||
193 | return $this; |
||
194 | |||
195 | } elseif (substr($prop, -1) === 'y' && isset($properties[$prop = substr($prop, 0, -1) . 'ies'])) { |
||
196 | if (!$this->$prop instanceof Collection) { |
||
197 | throw UnexpectedValueException::notACollection($this, $prop); |
||
198 | } |
||
199 | |||
200 | $this->$prop->removeElement($args[0]); |
||
201 | |||
202 | return $this; |
||
203 | |||
204 | } elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) { |
||
205 | if (!$this->$prop instanceof Collection) { |
||
206 | throw UnexpectedValueException::notACollection($this, $prop); |
||
207 | } |
||
208 | |||
209 | $this->$prop->removeElement($args[0]); |
||
210 | |||
211 | return $this; |
||
212 | |||
213 | } elseif (isset($properties[$prop])) { |
||
214 | throw UnexpectedValueException::notACollection($this, $prop); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | if ($name === '') { |
||
221 | throw MemberAccessException::callWithoutName($this); |
||
222 | } |
||
223 | $class = get_class($this); |
||
224 | |||
225 | // event functionality |
||
226 | if (preg_match('#^on[A-Z]#', $name) && property_exists($class, $name)) { |
||
227 | $rp = new \ReflectionProperty($this, $name); |
||
228 | if ($rp->isPublic() && !$rp->isStatic()) { |
||
229 | if (is_array($list = $this->$name) || $list instanceof \Traversable) { |
||
230 | foreach ($list as $handler) { |
||
231 | Callback::invokeArgs($handler, $args); |
||
232 | } |
||
233 | } elseif ($list !== NULL) { |
||
234 | throw UnexpectedValueException::invalidEventValue($list, $this, $name); |
||
235 | } |
||
236 | |||
237 | return NULL; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | // extension methods |
||
242 | if ($cb = static::extensionMethod($name)) { |
||
243 | /** @var \Nette\Callback $cb */ |
||
244 | array_unshift($args, $this); |
||
245 | |||
246 | return call_user_func_array($cb, $args); |
||
247 | } |
||
248 | |||
249 | throw MemberAccessException::undefinedMethodCall($this, $name); |
||
250 | } |
||
251 | |||
478 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.