Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 = self::DEFAULT_FORMAT) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string|NULL $controllerName |
||
| 276 | * @param string $format |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function resolveAvailableTemplateFiles($controllerName, $format = self::DEFAULT_FORMAT) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $format |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function resolveAvailablePartialFiles($format = self::DEFAULT_FORMAT) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $format |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function resolveAvailableLayoutFiles($format = self::DEFAULT_FORMAT) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $folders |
||
| 308 | * @param string $format |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | protected function resolveFilesInFolders(array $folders, $format) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $folder |
||
| 322 | * @param string $format |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | protected function resolveFilesInFolder($folder, $format) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Fills path arrays based on a traditional |
||
| 333 | * TypoScript array which may contain one or |
||
| 334 | * more of the supported structures, in order |
||
| 335 | * of priority: |
||
| 336 | * |
||
| 337 | * - `plugin.tx_yourext.view.templateRootPath` and siblings. |
||
| 338 | * - `plugin.tx_yourext.view.templateRootPaths` and siblings. |
||
| 339 | * - `plugin.tx_yourext.view.overlays.otherextension.templateRootPath` and siblings. |
||
| 340 | * |
||
| 341 | * The paths are treated as follows, using the |
||
| 342 | * `template`-type paths as an example: |
||
| 343 | * |
||
| 344 | * - If `templateRootPath` is defined, it gets |
||
| 345 | * used as the _first_ path in the internal |
||
| 346 | * paths array. |
||
| 347 | * - If `templateRootPaths` is defined, all |
||
| 348 | * values from it are _appended_ to the |
||
| 349 | * internal paths array. |
||
| 350 | * - If `overlays.*` exists in the array it is |
||
| 351 | * iterated, each `templateRootPath` entry |
||
| 352 | * from it _appended_ to the internal array. |
||
| 353 | * |
||
| 354 | * The result is that after filling, the path |
||
| 355 | * arrays will contain one or more entries in |
||
| 356 | * the order described above, depending on how |
||
| 357 | * many of the possible configurations were |
||
| 358 | * present in the input array. |
||
| 359 | * |
||
| 360 | * Will replace any currently configured paths. |
||
| 361 | * |
||
| 362 | * @param array $paths |
||
| 363 | * @return void |
||
| 364 | * @api |
||
| 365 | */ |
||
| 366 | public function fillFromConfigurationArray(array $paths) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Fills path arrays with default expected paths |
||
| 377 | * based on package name (converted to extension |
||
| 378 | * key automatically). |
||
| 379 | * |
||
| 380 | * Will replace any currently configured paths. |
||
| 381 | * |
||
| 382 | * @param string $packageName |
||
| 383 | * @return void |
||
| 384 | * @api |
||
| 385 | */ |
||
| 386 | public function fillDefaultsByPackageName($packageName) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Sanitize a path, ensuring it is absolute and |
||
| 396 | * if a directory, suffixed by a trailing slash. |
||
| 397 | * |
||
| 398 | * @param string|array $path |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | protected function sanitizePath($path) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Sanitize paths passing each through sanitizePath(). |
||
| 420 | * |
||
| 421 | * @param array $paths |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function sanitizePaths(array $paths) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Guarantees that $reference is turned into a |
||
| 431 | * correct, absolute path. |
||
| 432 | * |
||
| 433 | * @param string $path |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | protected function ensureAbsolutePath($path) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Guarantees that array $reference with paths |
||
| 443 | * are turned into correct, absolute paths |
||
| 444 | * |
||
| 445 | * @param array $reference |
||
| 446 | * @return array |
||
| 447 | */ |
||
| 448 | protected function ensureAbsolutePaths(array $reference) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $path |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | protected function ensureSuffixedPath($path) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Extract an array of three arrays of paths, one |
||
| 464 | * for each of the types of Fluid file resources. |
||
| 465 | * Accepts one or both of the singular and plural |
||
| 466 | * path definitions in the input - returns the |
||
| 467 | * combined collections of paths based on both |
||
| 468 | * the singular and plural entries with the singular |
||
| 469 | * entries being recorded first and plurals second. |
||
| 470 | * |
||
| 471 | * Adds legacy singular name as last option, if set. |
||
| 472 | * |
||
| 473 | * @param array $paths |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | protected function extractPathArrays(array $paths) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $packageName |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | protected function getPackagePath($packageName) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns a unique identifier for the resolved layout file. |
||
| 512 | * This identifier is based on the template path and last modification date |
||
| 513 | * |
||
| 514 | * @param string $layoutName The name of the layout |
||
| 515 | * @return string layout identifier |
||
| 516 | */ |
||
| 517 | public function getLayoutIdentifier($layoutName = 'Default') |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Resolve the path and file name of the layout file, based on |
||
| 527 | * $this->layoutPathAndFilename and $this->layoutPathAndFilenamePattern. |
||
| 528 | * |
||
| 529 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
| 530 | * this method returns that path, otherwise a path and filename will be |
||
| 531 | * resolved using the layoutPathAndFilenamePattern. |
||
| 532 | * |
||
| 533 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
| 534 | * @return string Path and filename of layout file |
||
| 535 | * @throws InvalidTemplateResourceException |
||
| 536 | */ |
||
| 537 | public function getLayoutSource($layoutName = 'Default') |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns a unique identifier for the resolved template file |
||
| 545 | * This identifier is based on the template path and last modification date |
||
| 546 | * |
||
| 547 | * @param string $controller |
||
| 548 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
| 549 | * @return string template identifier |
||
| 550 | */ |
||
| 551 | public function getTemplateIdentifier($controller = 'Default', $action = 'Default') |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @param mixed $source |
||
| 564 | */ |
||
| 565 | public function setTemplateSource($source) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Resolve the template path and filename for the given action. If $actionName |
||
| 572 | * is NULL, looks into the current request. |
||
| 573 | * |
||
| 574 | * @param string $controller |
||
| 575 | * @param string $action Name of the action. If NULL, will be taken from request. |
||
| 576 | * @return string Full path to template |
||
| 577 | * @throws InvalidTemplateResourceException |
||
| 578 | */ |
||
| 579 | public function getTemplateSource($controller = 'Default', $action = 'Default') |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Returns a unique identifier for the given file in the format |
||
| 608 | * <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1> |
||
| 609 | * The SH1 hash is a checksum that is based on the file path and last modification date |
||
| 610 | * |
||
| 611 | * @param string $pathAndFilename |
||
| 612 | * @param string $prefix |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | protected function createIdentifierForFile($pathAndFilename, $prefix) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Resolve the path and file name of the layout file, based on |
||
| 623 | * $this->options['layoutPathAndFilename'] and $this->options['layoutPathAndFilenamePattern']. |
||
| 624 | * |
||
| 625 | * In case a layout has already been set with setLayoutPathAndFilename(), |
||
| 626 | * this method returns that path, otherwise a path and filename will be |
||
| 627 | * resolved using the layoutPathAndFilenamePattern. |
||
| 628 | * |
||
| 629 | * @param string $layoutName Name of the layout to use. If none given, use "Default" |
||
| 630 | * @return string Path and filename of layout files |
||
| 631 | * @throws Exception\InvalidTemplateResourceException |
||
| 632 | */ |
||
| 633 | View Code Duplication | public function getLayoutPathAndFilename($layoutName = 'Default') |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Returns a unique identifier for the resolved partial file. |
||
| 647 | * This identifier is based on the template path and last modification date |
||
| 648 | * |
||
| 649 | * @param string $partialName The name of the partial |
||
| 650 | * @return string partial identifier |
||
| 651 | */ |
||
| 652 | public function getPartialIdentifier($partialName) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Figures out which partial to use. |
||
| 665 | * |
||
| 666 | * @param string $partialName The name of the partial |
||
| 667 | * @return string contents of the partial template |
||
| 668 | * @throws InvalidTemplateResourceException |
||
| 669 | */ |
||
| 670 | public function getPartialSource($partialName) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Resolve the partial path and filename based on $this->options['partialPathAndFilenamePattern']. |
||
| 678 | * |
||
| 679 | * @param string $partialName The name of the partial |
||
| 680 | * @return string the full path which should be used. The path definitely exists. |
||
| 681 | * @throws InvalidTemplateResourceException |
||
| 682 | */ |
||
| 683 | View Code Duplication | public function getPartialPathAndFilename($partialName) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * @param array $paths |
||
| 697 | * @param string $relativePathAndFilename |
||
| 698 | * @return string |
||
| 699 | * @throws \TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException |
||
| 700 | */ |
||
| 701 | protected function resolveFileInPaths(array $paths, $relativePathAndFilename, $format = self::DEFAULT_FORMAT) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param string|NULL $type |
||
| 727 | * @return void |
||
| 728 | */ |
||
| 729 | protected function clearResolvedIdentifiersAndTemplates($type = null) |
||
| 741 | } |
||
| 742 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.