Complex classes like NodeConverter 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 NodeConverter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class NodeConverter |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var integer |
||
30 | */ |
||
31 | protected $variableCounter = 0; |
||
32 | |||
33 | /** |
||
34 | * @var TemplateCompiler |
||
35 | */ |
||
36 | protected $templateCompiler; |
||
37 | |||
38 | /** |
||
39 | * @param TemplateCompiler $templateCompiler |
||
40 | */ |
||
41 | public function __construct(TemplateCompiler $templateCompiler) |
||
45 | |||
46 | /** |
||
47 | * @param integer $variableCounter |
||
48 | * @return void |
||
49 | */ |
||
50 | public function setVariableCounter($variableCounter) |
||
54 | |||
55 | /** |
||
56 | * Returns an array with two elements: |
||
57 | * - initialization: contains PHP code which is inserted *before* the actual rendering call. Must be valid, i.e. end with semi-colon. |
||
58 | * - execution: contains *a single PHP instruction* which needs to return the rendered output of the given element. Should NOT end with semi-colon. |
||
59 | * |
||
60 | * @param NodeInterface $node |
||
61 | * @return array two-element array, see above |
||
62 | * @throws FluidException |
||
63 | */ |
||
64 | public function convert(NodeInterface $node) |
||
102 | |||
103 | /** |
||
104 | * @param EscapingNode $node |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function convertEscapingNode(EscapingNode $node) |
||
108 | { |
||
109 | $configuration = $this->convert($node->getNode()); |
||
110 | if (!empty($configuration['execution']) && strtolower($configuration['execution']) !== 'null' && !is_numeric(trim($configuration['execution'], '\'"'))) { |
||
111 | $configuration['execution'] = sprintf( |
||
112 | 'call_user_func_array( function ($var) { ' . |
||
113 | 'return (is_string($var) || (is_object($var) && method_exists($var, \'__toString\')) ' . |
||
114 | '? htmlspecialchars((string) $var, ENT_QUOTES) : $var); }, [%s])', |
||
115 | $configuration['execution'] |
||
116 | ); |
||
117 | } |
||
118 | return $configuration; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param TextNode $node |
||
123 | * @return array |
||
124 | * @see convert() |
||
125 | */ |
||
126 | protected function convertTextNode(TextNode $node) |
||
133 | |||
134 | /** |
||
135 | * @param NumericNode $node |
||
136 | * @return array |
||
137 | * @see convert() |
||
138 | */ |
||
139 | protected function convertNumericNode(NumericNode $node) |
||
146 | |||
147 | /** |
||
148 | * Convert a single ViewHelperNode into its cached representation. If the ViewHelper implements the "Compilable" facet, |
||
149 | * the ViewHelper itself is asked for its cached PHP code representation. If not, a ViewHelper is built and then invoked. |
||
150 | * |
||
151 | * @param ViewHelperNode $node |
||
152 | * @return array |
||
153 | * @see convert() |
||
154 | */ |
||
155 | protected function convertViewHelperNode(ViewHelperNode $node) |
||
224 | |||
225 | /** |
||
226 | * @param ObjectAccessorNode $node |
||
227 | * @return array |
||
228 | * @see convert() |
||
229 | */ |
||
230 | protected function convertObjectAccessorNode(ObjectAccessorNode $node) |
||
272 | |||
273 | /** |
||
274 | * @param ArrayNode $node |
||
275 | * @return array |
||
276 | * @see convert() |
||
277 | */ |
||
278 | protected function convertArrayNode(ArrayNode $node) |
||
320 | |||
321 | /** |
||
322 | * @param NodeInterface $node |
||
323 | * @return array |
||
324 | * @see convert() |
||
325 | */ |
||
326 | public function convertListOfSubNodes(NodeInterface $node) |
||
360 | |||
361 | /** |
||
362 | * @param ExpressionNodeInterface $node |
||
363 | * @return array |
||
364 | * @see convert() |
||
365 | */ |
||
366 | protected function convertExpressionNode(ExpressionNodeInterface $node) |
||
370 | |||
371 | /** |
||
372 | * @param BooleanNode $node |
||
373 | * @return array |
||
374 | * @see convert() |
||
375 | */ |
||
376 | protected function convertBooleanNode(BooleanNode $node) |
||
377 | { |
||
378 | $booleanStack = $node->getStack(); |
||
379 | |||
380 | // Quick decisions: if there is only one node and it can be determined to be a string/numeric/boolean already |
||
381 | // then use it directly. |
||
382 | if (count($booleanStack) === 1) { |
||
383 | $compiledOnlyNode = $this->convert($booleanStack[0]); |
||
384 | // Execution without possible quotation marks, then converted to lowercase for easier comparison. |
||
385 | $execution = trim($compiledOnlyNode['execution'], '"\'\\'); |
||
386 | $lowercaseExecution = strtolower($execution); |
||
387 | if ($lowercaseExecution === 'true') { |
||
388 | $execution = 'true'; |
||
389 | } elseif ($lowercaseExecution === 'false' || empty($execution)) { |
||
390 | $execution = 'false'; |
||
391 | } elseif (is_numeric($execution) || is_bool($execution)) { |
||
392 | $execution = $execution ? 'true' : 'false'; |
||
393 | } else { |
||
394 | $execution = null; |
||
395 | } |
||
396 | if ($execution !== null) { |
||
397 | // Execution was re-written by one of the above cases, so we return this different execution. If it |
||
398 | // was not rewritten we fall through and proceed with the stack-based strategy below. |
||
399 | return [ |
||
400 | 'initialization' => $compiledOnlyNode['initialization'], |
||
401 | 'execution' => $execution, |
||
402 | ]; |
||
403 | } |
||
404 | } |
||
405 | |||
406 | $stack = $this->convertArrayNode(new ArrayNode($booleanStack)); |
||
407 | $initializationPhpCode = $stack['initialization'] . chr(10); |
||
408 | |||
409 | $parser = new BooleanParser(); |
||
410 | $compiledExpression = $parser->compile(BooleanNode::reconcatenateExpression($node->getStack())); |
||
411 | $functionName = $this->variableName('expression'); |
||
412 | $initializationPhpCode .= $functionName . ' = function($context) {return ' . $compiledExpression . ';};' . chr(10); |
||
413 | |||
414 | return [ |
||
415 | 'initialization' => $initializationPhpCode, |
||
416 | 'execution' => sprintf( |
||
417 | '%s::convertToBoolean( |
||
418 | %s( |
||
419 | %s::gatherContext($renderingContext, %s) |
||
420 | ), |
||
421 | $renderingContext |
||
422 | )', |
||
423 | BooleanNode::class, |
||
424 | $functionName, |
||
425 | BooleanNode::class, |
||
426 | $stack['execution'] |
||
427 | ) |
||
428 | ]; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @param string $text |
||
433 | * @return string |
||
434 | */ |
||
435 | protected function escapeTextForUseInSingleQuotes($text) |
||
439 | |||
440 | /** |
||
441 | * Returns a unique variable name by appending a global index to the given prefix |
||
442 | * |
||
443 | * @param string $prefix |
||
444 | * @return string |
||
445 | */ |
||
446 | public function variableName($prefix) |
||
450 | } |
||
451 |