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