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 |
||
38 | class TemplatePaths |
||
39 | { |
||
40 | |||
41 | const DEFAULT_FORMAT = 'html'; |
||
42 | const DEFAULT_TEMPLATES_DIRECTORY = 'Resources/Private/Templates/'; |
||
43 | const DEFAULT_LAYOUTS_DIRECTORY = 'Resources/Private/Layouts/'; |
||
44 | const DEFAULT_PARTIALS_DIRECTORY = 'Resources/Private/Partials/'; |
||
45 | const CONFIG_TEMPLATEROOTPATHS = 'templateRootPaths'; |
||
46 | const CONFIG_LAYOUTROOTPATHS = 'layoutRootPaths'; |
||
47 | const CONFIG_PARTIALROOTPATHS = 'partialRootPaths'; |
||
48 | const CONFIG_FORMAT = 'format'; |
||
49 | const NAME_TEMPLATES = 'templates'; |
||
50 | const NAME_LAYOUTS = 'layouts'; |
||
51 | const NAME_PARTIALS = 'partials'; |
||
52 | |||
53 | /** |
||
54 | * Holds already resolved identifiers for template files |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $resolvedIdentifiers = [ |
||
59 | self::NAME_TEMPLATES => [], |
||
60 | self::NAME_LAYOUTS => [], |
||
61 | self::NAME_PARTIALS => [] |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Holds already resolved identifiers for template files |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected static $resolvedFiles = [ |
||
70 | self::NAME_TEMPLATES => [], |
||
71 | self::NAME_LAYOUTS => [], |
||
72 | self::NAME_PARTIALS => [] |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $templateRootPaths = []; |
||
79 | |||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $layoutRootPaths = []; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $partialRootPaths = []; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $templatePathAndFilename = null; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $layoutPathAndFilename = null; |
||
99 | |||
100 | /** |
||
101 | * @var string|NULL |
||
102 | */ |
||
103 | protected $templateSource = null; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $format = self::DEFAULT_FORMAT; |
||
109 | |||
110 | /** |
||
111 | * @param string|NULL $packageNameOrArray |
||
112 | */ |
||
113 | public function __construct($packageNameOrArray = null) |
||
122 | |||
123 | /** |
||
124 | * @return array |
||
125 | */ |
||
126 | public function toArray() |
||
134 | |||
135 | /** |
||
136 | * @param string $templatePathAndFilename |
||
137 | * @return void |
||
138 | */ |
||
139 | public function setTemplatePathAndFilename($templatePathAndFilename) |
||
143 | |||
144 | /** |
||
145 | * @param string $layoutPathAndFilename |
||
146 | * @return void |
||
147 | */ |
||
148 | public function setLayoutPathAndFilename($layoutPathAndFilename) |
||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | public function getTemplateRootPaths() |
||
160 | |||
161 | /** |
||
162 | * @param array $templateRootPaths |
||
163 | * @return void |
||
164 | */ |
||
165 | public function setTemplateRootPaths(array $templateRootPaths) |
||
170 | |||
171 | /** |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getLayoutRootPaths() |
||
178 | |||
179 | /** |
||
180 | * @param array $layoutRootPaths |
||
181 | * @return void |
||
182 | */ |
||
183 | public function setLayoutRootPaths(array $layoutRootPaths) |
||
188 | |||
189 | /** |
||
190 | * @return array |
||
191 | */ |
||
192 | public function getPartialRootPaths() |
||
196 | |||
197 | /** |
||
198 | * @param array $partialRootPaths |
||
199 | * @return void |
||
200 | */ |
||
201 | public function setPartialRootPaths(array $partialRootPaths) |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getFormat() |
||
214 | |||
215 | /** |
||
216 | * @param string $format |
||
217 | * @return void |
||
218 | */ |
||
219 | public function setFormat($format) |
||
223 | |||
224 | /** |
||
225 | * Attempts to resolve an absolute filename |
||
226 | * of a template (i.e. `templateRootPaths`) |
||
227 | * using a controller name, action and format. |
||
228 | * |
||
229 | * Works _backwards_ through template paths in |
||
230 | * order to achieve an "overlay"-type behavior |
||
231 | * where the last paths added are the first to |
||
232 | * be checked and the first path added acts as |
||
233 | * fallback if no other paths have the file. |
||
234 | * |
||
235 | * If the file does not exist in any path, |
||
236 | * including fallback path, `NULL` is returned. |
||
237 | * |
||
238 | * Path configurations filled from TypoScript |
||
239 | * is automatically recorded in the right |
||
240 | * order (see `fillFromTypoScriptArray`), but |
||
241 | * when manually setting the paths that should |
||
242 | * be checked, you as user must be aware of |
||
243 | * this reverse behavior (which you should |
||
244 | * already be, given that it is the same way |
||
245 | * TypoScript path configurations work). |
||
246 | * |
||
247 | * @param string $controller |
||
248 | * @param string $action |
||
249 | * @param string $format |
||
250 | * @return string|NULL |
||
251 | * @api |
||
252 | */ |
||
253 | public function resolveTemplateFileForControllerAndActionAndFormat($controller, $action, $format = null) |
||
274 | |||
275 | /** |
||
276 | * @param string|NULL $controllerName |
||
277 | * @param string $format |
||
278 | * @return array |
||
279 | */ |
||
280 | public function resolveAvailableTemplateFiles($controllerName, $format = null) |
||
288 | |||
289 | /** |
||
290 | * @param string $format |
||
291 | * @return array |
||
292 | */ |
||
293 | public function resolveAvailablePartialFiles($format = null) |
||
297 | |||
298 | /** |
||
299 | * @param string $format |
||
300 | * @return array |
||
301 | */ |
||
302 | public function resolveAvailableLayoutFiles($format = null) |
||
306 | |||
307 | /** |
||
308 | * @param array $folders |
||
309 | * @param string $format |
||
310 | * @return array |
||
311 | */ |
||
312 | protected function resolveFilesInFolders(array $folders, $format) |
||
320 | |||
321 | /** |
||
322 | * @param string $folder |
||
323 | * @param string $format |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function resolveFilesInFolder($folder, $format) |
||
345 | |||
346 | /** |
||
347 | * Fills path arrays based on a traditional |
||
348 | * TypoScript array which may contain one or |
||
349 | * more of the supported structures, in order |
||
350 | * of priority: |
||
351 | * |
||
352 | * - `plugin.tx_yourext.view.templateRootPath` and siblings. |
||
353 | * - `plugin.tx_yourext.view.templateRootPaths` and siblings. |
||
354 | * - `plugin.tx_yourext.view.overlays.otherextension.templateRootPath` and siblings. |
||
355 | * |
||
356 | * The paths are treated as follows, using the |
||
357 | * `template`-type paths as an example: |
||
358 | * |
||
359 | * - If `templateRootPath` is defined, it gets |
||
360 | * used as the _first_ path in the internal |
||
361 | * paths array. |
||
362 | * - If `templateRootPaths` is defined, all |
||
363 | * values from it are _appended_ to the |
||
364 | * internal paths array. |
||
365 | * - If `overlays.*` exists in the array it is |
||
366 | * iterated, each `templateRootPath` entry |
||
367 | * from it _appended_ to the internal array. |
||
368 | * |
||
369 | * The result is that after filling, the path |
||
370 | * arrays will contain one or more entries in |
||
371 | * the order described above, depending on how |
||
372 | * many of the possible configurations were |
||
373 | * present in the input array. |
||
374 | * |
||
375 | * Will replace any currently configured paths. |
||
376 | * |
||
377 | * @param array $paths |
||
378 | * @return void |
||
379 | * @api |
||
380 | */ |
||
381 | public function fillFromConfigurationArray(array $paths) |
||
389 | |||
390 | /** |
||
391 | * Fills path arrays with default expected paths |
||
392 | * based on package name (converted to extension |
||
393 | * key automatically). |
||
394 | * |
||
395 | * Will replace any currently configured paths. |
||
396 | * |
||
397 | * @param string $packageName |
||
398 | * @return void |
||
399 | * @api |
||
400 | */ |
||
401 | public function fillDefaultsByPackageName($packageName) |
||
408 | |||
409 | /** |
||
410 | * Sanitize a path, ensuring it is absolute and |
||
411 | * if a directory, suffixed by a trailing slash. |
||
412 | * |
||
413 | * @param string|array $path |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function sanitizePath($path) |
||
432 | |||
433 | /** |
||
434 | * Sanitize paths passing each through sanitizePath(). |
||
435 | * |
||
436 | * @param array $paths |
||
437 | * @return array |
||
438 | */ |
||
439 | protected function sanitizePaths(array $paths) |
||
443 | |||
444 | /** |
||
445 | * Guarantees that $reference is turned into a |
||
446 | * correct, absolute path. |
||
447 | * |
||
448 | * @param string $path |
||
449 | * @return string |
||
450 | */ |
||
451 | protected function ensureAbsolutePath($path) |
||
455 | |||
456 | /** |
||
457 | * Guarantees that array $reference with paths |
||
458 | * are turned into correct, absolute paths |
||
459 | * |
||
460 | * @param array $reference |
||
461 | * @return array |
||
462 | */ |
||
463 | protected function ensureAbsolutePaths(array $reference) |
||
467 | |||
468 | /** |
||
469 | * @param string $path |
||
470 | * @return string |
||
471 | */ |
||
472 | protected function ensureSuffixedPath($path) |
||
476 | |||
477 | /** |
||
478 | * Extract an array of three arrays of paths, one |
||
479 | * for each of the types of Fluid file resources. |
||
480 | * Accepts one or both of the singular and plural |
||
481 | * path definitions in the input - returns the |
||
482 | * combined collections of paths based on both |
||
483 | * the singular and plural entries with the singular |
||
484 | * entries being recorded first and plurals second. |
||
485 | * |
||
486 | * Adds legacy singular name as last option, if set. |
||
487 | * |
||
488 | * @param array $paths |
||
489 | * @return array |
||
490 | */ |
||
491 | protected function extractPathArrays(array $paths) |
||
515 | |||
516 | /** |
||
517 | * @param string $packageName |
||
518 | * @return string |
||
519 | */ |
||
520 | protected function getPackagePath($packageName) |
||
524 | |||
525 | /** |
||
526 | * Returns a unique identifier for the resolved layout file. |
||
527 | * This identifier is based on the template path and last modification date |
||
528 | * |
||
529 | * @param string $layoutName The name of the layout |
||
530 | * @return string layout identifier |
||
531 | */ |
||
532 | public function getLayoutIdentifier($layoutName = 'Default') |
||
539 | |||
540 | /** |
||
541 | * Resolve the path and file name of the layout file, based on |
||
542 | * $this->layoutPathAndFilename and $this->layoutPathAndFilenamePattern. |
||
543 | * |
||
544 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
545 | * this method returns that path, otherwise a path and filename will be |
||
546 | * resolved using the layoutPathAndFilenamePattern. |
||
547 | * |
||
548 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
549 | * @return string Path and filename of layout file |
||
550 | * @throws InvalidTemplateResourceException |
||
551 | */ |
||
552 | public function getLayoutSource($layoutName = 'Default') |
||
557 | |||
558 | /** |
||
559 | * Returns a unique identifier for the resolved template file |
||
560 | * This identifier is based on the template path and last modification date |
||
561 | * |
||
562 | * @param string $controller |
||
563 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
564 | * @return string template identifier |
||
565 | */ |
||
566 | public function getTemplateIdentifier($controller = 'Default', $action = 'Default') |
||
575 | |||
576 | /** |
||
577 | * @param mixed $source |
||
578 | */ |
||
579 | public function setTemplateSource($source) |
||
583 | |||
584 | /** |
||
585 | * Resolve the template path and filename for the given action. If $actionName |
||
586 | * is NULL, looks into the current request. |
||
587 | * |
||
588 | * @param string $controller |
||
589 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
590 | * @return string Full path to template |
||
591 | * @throws InvalidTemplateResourceException |
||
592 | */ |
||
593 | public function getTemplateSource($controller = 'Default', $action = 'Default') |
||
619 | |||
620 | /** |
||
621 | * Returns a unique identifier for the given file in the format |
||
622 | * <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1> |
||
623 | * The SH1 hash is a checksum that is based on the file path and last modification date |
||
624 | * |
||
625 | * @param string $pathAndFilename |
||
626 | * @param string $prefix |
||
627 | * @return string |
||
628 | */ |
||
629 | protected function createIdentifierForFile($pathAndFilename, $prefix) |
||
634 | |||
635 | /** |
||
636 | * Resolve the path and file name of the layout file, based on |
||
637 | * $this->options['layoutPathAndFilename'] and $this->options['layoutPathAndFilenamePattern']. |
||
638 | * |
||
639 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
640 | * this method returns that path, otherwise a path and filename will be |
||
641 | * resolved using the layoutPathAndFilenamePattern. |
||
642 | * |
||
643 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
644 | * @return string Path and filename of layout files |
||
645 | * @throws Exception\InvalidTemplateResourceException |
||
646 | */ |
||
647 | public function getLayoutPathAndFilename($layoutName = 'Default') |
||
660 | |||
661 | /** |
||
662 | * Returns a unique identifier for the resolved partial file. |
||
663 | * This identifier is based on the template path and last modification date |
||
664 | * |
||
665 | * @param string $partialName The name of the partial |
||
666 | * @return string partial identifier |
||
667 | */ |
||
668 | public function getPartialIdentifier($partialName) |
||
678 | |||
679 | /** |
||
680 | * Figures out which partial to use. |
||
681 | * |
||
682 | * @param string $partialName The name of the partial |
||
683 | * @return string contents of the partial template |
||
684 | * @throws InvalidTemplateResourceException |
||
685 | */ |
||
686 | public function getPartialSource($partialName) |
||
691 | |||
692 | /** |
||
693 | * Resolve the partial path and filename based on $this->options['partialPathAndFilenamePattern']. |
||
694 | * |
||
695 | * @param string $partialName The name of the partial |
||
696 | * @return string the full path which should be used. The path definitely exists. |
||
697 | * @throws InvalidTemplateResourceException |
||
698 | */ |
||
699 | public function getPartialPathAndFilename($partialName) |
||
709 | |||
710 | /** |
||
711 | * @param array $paths |
||
712 | * @param string $relativePathAndFilename |
||
713 | * @param string $format Optional format to resolve. |
||
714 | * @return string |
||
715 | * @throws \TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException |
||
716 | */ |
||
717 | protected function resolveFileInPaths(array $paths, $relativePathAndFilename, $format = null) |
||
741 | |||
742 | /** |
||
743 | * @param string|NULL $type |
||
744 | * @return void |
||
745 | */ |
||
746 | protected function clearResolvedIdentifiersAndTemplates($type = null) |
||
758 | } |
||
759 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.