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') { |
||
| 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 | if (count($booleanStack) === 1) { |
||
| 380 | if (!$booleanStack[0] instanceof NodeInterface || $booleanStack[0] instanceof TextNode) { |
||
| 381 | return [ |
||
| 382 | 'initialization' => '', |
||
| 383 | 'execution' => $booleanStack[0] && strtolower((string)$booleanStack[0]) !== 'false' ? 'true' : 'false', |
||
| 384 | ]; |
||
| 385 | } |
||
| 386 | |||
| 387 | $compiledOnlyNode = $this->convert($booleanStack[0]); |
||
| 388 | return [ |
||
| 389 | 'initialization' => $compiledOnlyNode['initialization'], |
||
| 390 | 'execution' => '(bool)(' . ($compiledOnlyNode['execution'] ? $compiledOnlyNode['execution'] : 'false') . ')', |
||
| 391 | ]; |
||
| 392 | } |
||
| 393 | |||
| 394 | $stack = $this->convertArrayNode(new ArrayNode($booleanStack)); |
||
| 395 | $initializationPhpCode = $stack['initialization'] . chr(10); |
||
| 396 | |||
| 397 | $parser = new BooleanParser(); |
||
| 398 | $compiledExpression = $parser->compile(BooleanNode::reconcatenateExpression($node->getStack())); |
||
| 399 | $functionName = $this->variableName('expression'); |
||
| 400 | $initializationPhpCode .= $functionName . ' = function($context) {return ' . $compiledExpression . ';};' . chr(10); |
||
| 401 | |||
| 402 | return [ |
||
| 403 | 'initialization' => $initializationPhpCode, |
||
| 404 | 'execution' => sprintf( |
||
| 405 | '%s::convertToBoolean( |
||
| 406 | %s( |
||
| 407 | %s::gatherContext($renderingContext, %s) |
||
| 408 | ), |
||
| 409 | $renderingContext |
||
| 410 | )', |
||
| 411 | BooleanNode::class, |
||
| 412 | $functionName, |
||
| 413 | BooleanNode::class, |
||
| 414 | $stack['execution'] |
||
| 415 | ) |
||
| 416 | ]; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $text |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | protected function escapeTextForUseInSingleQuotes($text) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns a unique variable name by appending a global index to the given prefix |
||
| 430 | * |
||
| 431 | * @param string $prefix |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function variableName($prefix) |
||
| 438 | } |
||
| 439 |