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 |
||
| 32 | abstract class AbstractConditionViewHelper extends AbstractViewHelper |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | protected $escapeOutput = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initializes the "then" and "else" arguments |
||
| 42 | */ |
||
| 43 | public function initializeArguments() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Renders <f:then> child if $condition is true, otherwise renders <f:else> child. |
||
| 51 | * Method which only gets called if the template is not compiled. For static calling, |
||
| 52 | * the then/else nodes are converted to closures and condition evaluation closures. |
||
| 53 | * |
||
| 54 | * @return string the rendered string |
||
| 55 | * @api |
||
| 56 | */ |
||
| 57 | public function render() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param array $arguments |
||
| 67 | * @param \Closure $renderChildrenClosure |
||
| 68 | * @param RenderingContextInterface $renderingContext |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Static method which can be overridden by subclasses. If a subclass |
||
| 91 | * requires a different (or faster) decision then this method is the one |
||
| 92 | * to override and implement. |
||
| 93 | * |
||
| 94 | * @param array $arguments |
||
| 95 | * @param RenderingContextInterface $renderingContext |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public static function verdict(array $arguments, RenderingContextInterface $renderingContext) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Static method which can be overridden by subclasses. If a subclass |
||
| 105 | * requires a different (or faster) decision then this method is the one |
||
| 106 | * to override and implement. |
||
| 107 | * |
||
| 108 | * Note: method signature does not type-hint that an array is desired, |
||
| 109 | * and as such, *appears* to accept any input type. There is no type hint |
||
| 110 | * here for legacy reasons - the signature is kept compatible with third |
||
| 111 | * party packages which depending on PHP version would error out if this |
||
| 112 | * signature was not compatible with that of existing and in-production |
||
| 113 | * subclasses that will be using this base class in the future. Let this |
||
| 114 | * be a warning if someone considers changing this method signature! |
||
| 115 | * |
||
| 116 | * @deprecated Deprecated in favor of ClassName::verdict($arguments, renderingContext), will no longer be called in 3.0 |
||
| 117 | * @param array|NULL $arguments |
||
| 118 | * @return boolean |
||
| 119 | * @api |
||
| 120 | */ |
||
| 121 | protected static function evaluateCondition($arguments = null) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param array $closures |
||
| 128 | * @param array $conditionClosures |
||
| 129 | * @param RenderingContextInterface $renderingContext |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | private static function evaluateElseClosures(array $closures, array $conditionClosures, RenderingContextInterface $renderingContext) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns value of "then" attribute. |
||
| 148 | * If then attribute is not set, iterates through child nodes and renders ThenViewHelper. |
||
| 149 | * If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered |
||
| 150 | * |
||
| 151 | * @return mixed rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found |
||
| 152 | * @api |
||
| 153 | */ |
||
| 154 | protected function renderThenChild() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns value of "else" attribute. |
||
| 182 | * If else attribute is not set, iterates through child nodes and renders ElseViewHelper. |
||
| 183 | * If else attribute is not set and no ElseViewHelper is found, an empty string will be returned. |
||
| 184 | * |
||
| 185 | * @return string rendered ElseViewHelper or an empty string if no ThenViewHelper was found |
||
| 186 | * @api |
||
| 187 | */ |
||
| 188 | protected function renderElseChild() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The compiled ViewHelper adds two new ViewHelper arguments: __thenClosure and __elseClosure. |
||
| 216 | * These contain closures which are be executed to render the then(), respectively else() case. |
||
| 217 | * |
||
| 218 | * @param string $argumentsName |
||
| 219 | * @param string $closureName |
||
| 220 | * @param string $initializationPhpCode |
||
| 221 | * @param ViewHelperNode $node |
||
| 222 | * @param TemplateCompiler $compiler |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler) |
||
| 254 | } |
||
| 255 |
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: