| Conditions | 31 |
| Paths | 3895 |
| Total Lines | 112 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 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 |
||
| 91 | protected function getArguments($callable, $arguments) |
||
| 92 | { |
||
| 93 | $callType = $this->getAttribute('type'); |
||
| 94 | $callName = $this->getAttribute('name'); |
||
| 95 | |||
| 96 | $parameters = array(); |
||
| 97 | $named = false; |
||
| 98 | foreach ($arguments as $name => $node) { |
||
| 99 | if (!is_int($name)) { |
||
| 100 | $named = true; |
||
| 101 | $name = $this->normalizeName($name); |
||
| 102 | } elseif ($named) { |
||
| 103 | throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName)); |
||
| 104 | } |
||
| 105 | |||
| 106 | $parameters[$name] = $node; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!$named) { |
||
| 110 | return $parameters; |
||
| 111 | } |
||
| 112 | |||
| 113 | if (!$callable) { |
||
| 114 | throw new LogicException(sprintf('Named arguments are not supported for %s "%s".', $callType, $callName)); |
||
| 115 | } |
||
| 116 | |||
| 117 | // manage named arguments |
||
| 118 | if (is_array($callable)) { |
||
| 119 | $r = new ReflectionMethod($callable[0], $callable[1]); |
||
| 120 | } elseif (is_object($callable) && !$callable instanceof Closure) { |
||
| 121 | $r = new ReflectionObject($callable); |
||
| 122 | $r = $r->getMethod('__invoke'); |
||
| 123 | } else { |
||
| 124 | $r = new ReflectionFunction($callable); |
||
| 125 | } |
||
| 126 | |||
| 127 | $definition = $r->getParameters(); |
||
| 128 | if ($this->hasNode('node')) { |
||
| 129 | array_shift($definition); |
||
| 130 | } |
||
| 131 | if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) { |
||
| 132 | array_shift($definition); |
||
| 133 | } |
||
| 134 | if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) { |
||
| 135 | array_shift($definition); |
||
| 136 | } |
||
| 137 | if ($this->hasAttribute('arguments') && null !== $this->getAttribute('arguments')) { |
||
| 138 | foreach ($this->getAttribute('arguments') as $argument) { |
||
| 139 | array_shift($definition); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $arguments = array(); |
||
| 144 | $names = array(); |
||
| 145 | $missingArguments = array(); |
||
| 146 | $optionalArguments = array(); |
||
| 147 | $pos = 0; |
||
| 148 | foreach ($definition as $param) { |
||
| 149 | $names[] = $name = $this->normalizeName($param->name); |
||
| 150 | |||
| 151 | if (array_key_exists($name, $parameters)) { |
||
| 152 | if (array_key_exists($pos, $parameters)) { |
||
| 153 | throw new Twig_Error_Syntax(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName)); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (!empty($missingArguments)) { |
||
| 157 | throw new Twig_Error_Syntax(sprintf( |
||
| 158 | '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".', |
||
| 159 | $name, $callType, $callName, implode(', ', $names), count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments)) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | $arguments = array_merge($arguments, $optionalArguments); |
||
| 164 | $arguments[] = $parameters[$name]; |
||
| 165 | unset($parameters[$name]); |
||
| 166 | $optionalArguments = array(); |
||
| 167 | } elseif (array_key_exists($pos, $parameters)) { |
||
| 168 | $arguments = array_merge($arguments, $optionalArguments); |
||
| 169 | $arguments[] = $parameters[$pos]; |
||
| 170 | unset($parameters[$pos]); |
||
| 171 | $optionalArguments = array(); |
||
| 172 | ++$pos; |
||
| 173 | } elseif ($param->isDefaultValueAvailable()) { |
||
| 174 | $optionalArguments[] = new Twig_Node_Expression_Constant($param->getDefaultValue(), -1); |
||
| 175 | } elseif ($param->isOptional()) { |
||
| 176 | if (empty($parameters)) { |
||
| 177 | break; |
||
| 178 | } else { |
||
| 179 | $missingArguments[] = $name; |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName)); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!empty($parameters)) { |
||
| 187 | $unknownParameter = null; |
||
| 188 | foreach ($parameters as $parameter) { |
||
| 189 | if ($parameter instanceof Twig_Node) { |
||
| 190 | $unknownParameter = $parameter; |
||
| 191 | break; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | throw new Twig_Error_Syntax(sprintf( |
||
| 196 | 'Unknown argument%s "%s" for %s "%s(%s)".', |
||
| 197 | count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names) |
||
| 198 | ), $unknownParameter ? $unknownParameter->getLine() : -1); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $arguments; |
||
| 202 | } |
||
| 203 | |||
| 209 |