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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | protected function compileCallable(Twig_Compiler $compiler) |
||
14 | { |
||
15 | $closingParenthesis = false; |
||
16 | if ($this->hasAttribute('callable') && $callable = $this->getAttribute('callable')) { |
||
17 | if (is_string($callable)) { |
||
18 | $compiler->raw($callable); |
||
19 | } elseif (is_array($callable) && $callable[0] instanceof Twig_ExtensionInterface) { |
||
20 | $compiler->raw(sprintf('$this->env->getExtension(\'%s\')->%s', $callable[0]->getName(), $callable[1])); |
||
21 | } else { |
||
22 | $type = ucfirst($this->getAttribute('type')); |
||
23 | $compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), array', $type, $this->getAttribute('name'))); |
||
24 | $closingParenthesis = true; |
||
25 | } |
||
26 | } else { |
||
27 | $compiler->raw($this->getAttribute('thing')->compile()); |
||
28 | } |
||
29 | |||
30 | $this->compileArguments($compiler); |
||
31 | |||
32 | if ($closingParenthesis) { |
||
33 | $compiler->raw(')'); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | protected function compileArguments(Twig_Compiler $compiler) |
||
38 | { |
||
39 | $compiler->raw('('); |
||
40 | |||
41 | $first = true; |
||
42 | |||
43 | if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) { |
||
44 | $compiler->raw('$this->env'); |
||
45 | $first = false; |
||
46 | } |
||
47 | |||
48 | if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) { |
||
49 | if (!$first) { |
||
50 | $compiler->raw(', '); |
||
51 | } |
||
52 | $compiler->raw('$context'); |
||
53 | $first = false; |
||
54 | } |
||
55 | |||
56 | if ($this->hasAttribute('arguments')) { |
||
57 | foreach ($this->getAttribute('arguments') as $argument) { |
||
58 | if (!$first) { |
||
59 | $compiler->raw(', '); |
||
60 | } |
||
61 | $compiler->string($argument); |
||
62 | $first = false; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($this->hasNode('node')) { |
||
67 | if (!$first) { |
||
68 | $compiler->raw(', '); |
||
69 | } |
||
70 | $compiler->subcompile($this->getNode('node')); |
||
71 | $first = false; |
||
72 | } |
||
73 | |||
74 | if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) { |
||
75 | $callable = $this->hasAttribute('callable') ? $this->getAttribute('callable') : null; |
||
76 | |||
77 | $arguments = $this->getArguments($callable, $this->getNode('arguments')); |
||
78 | |||
79 | foreach ($arguments as $node) { |
||
80 | if (!$first) { |
||
81 | $compiler->raw(', '); |
||
82 | } |
||
83 | $compiler->subcompile($node); |
||
84 | $first = false; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | $compiler->raw(')'); |
||
89 | } |
||
90 | |||
91 | protected function getArguments($callable, $arguments) |
||
203 | |||
204 | protected function normalizeName($name) |
||
208 | } |
||
209 |