| Conditions | 40 |
| Paths | 59 |
| Total Lines | 126 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 (isset($properties[$prop])) { |
||
| 137 | throw UnexpectedValueException::notACollection($this, $prop); |
||
| 138 | } |
||
| 139 | |||
| 140 | } elseif ($op === 'has') { |
||
| 141 | if (isset($properties[$prop . 's'])) { |
||
| 142 | if (!$this->{$prop . 's'} instanceof Collection) { |
||
| 143 | throw UnexpectedValueException::notACollection($this, $prop . 's'); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->{$prop . 's'}->contains($args[0]); |
||
| 147 | |||
| 148 | } elseif (substr($prop, -1) === 'y' && isset($properties[$prop = substr($prop, 0, -1) . 'ies'])) { |
||
| 149 | if (!$this->$prop instanceof Collection) { |
||
| 150 | throw UnexpectedValueException::notACollection($this, $prop); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this->$prop->contains($args[0]); |
||
| 154 | |||
| 155 | } elseif (isset($properties[$prop])) { |
||
| 156 | throw UnexpectedValueException::notACollection($this, $prop); |
||
| 157 | } |
||
| 158 | |||
| 159 | } elseif (strlen($name) > 6 && ($op = substr($name, 0, 6)) === 'remove') { |
||
| 160 | $prop = strtolower($name[6]) . substr($name, 7); |
||
| 161 | |||
| 162 | if (isset($properties[$prop . 's'])) { |
||
| 163 | if (!$this->{$prop . 's'} instanceof Collection) { |
||
| 164 | throw UnexpectedValueException::notACollection($this, $prop . 's'); |
||
| 165 | } |
||
| 166 | |||
| 167 | $this->{$prop . 's'}->removeElement($args[0]); |
||
| 168 | |||
| 169 | return $this; |
||
| 170 | |||
| 171 | } elseif (substr($prop, -1) === 'y' && isset($properties[$prop = substr($prop, 0, -1) . 'ies'])) { |
||
| 172 | if (!$this->$prop instanceof Collection) { |
||
| 173 | throw UnexpectedValueException::notACollection($this, $prop); |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->$prop->removeElement($args[0]); |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | |||
| 180 | } elseif (isset($properties[$prop])) { |
||
| 181 | throw UnexpectedValueException::notACollection($this, $prop); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($name === '') { |
||
| 188 | throw MemberAccessException::callWithoutName($this); |
||
| 189 | } |
||
| 190 | $class = get_class($this); |
||
| 191 | |||
| 192 | // event functionality |
||
| 193 | if (preg_match('#^on[A-Z]#', $name) && property_exists($class, $name)) { |
||
| 194 | $rp = new \ReflectionProperty($this, $name); |
||
| 195 | if ($rp->isPublic() && !$rp->isStatic()) { |
||
| 196 | if (is_array($list = $this->$name) || $list instanceof \Traversable) { |
||
| 197 | foreach ($list as $handler) { |
||
| 198 | Callback::invokeArgs($handler, $args); |
||
| 199 | } |
||
| 200 | } elseif ($list !== NULL) { |
||
| 201 | throw UnexpectedValueException::invalidEventValue($list, $this, $name); |
||
| 202 | } |
||
| 203 | |||
| 204 | return NULL; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | // extension methods |
||
| 209 | if ($cb = static::extensionMethod($name)) { |
||
| 210 | /** @var \Nette\Callback $cb */ |
||
| 211 | array_unshift($args, $this); |
||
| 212 | |||
| 213 | return call_user_func_array($cb, $args); |
||
| 214 | } |
||
| 215 | |||
| 216 | throw MemberAccessException::undefinedMethodCall($this, $name); |
||
| 217 | } |
||
| 218 | |||
| 445 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.