| Conditions | 10 |
| Paths | 44 |
| Total Lines | 67 |
| Code Lines | 35 |
| 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 |
||
| 185 | private function gatherParameters($callable, $attribute, $subject, TokenInterface $token) |
||
| 186 | { |
||
| 187 | if ($callable[0] instanceof \Closure) { |
||
| 188 | // don't know why but, it seems that ['\Closure', '__invoke'] is not ok with \ReflectionMethod |
||
| 189 | $reflection = new \ReflectionFunction($callable[0]); |
||
| 190 | } else { |
||
| 191 | $reflection = new \ReflectionMethod(get_class($callable[0]), $callable[1]); |
||
| 192 | } |
||
| 193 | |||
| 194 | $parameters = []; |
||
| 195 | |||
| 196 | // iterating over all parameters for method |
||
| 197 | foreach ($reflection->getParameters() as $parameter) { |
||
| 198 | $parameterType = $parameter->getType(); |
||
| 199 | if (method_exists($parameterType, 'getName')) { |
||
| 200 | $parameterType = $parameterType->getName(); |
||
| 201 | } else { |
||
| 202 | $parameterType = (string) $parameterType; // PHP < 7.1 supports |
||
| 203 | } |
||
| 204 | $parameterName = $parameter->getName(); |
||
| 205 | $parameterPosition = $parameter->getPosition(); |
||
| 206 | switch (true) { |
||
| 207 | // attribute is a bit tricky, cannot use any type to determine whether or not it is required |
||
| 208 | // if the parameter name is "attribute" this assume it should be provided |
||
| 209 | // adding subject to required parameters |
||
| 210 | case $parameterName === 'attribute': |
||
| 211 | $parameters[$parameterPosition] = $attribute; |
||
| 212 | break; |
||
| 213 | |||
| 214 | // parameter looks like the subject being voting on |
||
| 215 | // adding subject to required parameters |
||
| 216 | case is_a($subject, $parameterType) || gettype($subject) === $parameterType: |
||
| 217 | $parameters[$parameterPosition] = $subject; |
||
| 218 | break; |
||
| 219 | |||
| 220 | // parameter looks like a security token |
||
| 221 | // adding token to required parameters |
||
| 222 | case is_a($token, $parameterType): |
||
| 223 | $parameters[$parameterPosition] = $token; |
||
| 224 | break; |
||
| 225 | |||
| 226 | // parameter looks like a security user |
||
| 227 | // adding user to required parameters |
||
| 228 | case is_a($token->getUser(), $parameterType): |
||
| 229 | $parameters[$parameterPosition] = $token->getUser(); |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // this gathered parameters, but the callable needs something more |
||
| 235 | // calling with these parameters will probably results to an error |
||
| 236 | // so throwing an exception is the only thing to do |
||
| 237 | if ($reflection->getNumberOfRequiredParameters() !== count($parameters)) { |
||
| 238 | throw new LogicException( |
||
| 239 | sprintf( |
||
| 240 | 'The callable method "%s"->"%s"() needs parameters that cannot be provided.', |
||
| 241 | get_class($callable[0]), |
||
| 242 | $callable[1] |
||
| 243 | ) |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | |||
| 247 | // return sorted parameters |
||
| 248 | ksort($parameters); |
||
| 249 | |||
| 250 | return $parameters; |
||
| 251 | } |
||
| 252 | } |
||
| 253 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: