Complex classes like NodeVisitor 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 NodeVisitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class NodeVisitor extends \Twig_BaseNodeVisitor |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $settings; |
||
31 | |||
32 | /** |
||
33 | * @var string Current template name. |
||
34 | */ |
||
35 | protected $templateName; |
||
36 | |||
37 | /** |
||
38 | * @var bool Denotes if current template body should be bufferized. |
||
39 | */ |
||
40 | private $shouldBufferize = false; |
||
41 | |||
42 | /** |
||
43 | * @var string Denotes current scope of AST (block or body). |
||
44 | */ |
||
45 | private $currentScope; |
||
46 | |||
47 | /** |
||
48 | * @var \Twig_Node[] List of blocks for current template. |
||
49 | */ |
||
50 | private $blocks; |
||
51 | |||
52 | 2 | public function __construct(array $settings) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 2 | protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) |
|
61 | { |
||
62 | 2 | if ($node instanceof \Twig_Node_Module) { |
|
63 | 2 | $this->templateName = $node->getTemplateName(); |
|
64 | } |
||
65 | |||
66 | 2 | if ($this->shouldProcess()) { |
|
67 | |||
68 | 2 | if ($this->isBufferizingNode($node)) { |
|
69 | 2 | $this->shouldBufferize = true; |
|
70 | } |
||
71 | |||
72 | 2 | if ($node instanceof \Twig_Node_Module) { |
|
73 | 2 | $this->blocks = $node->getNode('blocks')->getIterator()->getArrayCopy(); |
|
|
|||
74 | } |
||
75 | |||
76 | 2 | if ($node instanceof \Twig_Node_Body) { |
|
77 | 2 | $this->currentScope = null; |
|
78 | 2 | } elseif ($node instanceof \Twig_Node_Block) { |
|
79 | 2 | $this->currentScope = $node->getAttribute('name'); |
|
80 | } |
||
81 | } |
||
82 | |||
83 | 2 | return $node; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 2 | protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 2 | public function getPriority() |
|
143 | |||
144 | |||
145 | /** |
||
146 | * Check if current template should be processed with node visitor based on whitelist or blacklist. |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 2 | protected function shouldProcess() |
|
160 | |||
161 | /** |
||
162 | * Check if provided node is node for bufferizing. |
||
163 | * |
||
164 | * @param \Twig_Node $node |
||
165 | * @return bool |
||
166 | */ |
||
167 | 2 | protected function isBufferizingNode(\Twig_Node $node = null) |
|
186 | |||
187 | /** |
||
188 | * Checks if current node is asset injection node, or if such node exists in its sub-tree. |
||
189 | * |
||
190 | * @param \Twig_Node $node Node to check. |
||
191 | * @return bool TRUE if this subtree has bufferizing node. |
||
192 | */ |
||
193 | 2 | protected function hasBufferizingNode(\Twig_Node $node = null) |
|
216 | |||
217 | /** |
||
218 | * Get execution priority of bufferized node. |
||
219 | * |
||
220 | * Get execution priority of bufferized node based on the node settings or configuration of the extension. |
||
221 | * |
||
222 | * @param \Twig_Node $node |
||
223 | * @return mixed |
||
224 | */ |
||
225 | 2 | protected function getNodeExecutionPriority(\Twig_Node $node) |
|
240 | } |
||
241 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: