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