| Total Complexity | 77 |
| Total Lines | 278 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Twig_Node_Expression_Call often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Twig_Node_Expression_Call, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Twig_Node_Expression_Call extends Twig_Node_Expression |
||
| 12 | { |
||
| 13 | private $reflector; |
||
| 14 | |||
| 15 | protected function compileCallable(Twig_Compiler $compiler) |
||
| 16 | { |
||
| 17 | $callable = $this->getAttribute('callable'); |
||
| 18 | |||
| 19 | $closingParenthesis = false; |
||
| 20 | if (is_string($callable) && false === strpos($callable, '::')) { |
||
| 21 | $compiler->raw($callable); |
||
| 22 | } else { |
||
| 23 | list($r, $callable) = $this->reflectCallable($callable); |
||
| 24 | if ($r instanceof ReflectionMethod && is_string($callable[0])) { |
||
| 25 | if ($r->isStatic()) { |
||
| 26 | $compiler->raw(sprintf('%s::%s', $callable[0], $callable[1])); |
||
| 27 | } else { |
||
| 28 | $compiler->raw(sprintf('$this->env->getRuntime(\'%s\')->%s', $callable[0], $callable[1])); |
||
| 29 | } |
||
| 30 | } elseif ($r instanceof ReflectionMethod && $callable[0] instanceof Twig_ExtensionInterface) { |
||
| 31 | // For BC/FC with namespaced aliases |
||
| 32 | $class = (new ReflectionClass(get_class($callable[0])))->name; |
||
| 33 | if (!$compiler->getEnvironment()->hasExtension($class)) { |
||
| 34 | throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $class)); |
||
| 35 | } |
||
| 36 | |||
| 37 | $compiler->raw(sprintf('$this->extensions[\'%s\']->%s', ltrim($class, '\\'), $callable[1])); |
||
| 38 | } else { |
||
| 39 | $closingParenthesis = true; |
||
| 40 | $compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), array', ucfirst($this->getAttribute('type')), $this->getAttribute('name'))); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | $this->compileArguments($compiler); |
||
| 45 | |||
| 46 | if ($closingParenthesis) { |
||
| 47 | $compiler->raw(')'); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function compileArguments(Twig_Compiler $compiler) |
||
| 52 | { |
||
| 53 | $compiler->raw('('); |
||
| 54 | |||
| 55 | $first = true; |
||
| 56 | |||
| 57 | if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) { |
||
| 58 | $compiler->raw('$this->env'); |
||
| 59 | $first = false; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) { |
||
| 63 | if (!$first) { |
||
| 64 | $compiler->raw(', '); |
||
| 65 | } |
||
| 66 | $compiler->raw('$context'); |
||
| 67 | $first = false; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($this->hasAttribute('arguments')) { |
||
| 71 | foreach ($this->getAttribute('arguments') as $argument) { |
||
| 72 | if (!$first) { |
||
| 73 | $compiler->raw(', '); |
||
| 74 | } |
||
| 75 | $compiler->string($argument); |
||
| 76 | $first = false; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->hasNode('node')) { |
||
| 81 | if (!$first) { |
||
| 82 | $compiler->raw(', '); |
||
| 83 | } |
||
| 84 | $compiler->subcompile($this->getNode('node')); |
||
| 85 | $first = false; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($this->hasNode('arguments')) { |
||
| 89 | $callable = $this->getAttribute('callable'); |
||
| 90 | $arguments = $this->getArguments($callable, $this->getNode('arguments')); |
||
| 91 | foreach ($arguments as $node) { |
||
| 92 | if (!$first) { |
||
| 93 | $compiler->raw(', '); |
||
| 94 | } |
||
| 95 | $compiler->subcompile($node); |
||
| 96 | $first = false; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $compiler->raw(')'); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function getArguments($callable = null, $arguments) |
||
| 213 | } |
||
| 214 | |||
| 215 | protected function normalizeName($name) |
||
| 216 | { |
||
| 217 | return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), $name)); |
||
| 218 | } |
||
| 219 | |||
| 220 | private function getCallableParameters($callable, $isVariadic) |
||
| 257 | } |
||
| 258 | |||
| 259 | private function reflectCallable($callable) |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 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.