Complex classes like TemplatePaths 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 TemplatePaths, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | class TemplatePaths |
||
61 | { |
||
62 | |||
63 | const DEFAULT_FORMAT = 'html'; |
||
64 | const DEFAULT_TEMPLATES_DIRECTORY = 'Resources/Private/Templates/'; |
||
65 | const DEFAULT_LAYOUTS_DIRECTORY = 'Resources/Private/Layouts/'; |
||
66 | const DEFAULT_PARTIALS_DIRECTORY = 'Resources/Private/Partials/'; |
||
67 | const CONFIG_TEMPLATEROOTPATHS = 'templateRootPaths'; |
||
68 | const CONFIG_LAYOUTROOTPATHS = 'layoutRootPaths'; |
||
69 | const CONFIG_PARTIALROOTPATHS = 'partialRootPaths'; |
||
70 | const CONFIG_FORMAT = 'format'; |
||
71 | const NAME_TEMPLATES = 'templates'; |
||
72 | const NAME_LAYOUTS = 'layouts'; |
||
73 | const NAME_PARTIALS = 'partials'; |
||
74 | |||
75 | /** |
||
76 | * Holds already resolved identifiers for template files |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $resolvedIdentifiers = [ |
||
81 | self::NAME_TEMPLATES => [], |
||
82 | self::NAME_LAYOUTS => [], |
||
83 | self::NAME_PARTIALS => [] |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * Holds already resolved identifiers for template files |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $resolvedFiles = [ |
||
92 | self::NAME_TEMPLATES => [], |
||
93 | self::NAME_LAYOUTS => [], |
||
94 | self::NAME_PARTIALS => [] |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $templateRootPaths = []; |
||
101 | |||
102 | /** |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $layoutRootPaths = []; |
||
106 | |||
107 | /** |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $partialRootPaths = []; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $templatePathAndFilename = null; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | */ |
||
120 | protected $layoutPathAndFilename = null; |
||
121 | |||
122 | /** |
||
123 | * @var string|NULL |
||
124 | */ |
||
125 | protected $templateSource = null; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $format = self::DEFAULT_FORMAT; |
||
131 | |||
132 | /** |
||
133 | * @param array|string|NULL $packageNameOrArray |
||
134 | */ |
||
135 | public function __construct($packageNameOrArray = null) |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | public function toArray(): array |
||
155 | |||
156 | /** |
||
157 | * @param string|null $templatePathAndFilename |
||
158 | * @return void |
||
159 | */ |
||
160 | public function setTemplatePathAndFilename($templatePathAndFilename) |
||
164 | |||
165 | /** |
||
166 | * @param string $layoutPathAndFilename |
||
167 | * @return void |
||
168 | */ |
||
169 | public function setLayoutPathAndFilename($layoutPathAndFilename) |
||
173 | |||
174 | /** |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getTemplateRootPaths() |
||
181 | |||
182 | /** |
||
183 | * @param array $templateRootPaths |
||
184 | * @return void |
||
185 | */ |
||
186 | public function setTemplateRootPaths(array $templateRootPaths) |
||
191 | |||
192 | /** |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getLayoutRootPaths() |
||
199 | |||
200 | /** |
||
201 | * @param array $layoutRootPaths |
||
202 | * @return void |
||
203 | */ |
||
204 | public function setLayoutRootPaths(array $layoutRootPaths) |
||
209 | |||
210 | /** |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getPartialRootPaths(): array |
||
217 | |||
218 | /** |
||
219 | * @param array $partialRootPaths |
||
220 | * @return void |
||
221 | */ |
||
222 | public function setPartialRootPaths(array $partialRootPaths) |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getFormat() |
||
235 | |||
236 | /** |
||
237 | * @param string $format |
||
238 | * @return void |
||
239 | */ |
||
240 | public function setFormat(string $format) |
||
244 | |||
245 | /** |
||
246 | * Attempts to resolve an absolute filename |
||
247 | * of a template (i.e. `templateRootPaths`) |
||
248 | * using a controller name, action and format. |
||
249 | * |
||
250 | * Works _backwards_ through template paths in |
||
251 | * order to achieve an "overlay"-type behavior |
||
252 | * where the last paths added are the first to |
||
253 | * be checked and the first path added acts as |
||
254 | * fallback if no other paths have the file. |
||
255 | * |
||
256 | * If the file does not exist in any path, |
||
257 | * including fallback path, `NULL` is returned. |
||
258 | * |
||
259 | * Path configurations filled from TypoScript |
||
260 | * is automatically recorded in the right |
||
261 | * order (see `fillFromTypoScriptArray`), but |
||
262 | * when manually setting the paths that should |
||
263 | * be checked, you as user must be aware of |
||
264 | * this reverse behavior (which you should |
||
265 | * already be, given that it is the same way |
||
266 | * TypoScript path configurations work). |
||
267 | * |
||
268 | * @param string $controller |
||
269 | * @param string $action |
||
270 | * @param string|null $format |
||
271 | * @return string|null |
||
272 | */ |
||
273 | public function resolveTemplateFileForControllerAndActionAndFormat(string $controller, string $action, ?string $format = null): ?string |
||
294 | |||
295 | /** |
||
296 | * @param string|NULL $controllerName |
||
297 | * @param string $format |
||
298 | * @return array |
||
299 | */ |
||
300 | public function resolveAvailableTemplateFiles(?string $controllerName, string $format = null): array |
||
308 | |||
309 | /** |
||
310 | * @param string $format |
||
311 | * @return array |
||
312 | */ |
||
313 | public function resolveAvailablePartialFiles(string $format = null): array |
||
317 | |||
318 | /** |
||
319 | * @param string $format |
||
320 | * @return array |
||
321 | */ |
||
322 | public function resolveAvailableLayoutFiles(string $format = null): array |
||
326 | |||
327 | /** |
||
328 | * @param array $folders |
||
329 | * @param string $format |
||
330 | * @return array |
||
331 | */ |
||
332 | protected function resolveFilesInFolders(array $folders, string $format): array |
||
340 | |||
341 | /** |
||
342 | * @param string $folder |
||
343 | * @param string $format |
||
344 | * @return array |
||
345 | */ |
||
346 | protected function resolveFilesInFolder(string $folder, string $format): array |
||
360 | |||
361 | /** |
||
362 | * Fills path arrays based on a traditional |
||
363 | * TypoScript array which may contain one or |
||
364 | * more of the supported structures, in order |
||
365 | * of priority: |
||
366 | * |
||
367 | * - `plugin.tx_yourext.view.templateRootPath` and siblings. |
||
368 | * - `plugin.tx_yourext.view.templateRootPaths` and siblings. |
||
369 | * - `plugin.tx_yourext.view.overlays.otherextension.templateRootPath` and siblings. |
||
370 | * |
||
371 | * The paths are treated as follows, using the |
||
372 | * `template`-type paths as an example: |
||
373 | * |
||
374 | * - If `templateRootPath` is defined, it gets |
||
375 | * used as the _first_ path in the internal |
||
376 | * paths array. |
||
377 | * - If `templateRootPaths` is defined, all |
||
378 | * values from it are _appended_ to the |
||
379 | * internal paths array. |
||
380 | * - If `overlays.*` exists in the array it is |
||
381 | * iterated, each `templateRootPath` entry |
||
382 | * from it _appended_ to the internal array. |
||
383 | * |
||
384 | * The result is that after filling, the path |
||
385 | * arrays will contain one or more entries in |
||
386 | * the order described above, depending on how |
||
387 | * many of the possible configurations were |
||
388 | * present in the input array. |
||
389 | * |
||
390 | * Will replace any currently configured paths. |
||
391 | * |
||
392 | * @param array $paths |
||
393 | * @return void |
||
394 | */ |
||
395 | public function fillFromConfigurationArray($paths) |
||
403 | |||
404 | /** |
||
405 | * Fills path arrays with default expected paths |
||
406 | * based on package name (converted to extension |
||
407 | * key automatically). |
||
408 | * |
||
409 | * Will replace any currently configured paths. |
||
410 | * |
||
411 | * @param string $packageName |
||
412 | * @return void |
||
413 | */ |
||
414 | public function fillDefaultsByPackageName($packageName) |
||
421 | |||
422 | /** |
||
423 | * Sanitize a path, ensuring it is absolute and |
||
424 | * if a directory, suffixed by a trailing slash. |
||
425 | * |
||
426 | * @param string|array $path |
||
427 | * @return string|string[] |
||
428 | */ |
||
429 | protected function sanitizePath($path) |
||
445 | |||
446 | /** |
||
447 | * Sanitize paths passing each through sanitizePath(). |
||
448 | * |
||
449 | * @param array $paths |
||
450 | * @return array |
||
451 | */ |
||
452 | protected function sanitizePaths(array $paths) |
||
456 | |||
457 | /** |
||
458 | * Guarantees that $reference is turned into a |
||
459 | * correct, absolute path. |
||
460 | * |
||
461 | * @param string $path |
||
462 | * @return string |
||
463 | */ |
||
464 | protected function ensureAbsolutePath(string $path) |
||
468 | |||
469 | /** |
||
470 | * Guarantees that array $reference with paths |
||
471 | * are turned into correct, absolute paths |
||
472 | * |
||
473 | * @param array $reference |
||
474 | * @return array |
||
475 | */ |
||
476 | protected function ensureAbsolutePaths(array $reference): array |
||
480 | |||
481 | /** |
||
482 | * @param string $path |
||
483 | * @return string |
||
484 | */ |
||
485 | protected function ensureSuffixedPath(string $path): string |
||
489 | |||
490 | /** |
||
491 | * Extract an array of three arrays of paths, one |
||
492 | * for each of the types of Fluid file resources. |
||
493 | * Accepts one or both of the singular and plural |
||
494 | * path definitions in the input - returns the |
||
495 | * combined collections of paths based on both |
||
496 | * the singular and plural entries with the singular |
||
497 | * entries being recorded first and plurals second. |
||
498 | * |
||
499 | * Adds legacy singular name as last option, if set. |
||
500 | * |
||
501 | * @param array $paths |
||
502 | * @return array |
||
503 | */ |
||
504 | protected function extractPathArrays(array $paths): array |
||
528 | |||
529 | /** |
||
530 | * @param string $packageName |
||
531 | * @return string |
||
532 | */ |
||
533 | protected function getPackagePath(string $packageName): string |
||
537 | |||
538 | /** |
||
539 | * Returns a unique identifier for the resolved layout file. |
||
540 | * This identifier is based on the template path and last modification date |
||
541 | * |
||
542 | * @param string $layoutName The name of the layout |
||
543 | * @return string layout identifier |
||
544 | */ |
||
545 | public function getLayoutIdentifier(string $layoutName = 'Default'): string |
||
552 | |||
553 | /** |
||
554 | * Resolve the path and file name of the layout file, based on |
||
555 | * $this->layoutPathAndFilename and $this->layoutPathAndFilenamePattern. |
||
556 | * |
||
557 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
558 | * this method returns that path, otherwise a path and filename will be |
||
559 | * resolved using the layoutPathAndFilenamePattern. |
||
560 | * |
||
561 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
562 | * @return string Path and filename of layout file |
||
563 | * @throws InvalidTemplateResourceException |
||
564 | */ |
||
565 | public function getLayoutSource(string $layoutName = 'Default'): string |
||
570 | |||
571 | /** |
||
572 | * Returns a unique identifier for the resolved template file |
||
573 | * This identifier is based on the template path and last modification date |
||
574 | * |
||
575 | * @param string $controller |
||
576 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
577 | * @return string template identifier |
||
578 | */ |
||
579 | public function getTemplateIdentifier(string $controller = 'Default', string $action = 'Default'): string |
||
588 | |||
589 | /** |
||
590 | * @param mixed $source |
||
591 | */ |
||
592 | public function setTemplateSource($source) |
||
596 | |||
597 | /** |
||
598 | * Resolve the template path and filename for the given action. If $actionName |
||
599 | * is NULL, looks into the current request. |
||
600 | * |
||
601 | * @param string $controller |
||
602 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
603 | * @return string Full path to template |
||
604 | * @throws InvalidTemplateResourceException |
||
605 | */ |
||
606 | public function getTemplateSource(string $controller = 'Default', string $action = 'Default'): string |
||
632 | |||
633 | /** |
||
634 | * Returns a unique identifier for the given file in the format |
||
635 | * <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1> |
||
636 | * The SH1 hash is a checksum that is based on the file path and last modification date |
||
637 | * |
||
638 | * @param string $pathAndFilename |
||
639 | * @param string $prefix |
||
640 | * @return string |
||
641 | */ |
||
642 | protected function createIdentifierForFile(string $pathAndFilename, string $prefix): string |
||
647 | |||
648 | /** |
||
649 | * Resolve the path and file name of the layout file, based on |
||
650 | * $this->options['layoutPathAndFilename'] and $this->options['layoutPathAndFilenamePattern']. |
||
651 | * |
||
652 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
653 | * this method returns that path, otherwise a path and filename will be |
||
654 | * resolved using the layoutPathAndFilenamePattern. |
||
655 | * |
||
656 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
657 | * @return string Path and filename of layout files |
||
658 | * @throws InvalidTemplateResourceException |
||
659 | */ |
||
660 | public function getLayoutPathAndFilename(string $layoutName = 'Default'): string |
||
673 | |||
674 | /** |
||
675 | * Returns a unique identifier for the resolved partial file. |
||
676 | * This identifier is based on the template path and last modification date |
||
677 | * |
||
678 | * @param string $partialName The name of the partial |
||
679 | * @return string partial identifier |
||
680 | */ |
||
681 | public function getPartialIdentifier(string $partialName) |
||
692 | |||
693 | /** |
||
694 | * Figures out which partial to use. |
||
695 | * |
||
696 | * @param string $partialName The name of the partial |
||
697 | * @return string contents of the partial template |
||
698 | * @throws InvalidTemplateResourceException |
||
699 | */ |
||
700 | public function getPartialSource(string $partialName) |
||
705 | |||
706 | /** |
||
707 | * Resolve the partial path and filename based on $this->options['partialPathAndFilenamePattern']. |
||
708 | * |
||
709 | * @param string $partialName The name of the partial |
||
710 | * @return string the full path which should be used. The path definitely exists. |
||
711 | * @throws InvalidTemplateResourceException |
||
712 | */ |
||
713 | public function getPartialPathAndFilename(string $partialName) |
||
723 | |||
724 | /** |
||
725 | * @param array $paths |
||
726 | * @param string $relativePathAndFilename |
||
727 | * @param string $format Optional format to resolve. |
||
728 | * @return string |
||
729 | * @throws InvalidTemplateResourceException |
||
730 | */ |
||
731 | protected function resolveFileInPaths(array $paths, string $relativePathAndFilename, string $format = null): string |
||
755 | |||
756 | /** |
||
757 | * @param string|NULL $type |
||
758 | * @return void |
||
759 | */ |
||
760 | protected function clearResolvedIdentifiersAndTemplates(?string $type = null): void |
||
766 | } |
||
767 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.