Complex classes like ViewHelperResolver 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 ViewHelperResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ViewHelperResolver |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $resolvedViewHelperClassNames = []; |
||
30 | |||
31 | /** |
||
32 | * Namespaces requested by the template being rendered, |
||
33 | * in [shortname => phpnamespace] format. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $namespaces = [ |
||
38 | 'f' => ['TYPO3Fluid\\Fluid\\ViewHelpers'] |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $aliases = []; |
||
45 | |||
46 | /** |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getNamespaces() |
||
53 | |||
54 | /** |
||
55 | * Adds an alias of a ViewHelper, allowing you to call for example |
||
56 | * |
||
57 | * @param string $alias |
||
58 | * @param string $namespace |
||
59 | * @param string $identifier |
||
60 | */ |
||
61 | public function addViewHelperAlias(string $alias, string $namespace, string $identifier) |
||
65 | |||
66 | public function isAliasRegistered(string $alias): bool |
||
70 | |||
71 | /** |
||
72 | * Add a PHP namespace where ViewHelpers can be found and give |
||
73 | * it an alias/identifier. |
||
74 | * |
||
75 | * The provided namespace can be either a single namespace or |
||
76 | * an array of namespaces, as strings. The identifier/alias is |
||
77 | * always a single, alpha-numeric ASCII string. |
||
78 | * |
||
79 | * Calling this method multiple times with different PHP namespaces |
||
80 | * for the same alias causes that namespace to be *extended*, |
||
81 | * meaning that the PHP namespace you provide second, third etc. |
||
82 | * are also used in lookups and are used *first*, so that if any |
||
83 | * of the namespaces you add contains a class placed and named the |
||
84 | * same way as one that exists in an earlier namespace, then your |
||
85 | * class gets used instead of the earlier one. |
||
86 | * |
||
87 | * Example: |
||
88 | * |
||
89 | * $resolver->addNamespace('my', 'My\Package\ViewHelpers'); |
||
90 | * // Any ViewHelpers under this namespace can now be accessed using for example {my:example()} |
||
91 | * // Now, assuming you also have an ExampleViewHelper class in a different |
||
92 | * // namespace and wish to make that ExampleViewHelper override the other: |
||
93 | * $resolver->addNamespace('my', 'My\OtherPackage\ViewHelpers'); |
||
94 | * // Now, since ExampleViewHelper exists in both places but the |
||
95 | * // My\OtherPackage\ViewHelpers namespace was added *last*, Fluid |
||
96 | * // will find and use My\OtherPackage\ViewHelpers\ExampleViewHelper. |
||
97 | * |
||
98 | * Alternatively, setNamespaces() can be used to reset and redefine |
||
99 | * all previously added namespaces - which is great for cases where |
||
100 | * you need to remove or replace previously added namespaces. Be aware |
||
101 | * that setNamespaces() also removes the default "f" namespace, so |
||
102 | * when you use this method you should always include the "f" namespace. |
||
103 | * |
||
104 | * @param string $identifier |
||
105 | * @param string|array $phpNamespace |
||
106 | * @return void |
||
107 | */ |
||
108 | public function addNamespace($identifier, $phpNamespace) |
||
118 | |||
119 | /** |
||
120 | * Wrapper to allow adding namespaces in bulk *without* first |
||
121 | * clearing the already added namespaces. Utility method mainly |
||
122 | * used in compiled templates, where some namespaces can be added |
||
123 | * from outside and some can be added from compiled values. |
||
124 | * |
||
125 | * @param array $namespaces |
||
126 | * @return void |
||
127 | */ |
||
128 | public function addNamespaces(array $namespaces) |
||
134 | |||
135 | /** |
||
136 | * Resolves the PHP namespace based on the Fluid xmlns namespace, |
||
137 | * which can be either a URL matching the Patterns::NAMESPACEPREFIX |
||
138 | * and Patterns::NAMESPACESUFFIX rules, or a PHP namespace. When |
||
139 | * namespace is a PHP namespace it is optional to suffix it with |
||
140 | * the "\ViewHelpers" segment, e.g. "My\Package" is as valid to |
||
141 | * use as "My\Package\ViewHelpers" is. |
||
142 | * |
||
143 | * @param string $fluidNamespace |
||
144 | * @return string |
||
145 | */ |
||
146 | public function resolvePhpNamespaceFromFluidNamespace($fluidNamespace) |
||
162 | |||
163 | /** |
||
164 | * Set all namespaces as an array of ['identifier' => ['Php\Namespace1', 'Php\Namespace2']] |
||
165 | * namespace definitions. For convenience and legacy support, a |
||
166 | * format of ['identifier' => 'Only\Php\Namespace'] is allowed, |
||
167 | * but will internally convert the namespace to an array and |
||
168 | * allow it to be extended by addNamespace(). |
||
169 | * |
||
170 | * Note that when using this method the default "f" namespace is |
||
171 | * also removed and so must be included in $namespaces or added |
||
172 | * after using addNamespace(). Or, add the PHP namespaces that |
||
173 | * belonged to "f" as a new alias and use that in your templates. |
||
174 | * |
||
175 | * Use getNamespaces() to get an array of currently added namespaces. |
||
176 | * |
||
177 | * @param array $namespaces |
||
178 | * @return void |
||
179 | */ |
||
180 | public function setNamespaces(array $namespaces) |
||
187 | |||
188 | /** |
||
189 | * Validates the given namespaceIdentifier and returns FALSE |
||
190 | * if the namespace is unknown, causing the tag to be rendered |
||
191 | * without processing. |
||
192 | * |
||
193 | * @param string $namespaceIdentifier |
||
194 | * @return boolean TRUE if the given namespace is valid, otherwise FALSE |
||
195 | */ |
||
196 | public function isNamespaceValid($namespaceIdentifier) |
||
204 | |||
205 | /** |
||
206 | * Validates the given namespaceIdentifier and returns FALSE |
||
207 | * if the namespace is unknown and not ignored |
||
208 | * |
||
209 | * @param string $namespaceIdentifier |
||
210 | * @return boolean TRUE if the given namespace is valid, otherwise FALSE |
||
211 | */ |
||
212 | public function isNamespaceValidOrIgnored($namespaceIdentifier) |
||
228 | |||
229 | /** |
||
230 | * @param string $namespaceIdentifier |
||
231 | * @return boolean |
||
232 | */ |
||
233 | public function isNamespaceIgnored($namespaceIdentifier) |
||
249 | |||
250 | /** |
||
251 | * Resolves a ViewHelper class name by namespace alias and |
||
252 | * Fluid-format identity, e.g. "f" and "format.htmlspecialchars". |
||
253 | * |
||
254 | * Looks in all PHP namespaces which have been added for the |
||
255 | * provided alias, starting in the last added PHP namespace. If |
||
256 | * a ViewHelper class exists in multiple PHP namespaces Fluid |
||
257 | * will detect and use whichever one was added last. |
||
258 | * |
||
259 | * If no ViewHelper class can be detected in any of the added |
||
260 | * PHP namespaces a Fluid Parser Exception is thrown. |
||
261 | * |
||
262 | * @param string|null $namespaceIdentifier |
||
263 | * @param string $methodIdentifier |
||
264 | * @return string|NULL |
||
265 | * @throws ParserException |
||
266 | */ |
||
267 | public function resolveViewHelperClassName($namespaceIdentifier, $methodIdentifier) |
||
289 | |||
290 | /** |
||
291 | * Can be overridden by custom implementations to change the way |
||
292 | * classes are loaded when the class is a ViewHelper - for |
||
293 | * example making it possible to use a DI-aware class loader. |
||
294 | * |
||
295 | * @param string $namespace |
||
296 | * @param string $viewHelperShortName |
||
297 | * @return ViewHelperInterface |
||
298 | */ |
||
299 | public function createViewHelperInstance($namespace, $viewHelperShortName) |
||
304 | |||
305 | /** |
||
306 | * Wrapper to create a ViewHelper instance by class name. This is |
||
307 | * the final method called when creating ViewHelper classes - |
||
308 | * overriding this method allows custom constructors, dependency |
||
309 | * injections etc. to be performed on the ViewHelper instance. |
||
310 | * |
||
311 | * @param string $viewHelperClassName |
||
312 | * @return ViewHelperInterface |
||
313 | */ |
||
314 | public function createViewHelperInstanceFromClassName($viewHelperClassName) |
||
318 | |||
319 | /** |
||
320 | * Return an array of ArgumentDefinition instances which describe |
||
321 | * the arguments that the ViewHelper supports. By default, the |
||
322 | * arguments are simply fetched from the ViewHelper - but custom |
||
323 | * implementations can if necessary add/remove/replace arguments |
||
324 | * which will be passed to the ViewHelper. |
||
325 | * |
||
326 | * @param ViewHelperInterface $viewHelper |
||
327 | * @return ArgumentDefinition[] |
||
328 | */ |
||
329 | public function getArgumentDefinitionsForViewHelper(ViewHelperInterface $viewHelper) |
||
333 | |||
334 | /** |
||
335 | * Resolve a viewhelper name. |
||
336 | * |
||
337 | * @param string $namespaceIdentifier Namespace identifier for the view helper. |
||
338 | * @param string $methodIdentifier Method identifier, might be hierarchical like "link.url" |
||
339 | * @return string The fully qualified class name of the viewhelper |
||
340 | */ |
||
341 | protected function resolveViewHelperName($namespaceIdentifier, $methodIdentifier) |
||
359 | } |
||
360 |