| Conditions | 27 |
| Paths | 469 |
| Total Lines | 110 |
| Code Lines | 76 |
| 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 |
||
| 103 | protected function getArguments($callable = null, $arguments) |
||
| 104 | { |
||
| 105 | $callType = $this->getAttribute('type'); |
||
| 106 | $callName = $this->getAttribute('name'); |
||
| 107 | |||
| 108 | $parameters = array(); |
||
| 109 | $named = false; |
||
| 110 | foreach ($arguments as $name => $node) { |
||
| 111 | if (!is_int($name)) { |
||
| 112 | $named = true; |
||
| 113 | $name = $this->normalizeName($name); |
||
| 114 | } elseif ($named) { |
||
| 115 | throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName)); |
||
| 116 | } |
||
| 117 | |||
| 118 | $parameters[$name] = $node; |
||
| 119 | } |
||
| 120 | |||
| 121 | $isVariadic = $this->hasAttribute('is_variadic') && $this->getAttribute('is_variadic'); |
||
| 122 | if (!$named && !$isVariadic) { |
||
| 123 | return $parameters; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!$callable) { |
||
| 127 | if ($named) { |
||
| 128 | $message = sprintf('Named arguments are not supported for %s "%s".', $callType, $callName); |
||
| 129 | } else { |
||
| 130 | $message = sprintf('Arbitrary positional arguments are not supported for %s "%s".', $callType, $callName); |
||
| 131 | } |
||
| 132 | |||
| 133 | throw new LogicException($message); |
||
| 134 | } |
||
| 135 | |||
| 136 | $callableParameters = $this->getCallableParameters($callable, $isVariadic); |
||
| 137 | $arguments = array(); |
||
| 138 | $names = array(); |
||
| 139 | $missingArguments = array(); |
||
| 140 | $optionalArguments = array(); |
||
| 141 | $pos = 0; |
||
| 142 | foreach ($callableParameters as $callableParameter) { |
||
| 143 | $names[] = $name = $this->normalizeName($callableParameter->name); |
||
| 144 | |||
| 145 | if (array_key_exists($name, $parameters)) { |
||
| 146 | if (array_key_exists($pos, $parameters)) { |
||
| 147 | throw new Twig_Error_Syntax(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName)); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (count($missingArguments)) { |
||
| 151 | throw new Twig_Error_Syntax(sprintf( |
||
| 152 | 'Argument "%s" could not be assigned for %s "%s(%s)" because it is mapped to an internal PHP function which cannot determine default value for optional argument%s "%s".', |
||
| 153 | $name, $callType, $callName, implode(', ', $names), count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments)) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | $arguments = array_merge($arguments, $optionalArguments); |
||
| 158 | $arguments[] = $parameters[$name]; |
||
| 159 | unset($parameters[$name]); |
||
| 160 | $optionalArguments = array(); |
||
| 161 | } elseif (array_key_exists($pos, $parameters)) { |
||
| 162 | $arguments = array_merge($arguments, $optionalArguments); |
||
| 163 | $arguments[] = $parameters[$pos]; |
||
| 164 | unset($parameters[$pos]); |
||
| 165 | $optionalArguments = array(); |
||
| 166 | ++$pos; |
||
| 167 | } elseif ($callableParameter->isDefaultValueAvailable()) { |
||
| 168 | $optionalArguments[] = new Twig_Node_Expression_Constant($callableParameter->getDefaultValue(), -1); |
||
| 169 | } elseif ($callableParameter->isOptional()) { |
||
| 170 | if (empty($parameters)) { |
||
| 171 | break; |
||
| 172 | } else { |
||
| 173 | $missingArguments[] = $name; |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName)); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($isVariadic) { |
||
| 181 | $arbitraryArguments = new Twig_Node_Expression_Array(array(), -1); |
||
| 182 | foreach ($parameters as $key => $value) { |
||
| 183 | if (is_int($key)) { |
||
| 184 | $arbitraryArguments->addElement($value); |
||
| 185 | } else { |
||
| 186 | $arbitraryArguments->addElement($value, new Twig_Node_Expression_Constant($key, -1)); |
||
| 187 | } |
||
| 188 | unset($parameters[$key]); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($arbitraryArguments->count()) { |
||
| 192 | $arguments = array_merge($arguments, $optionalArguments); |
||
| 193 | $arguments[] = $arbitraryArguments; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | if (!empty($parameters)) { |
||
| 198 | $unknownParameter = null; |
||
| 199 | foreach ($parameters as $parameter) { |
||
| 200 | if ($parameter instanceof Twig_Node) { |
||
| 201 | $unknownParameter = $parameter; |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | throw new Twig_Error_Syntax(sprintf( |
||
| 207 | 'Unknown argument%s "%s" for %s "%s(%s)".', |
||
| 208 | count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names) |
||
| 209 | ), $unknownParameter ? $unknownParameter->getTemplateLine() : -1); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $arguments; |
||
| 213 | } |
||
| 293 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.