Complex classes like AbstractConditionViewHelper 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 AbstractConditionViewHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractConditionViewHelper extends AbstractViewHelper |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var boolean |
||
| 40 | */ |
||
| 41 | protected $escapeOutput = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Initializes the "then" and "else" arguments |
||
| 45 | */ |
||
| 46 | public function initializeArguments() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Renders <f:then> child if $condition is true, otherwise renders <f:else> child. |
||
| 54 | * Method which only gets called if the template is not compiled. For static calling, |
||
| 55 | * the then/else nodes are converted to closures and condition evaluation closures. |
||
| 56 | * |
||
| 57 | * @return string the rendered string |
||
| 58 | * @api |
||
| 59 | */ |
||
| 60 | public function render() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $arguments |
||
| 70 | * @param \Closure $renderChildrenClosure |
||
| 71 | * @param RenderingContextInterface $renderingContext |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Static method which can be overridden by subclasses. If a subclass |
||
| 94 | * requires a different (or faster) decision then this method is the one |
||
| 95 | * to override and implement. |
||
| 96 | * |
||
| 97 | * @param array $arguments |
||
| 98 | * @param RenderingContextInterface $renderingContext |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public static function verdict(array $arguments, RenderingContextInterface $renderingContext) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Static method which can be overridden by subclasses. If a subclass |
||
| 108 | * requires a different (or faster) decision then this method is the one |
||
| 109 | * to override and implement. |
||
| 110 | * |
||
| 111 | * Note: method signature does not type-hint that an array is desired, |
||
| 112 | * and as such, *appears* to accept any input type. There is no type hint |
||
| 113 | * here for legacy reasons - the signature is kept compatible with third |
||
| 114 | * party packages which depending on PHP version would error out if this |
||
| 115 | * signature was not compatible with that of existing and in-production |
||
| 116 | * subclasses that will be using this base class in the future. Let this |
||
| 117 | * be a warning if someone considers changing this method signature! |
||
| 118 | * |
||
| 119 | * @deprecated Deprecated in favor of ClassName::verdict($arguments, renderingContext), will no longer be called in 3.0 |
||
| 120 | * @param array|NULL $arguments |
||
| 121 | * @return boolean |
||
| 122 | * @api |
||
| 123 | */ |
||
| 124 | protected static function evaluateCondition($arguments = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param array $closures |
||
| 131 | * @param array $conditionClosures |
||
| 132 | * @param RenderingContextInterface $renderingContext |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | private static function evaluateElseClosures(array $closures, array $conditionClosures, RenderingContextInterface $renderingContext) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns value of "then" attribute. |
||
| 151 | * If then attribute is not set, iterates through child nodes and renders ThenViewHelper. |
||
| 152 | * If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered |
||
| 153 | * |
||
| 154 | * @return mixed rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found |
||
| 155 | * @api |
||
| 156 | */ |
||
| 157 | protected function renderThenChild() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns value of "else" attribute. |
||
| 187 | * If else attribute is not set, iterates through child nodes and renders ElseViewHelper. |
||
| 188 | * If else attribute is not set and no ElseViewHelper is found, an empty string will be returned. |
||
| 189 | * |
||
| 190 | * @return string rendered ElseViewHelper or an empty string if no ThenViewHelper was found |
||
| 191 | * @api |
||
| 192 | */ |
||
| 193 | protected function renderElseChild() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * The compiled ViewHelper adds two new ViewHelper arguments: __thenClosure and __elseClosure. |
||
| 226 | * These contain closures which are be executed to render the then(), respectively else() case. |
||
| 227 | * |
||
| 228 | * @param string $argumentsName |
||
| 229 | * @param string $closureName |
||
| 230 | * @param string $initializationPhpCode |
||
| 231 | * @param ViewHelperNode $node |
||
| 232 | * @param TemplateCompiler $compiler |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler) |
||
| 264 | } |
||
| 265 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: