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