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 |
||
27 | class ViewHelperResolver |
||
28 | { |
||
29 | /** |
||
30 | * @var RenderingContextInterface |
||
31 | */ |
||
32 | protected $renderingContext; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $resolvedViewHelperClassNames = []; |
||
38 | |||
39 | /** |
||
40 | * Atom paths indexed by namespace, in |
||
41 | * [shortname => [path1, path2, ...]] format. |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $atoms = []; |
||
45 | |||
46 | /** |
||
47 | * Namespaces requested by the template being rendered, |
||
48 | * in [shortname => [phpnamespace1, phpnamespace2, ...]] format. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $namespaces = [ |
||
53 | 'f' => ['TYPO3Fluid\\Fluid\\ViewHelpers'] |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $aliases = [ |
||
60 | 'html' => ['f', 'html'], |
||
61 | 'raw' => ['f', 'format.raw'], |
||
62 | ]; |
||
63 | |||
64 | public function __construct(RenderingContextInterface $renderingContext) |
||
68 | |||
69 | public function addAtomPath(string $namespace, string $path): void |
||
75 | |||
76 | /** |
||
77 | * Add all Atom paths as array-in-array, with the first level key |
||
78 | * being the namespace and the value being an array of paths. |
||
79 | * |
||
80 | * Example: |
||
81 | * |
||
82 | * $resolver->addAtomPaths( |
||
83 | * [ |
||
84 | * 'my' => [ |
||
85 | * 'path/first/', |
||
86 | * 'path/second/', |
||
87 | * ], |
||
88 | * 'other' => [ |
||
89 | * 'path/third/', |
||
90 | * ], |
||
91 | * ] |
||
92 | * ); |
||
93 | * |
||
94 | * @param iterable|string[][] $paths |
||
95 | */ |
||
96 | public function addAtomPaths(iterable $paths): void |
||
104 | |||
105 | public function resolveAtom(string $namespace, string $name): ComponentInterface |
||
117 | |||
118 | public function resolveAtomFile(string $namespace, string $name): ?string |
||
140 | |||
141 | /** |
||
142 | * @return array|string[][] |
||
143 | */ |
||
144 | public function getAtoms(): array |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getNamespaces(): array |
||
156 | |||
157 | /** |
||
158 | * Adds an alias of a ViewHelper, allowing you to call for example |
||
159 | * |
||
160 | * @param string $alias |
||
161 | * @param string $namespace |
||
162 | * @param string $identifier |
||
163 | */ |
||
164 | public function addViewHelperAlias(string $alias, string $namespace, string $identifier) |
||
168 | |||
169 | public function isAliasRegistered(string $alias): bool |
||
173 | |||
174 | /** |
||
175 | * Add a PHP namespace where ViewHelpers can be found and give |
||
176 | * it an alias/identifier. |
||
177 | * |
||
178 | * The provided namespace can be either a single namespace or |
||
179 | * an array of namespaces, as strings. The identifier/alias is |
||
180 | * always a single, alpha-numeric ASCII string. |
||
181 | * |
||
182 | * Calling this method multiple times with different PHP namespaces |
||
183 | * for the same alias causes that namespace to be *extended*, |
||
184 | * meaning that the PHP namespace you provide second, third etc. |
||
185 | * are also used in lookups and are used *first*, so that if any |
||
186 | * of the namespaces you add contains a class placed and named the |
||
187 | * same way as one that exists in an earlier namespace, then your |
||
188 | * class gets used instead of the earlier one. |
||
189 | * |
||
190 | * Example: |
||
191 | * |
||
192 | * $resolver->addNamespace('my', 'My\Package\ViewHelpers'); |
||
193 | * // Any ViewHelpers under this namespace can now be accessed using for example {my:example()} |
||
194 | * // Now, assuming you also have an ExampleViewHelper class in a different |
||
195 | * // namespace and wish to make that ExampleViewHelper override the other: |
||
196 | * $resolver->addNamespace('my', 'My\OtherPackage\ViewHelpers'); |
||
197 | * // Now, since ExampleViewHelper exists in both places but the |
||
198 | * // My\OtherPackage\ViewHelpers namespace was added *last*, Fluid |
||
199 | * // will find and use My\OtherPackage\ViewHelpers\ExampleViewHelper. |
||
200 | * |
||
201 | * Alternatively, setNamespaces() can be used to reset and redefine |
||
202 | * all previously added namespaces - which is great for cases where |
||
203 | * you need to remove or replace previously added namespaces. Be aware |
||
204 | * that setNamespaces() also removes the default "f" namespace, so |
||
205 | * when you use this method you should always include the "f" namespace. |
||
206 | * |
||
207 | * @param string $identifier |
||
208 | * @param string|array $phpNamespace |
||
209 | * @return void |
||
210 | */ |
||
211 | public function addNamespace(string $identifier, $phpNamespace): void |
||
222 | |||
223 | /** |
||
224 | * Wrapper to allow adding namespaces in bulk *without* first |
||
225 | * clearing the already added namespaces. Utility method mainly |
||
226 | * used in compiled templates, where some namespaces can be added |
||
227 | * from outside and some can be added from compiled values. |
||
228 | * |
||
229 | * @param array $namespaces |
||
230 | * @return void |
||
231 | */ |
||
232 | public function addNamespaces(array $namespaces): void |
||
238 | |||
239 | public function removeNamespace(string $identifier, $phpNamespace): void |
||
248 | |||
249 | /** |
||
250 | * Resolves the PHP namespace based on the Fluid xmlns namespace, |
||
251 | * which can be either a URL matching the Patterns::NAMESPACEPREFIX |
||
252 | * and Patterns::NAMESPACESUFFIX rules, or a PHP namespace. When |
||
253 | * namespace is a PHP namespace it is optional to suffix it with |
||
254 | * the "\ViewHelpers" segment, e.g. "My\Package" is as valid to |
||
255 | * use as "My\Package\ViewHelpers" is. |
||
256 | * |
||
257 | * @param string $fluidNamespace |
||
258 | * @return string |
||
259 | */ |
||
260 | public function resolvePhpNamespaceFromFluidNamespace(string $fluidNamespace): string |
||
278 | |||
279 | /** |
||
280 | * Set all namespaces as an array of ['identifier' => ['Php\Namespace1', 'Php\Namespace2']] |
||
281 | * namespace definitions. For convenience and legacy support, a |
||
282 | * format of ['identifier' => 'Only\Php\Namespace'] is allowed, |
||
283 | * but will internally convert the namespace to an array and |
||
284 | * allow it to be extended by addNamespace(). |
||
285 | * |
||
286 | * Note that when using this method the default "f" namespace is |
||
287 | * also removed and so must be included in $namespaces or added |
||
288 | * after using addNamespace(). Or, add the PHP namespaces that |
||
289 | * belonged to "f" as a new alias and use that in your templates. |
||
290 | * |
||
291 | * Use getNamespaces() to get an array of currently added namespaces. |
||
292 | * |
||
293 | * @param array $namespaces |
||
294 | * @return void |
||
295 | */ |
||
296 | public function setNamespaces(array $namespaces): void |
||
303 | |||
304 | /** |
||
305 | * Validates the given namespaceIdentifier and returns FALSE |
||
306 | * if the namespace is unknown, causing the tag to be rendered |
||
307 | * without processing. |
||
308 | * |
||
309 | * @param string $namespaceIdentifier |
||
310 | * @return boolean TRUE if the given namespace is valid, otherwise FALSE |
||
311 | */ |
||
312 | public function isNamespaceValid(string $namespaceIdentifier): bool |
||
320 | |||
321 | /** |
||
322 | * Validates the given namespaceIdentifier and returns FALSE |
||
323 | * if the namespace is unknown and not ignored |
||
324 | * |
||
325 | * @param string $namespaceIdentifier |
||
326 | * @return boolean TRUE if the given namespace is valid, otherwise FALSE |
||
327 | */ |
||
328 | public function isNamespaceValidOrIgnored(string $namespaceIdentifier): bool |
||
339 | |||
340 | /** |
||
341 | * @param string $namespaceIdentifier |
||
342 | * @return boolean |
||
343 | */ |
||
344 | public function isNamespaceIgnored(string $namespaceIdentifier): bool |
||
360 | |||
361 | /** |
||
362 | * Resolves a ViewHelper class name by namespace alias and |
||
363 | * Fluid-format identity, e.g. "f" and "format.htmlspecialchars". |
||
364 | * |
||
365 | * Looks in all PHP namespaces which have been added for the |
||
366 | * provided alias, starting in the last added PHP namespace. If |
||
367 | * a ViewHelper class exists in multiple PHP namespaces Fluid |
||
368 | * will detect and use whichever one was added last. |
||
369 | * |
||
370 | * If no ViewHelper class can be detected in any of the added |
||
371 | * PHP namespaces a Fluid Parser Exception is thrown. |
||
372 | * |
||
373 | * @param string|null $namespaceIdentifier |
||
374 | * @param string $methodIdentifier |
||
375 | * @return string|null |
||
376 | * @throws Exception |
||
377 | */ |
||
378 | public function resolveViewHelperClassName(?string $namespaceIdentifier, string $methodIdentifier): ?string |
||
420 | |||
421 | /** |
||
422 | * Can be overridden by custom implementations to change the way |
||
423 | * classes are loaded when the class is a ViewHelper - for |
||
424 | * example making it possible to use a DI-aware class loader. |
||
425 | * |
||
426 | * If null is passed as namespace, only registered ViewHelper |
||
427 | * aliases are checked against the $viewHelperShortName. |
||
428 | * |
||
429 | * @param string|null $namespace |
||
430 | * @param string $viewHelperShortName |
||
431 | * @return ComponentInterface |
||
432 | */ |
||
433 | public function createViewHelperInstance(?string $namespace, string $viewHelperShortName): ComponentInterface |
||
448 | |||
449 | /** |
||
450 | * Wrapper to create a ViewHelper instance by class name. This is |
||
451 | * the final method called when creating ViewHelper classes - |
||
452 | * overriding this method allows custom constructors, dependency |
||
453 | * injections etc. to be performed on the ViewHelper instance. |
||
454 | * |
||
455 | * @param string $viewHelperClassName |
||
456 | * @return ComponentInterface |
||
457 | */ |
||
458 | public function createViewHelperInstanceFromClassName(string $viewHelperClassName): ComponentInterface |
||
462 | } |
||
463 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: