| Conditions | 28 |
| Paths | 186 |
| Total Lines | 94 |
| Code Lines | 65 |
| 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 |
||
| 225 | public function compileParams( |
||
| 226 | Context $context, |
||
| 227 | Context $mixinEnv, |
||
| 228 | $arguments = [], |
||
| 229 | array &$compiledArguments = [] |
||
| 230 | ) { |
||
| 231 | $frame = new RulesetNode([], []); |
||
| 232 | $params = $this->params; |
||
| 233 | $argsCount = 0; |
||
| 234 | |||
| 235 | if (isset($mixinEnv->frames[0]) && $mixinEnv->frames[0]->functionRegistry) { |
||
| 236 | $frame->functionRegistry = $mixinEnv->frames[0]->functionRegistry->inherit(); |
||
| 237 | } |
||
| 238 | |||
| 239 | // create a copy of mixin environment |
||
| 240 | $mixinEnv = Context::createCopyForCompilation($mixinEnv, array_merge([$frame], $mixinEnv->frames)); |
||
| 241 | |||
| 242 | if ($arguments) { |
||
| 243 | $argsCount = count($arguments); |
||
| 244 | for ($i = 0; $i < $argsCount; ++$i) { |
||
| 245 | if (!isset($arguments[$i])) { |
||
| 246 | continue; |
||
| 247 | } |
||
| 248 | $arg = $arguments[$i]; |
||
| 249 | if (isset($arg['name']) && $name = $arg['name']) { |
||
| 250 | $isNamedFound = false; |
||
| 251 | foreach ($params as $j => $param) { |
||
| 252 | if (!isset($compiledArguments[$j]) && $name === $params[$j]['name']) { |
||
| 253 | $compiledArguments[$j] = $arg['value']->compile($context); |
||
| 254 | array_unshift($frame->rules, new RuleNode($name, $arg['value']->compile($context))); |
||
| 255 | $isNamedFound = true; |
||
| 256 | break; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | if ($isNamedFound) { |
||
| 260 | array_splice($arguments, $i, 1); |
||
| 261 | --$i; |
||
| 262 | continue; |
||
| 263 | } else { |
||
| 264 | throw new CompilerException(sprintf('The named argument for `%s` %s was not found.', |
||
| 265 | $this->name, $arguments[$i]['name'])); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | $argIndex = 0; |
||
| 272 | foreach ($params as $i => $param) { |
||
| 273 | if (array_key_exists($i, $compiledArguments)) { |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | $arg = null; |
||
| 277 | if (array_key_exists($argIndex, $arguments)) { |
||
| 278 | $arg = $arguments[$argIndex]; |
||
| 279 | } |
||
| 280 | if (isset($param['name']) && ($name = $param['name'])) { |
||
| 281 | if (isset($param['variadic']) && $param['variadic']) { |
||
| 282 | $varArgs = []; |
||
| 283 | for ($j = $argIndex; $j < $argsCount; ++$j) { |
||
| 284 | $varArgs[] = $arguments[$j]['value']->compile($context); |
||
| 285 | } |
||
| 286 | $expression = new ExpressionNode($varArgs); |
||
| 287 | array_unshift($frame->rules, new RuleNode($name, $expression->compile($context))); |
||
| 288 | } else { |
||
| 289 | $val = ($arg && $arg['value']) ? $arg['value'] : false; |
||
| 290 | if ($val) { |
||
| 291 | $val = $val->compile($context); |
||
| 292 | } elseif (isset($param['value'])) { |
||
| 293 | $val = $param['value']->compile($mixinEnv); |
||
| 294 | $frame->resetCache(); |
||
| 295 | } else { |
||
| 296 | throw new CompilerException( |
||
| 297 | sprintf('Wrong number of arguments for `%s` (%s for %s)', |
||
| 298 | $this->name, count($arguments), $this->arity) |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | |||
| 302 | array_unshift($frame->rules, new RuleNode($name, $val)); |
||
| 303 | $compiledArguments[$i] = $val; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | if (isset($param['variadic']) && $param['variadic'] && $arguments) { |
||
| 308 | for ($j = $argIndex; $j < $argsCount; ++$j) { |
||
| 309 | $compiledArguments[$j] = $arguments[$j]['value']->compile($context); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | ++$argIndex; |
||
| 313 | } |
||
| 314 | |||
| 315 | ksort($compiledArguments); |
||
| 316 | |||
| 317 | return $frame; |
||
| 318 | } |
||
| 319 | |||
| 416 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..