Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Template |
||
| 40 | { |
||
| 41 | const CLEAN_TEMPLATE_YES = true; |
||
| 42 | const CLEAN_TEMPLATE_NO = false; |
||
| 43 | |||
| 44 | protected $prefix; |
||
| 45 | protected $cObj; |
||
| 46 | protected $templateFile; |
||
| 47 | protected $template; |
||
| 48 | protected $workOnSubpart; |
||
| 49 | protected $viewHelperIncludePath; |
||
| 50 | protected $helpers = array(); |
||
| 51 | protected $loadedHelperFiles = array(); |
||
| 52 | protected $variables = array(); |
||
| 53 | protected $markers = array(); |
||
| 54 | protected $subparts = array(); |
||
| 55 | protected $loops = array(); |
||
| 56 | |||
| 57 | protected $debugMode = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor for the html marker template engine. |
||
| 61 | * |
||
| 62 | * @param ContentObjectRenderer $contentObject content object |
||
| 63 | * @param string $templateFile path to the template file |
||
| 64 | * @param string $subpart name of the subpart to work on |
||
| 65 | */ |
||
| 66 | 26 | public function __construct( |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @return \TYPO3\CMS\Core\Service\MarkerBasedTemplateService |
||
| 80 | */ |
||
| 81 | 26 | protected function getTemplateService() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Copy constructor, sets the clone object's template content to original |
||
| 88 | * object's work subpart. |
||
| 89 | * |
||
| 90 | */ |
||
| 91 | 25 | public function __clone() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Loads the content of a html template file. Resolves paths beginning with |
||
| 98 | * "EXT:". |
||
| 99 | * |
||
| 100 | * @param string $htmlFile path to html template file |
||
| 101 | */ |
||
| 102 | 26 | public function loadHtmlFile($htmlFile) |
|
| 103 | { |
||
| 104 | 26 | $path = $GLOBALS['TSFE']->tmpl->getFileName($htmlFile); |
|
| 105 | 26 | if ($path !== null && file_exists($path)) { |
|
| 106 | 26 | $this->template = file_get_contents($path); |
|
| 107 | } |
||
| 108 | 26 | if (empty($this->template)) { |
|
| 109 | throw new \RuntimeException( |
||
| 110 | 'Could not load template file "' . htmlspecialchars($htmlFile) . '"', |
||
| 111 | 1327490358 |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | 26 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Sets the content for the template we're working on |
||
| 118 | * |
||
| 119 | * @param string $templateContent the template's content - usually HTML |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | 25 | public function setWorkingTemplateContent($templateContent) |
|
| 123 | { |
||
| 124 | 25 | $this->workOnSubpart = $templateContent; |
|
| 125 | 25 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Finds the view helpers in the template and loads them. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 26 | protected function initializeViewHelpers($content) |
|
| 133 | { |
||
| 134 | 26 | $viewHelpersFound = $this->findViewHelpers($content); |
|
| 135 | |||
| 136 | 26 | foreach ($viewHelpersFound as $helperKey) { |
|
| 137 | 24 | if (!isset($this->helpers[strtolower($helperKey)])) { |
|
| 138 | 18 | $viewHelperLoaded = $this->loadViewHelper($helperKey); |
|
| 139 | |||
| 140 | 18 | if (!$viewHelperLoaded) { |
|
| 141 | // skipping processing in case we couldn't find a class |
||
| 142 | // to handle the view helper |
||
| 143 | continue; |
||
| 144 | } |
||
| 145 | |||
| 146 | 24 | $this->addViewHelper($helperKey); |
|
| 147 | } |
||
| 148 | } |
||
| 149 | 26 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Adds an include path where the template engine should look for template |
||
| 153 | * view helpers. |
||
| 154 | * |
||
| 155 | * @param string $extensionKey Extension key |
||
| 156 | * @param string $viewHelperPath Path inside the extension to look for view helpers |
||
| 157 | */ |
||
| 158 | 26 | public function addViewHelperIncludePath($extensionKey, $viewHelperPath) |
|
| 159 | { |
||
| 160 | 26 | $this->viewHelperIncludePath[$extensionKey] = $viewHelperPath; |
|
| 161 | 26 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Adds a view helper |
||
| 165 | * |
||
| 166 | * @param string $helperName view helper name |
||
| 167 | * @param array $arguments optional array of arguments |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 26 | public function addViewHelper($helperName, array $arguments = array()) |
|
| 198 | |||
| 199 | 26 | protected function loadViewHelper($helperKey) |
|
| 200 | { |
||
| 201 | 26 | if (isset($this->loadedHelperFiles[strtolower($helperKey)])) { |
|
| 202 | 18 | return $this->loadedHelperFiles[strtolower($helperKey)]['class']; |
|
| 237 | |||
| 238 | /** |
||
| 239 | * adds an already instantiated view helper |
||
| 240 | * |
||
| 241 | * @param $helperName |
||
| 242 | * @param ViewHelper $helperObject |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 26 | public function addViewHelperObject($helperName, ViewHelper $helperObject) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Renders the template and fills its markers. |
||
| 261 | * |
||
| 262 | * @return string the rendered html template with markers replaced with their content |
||
| 263 | */ |
||
| 264 | 26 | public function render($cleanTemplate = false) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Escapes marker hashes and the pipe symbol so that they will not be |
||
| 322 | * executed in templates. |
||
| 323 | * |
||
| 324 | * @param string $content Content potentially containing markers |
||
| 325 | * @return string Content with markers escaped |
||
| 326 | */ |
||
| 327 | 25 | public static function escapeMarkers($content) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * cleans the template from non-replaced markers and subparts |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | 26 | public function cleanTemplate() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Renders view helpers, detects whether it is a regular marker view helper |
||
| 383 | * or a subpart view helper and passes rendering on to more specialized |
||
| 384 | * render methods for each type. |
||
| 385 | * |
||
| 386 | * @param string $content The content to process by view helpers |
||
| 387 | * @return string the view helper processed content |
||
| 388 | */ |
||
| 389 | 26 | protected function renderViewHelpers($content) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Renders single marker view helpers. |
||
| 412 | * |
||
| 413 | * @param ViewHelper $viewHelper View helper instance to execute. |
||
| 414 | * @param string $helperKey The view helper marker key. |
||
| 415 | * @param string $content Markup that contains the unsubstituted view helper marker. |
||
| 416 | * @return string Markup with the view helper replaced by the content it returned. |
||
| 417 | */ |
||
| 418 | 24 | protected function renderMarkerViewHelper( |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Renders subpart view helpers. |
||
| 458 | * |
||
| 459 | * @param SubpartViewHelper $viewHelper View helper instance to execute. |
||
| 460 | * @param string $helperKey The view helper marker key. |
||
| 461 | * @param string $content Markup that contains the unsubstituted view helper subpart. |
||
| 462 | * @return string Markup with the view helper replaced by the content it returned. |
||
| 463 | */ |
||
| 464 | protected function renderSubpartViewHelper( |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Renders the loop for a given loop name. |
||
| 522 | * |
||
| 523 | * @param string $loopName Key from $this->loops to render |
||
| 524 | */ |
||
| 525 | 23 | protected function renderLoop($loopName) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Processes marker in a loop that start with LOOP:. |
||
| 603 | * |
||
| 604 | * This is useful especially for calling view helpers with the current |
||
| 605 | * iteration's value as a parameter. |
||
| 606 | * |
||
| 607 | * @param string $content |
||
| 608 | * @param string $loopName |
||
| 609 | * @param array $markers |
||
| 610 | * @param string $currentIterationValue |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | 20 | protected function processInLoopMarkers( |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Some marker subparts must be protected and only rendered by their |
||
| 653 | * according commands. This method filters these protected markers from |
||
| 654 | * others when rendering loops so that they are not replaced and left in |
||
| 655 | * the template for rendering by the correct command. |
||
| 656 | * |
||
| 657 | * @param array $loopMarkers An array of loop markers found during rendering of a loop. |
||
| 658 | * @return array The array with protected subpart markers removed. |
||
| 659 | */ |
||
| 660 | 20 | protected function filterProtectedLoops($loopMarkers) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Processes conditions: finds and evaluates them in HTML code. |
||
| 675 | * |
||
| 676 | * @param string $content HTML |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | 26 | protected function processConditions($content) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Finds conditions in HTML code. |
||
| 722 | * |
||
| 723 | * Conditions are subparts with markers in the form of |
||
| 724 | * |
||
| 725 | * ###IF:comparand1|operator|comparand2### |
||
| 726 | * Some content only visible if the condition evaluates as TRUE |
||
| 727 | * ###IF:comparand1|operator|comparand2### |
||
| 728 | * |
||
| 729 | * The returned result is an array of arrays describing a found condition. |
||
| 730 | * Each conditions is described as follows: |
||
| 731 | * [marker] the complete marker used to specify the condition |
||
| 732 | * [content] the content wrapped by the condition |
||
| 733 | * [operator] the condition operator |
||
| 734 | * [comparand1] and [comparand2] the comparands. |
||
| 735 | * |
||
| 736 | * @param string $content HTML |
||
| 737 | * @return array An array describing the conditions found |
||
| 738 | */ |
||
| 739 | 26 | protected function findConditions($content) |
|
| 763 | |||
| 764 | /** |
||
| 765 | * Evaluates conditions. |
||
| 766 | * |
||
| 767 | * Supported operators are ==, !=, <, <=, >, >=, % |
||
| 768 | * |
||
| 769 | * @param string $comparand1 First comparand |
||
| 770 | * @param string $comparand2 Second comparand |
||
| 771 | * @param string $operator Operator |
||
| 772 | * @return bool Boolean evaluation of the condition. |
||
| 773 | * @throws \InvalidArgumentException for unknown $operator |
||
| 774 | */ |
||
| 775 | 20 | protected function evaluateCondition($comparand1, $comparand2, $operator) |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Resolves variables to marker. Markers can be simple markers like |
||
| 816 | * ###MY_MARKER## or "nested" markers which divide their sub values by a |
||
| 817 | * dot: ###MY_MARKER.MY_VALUE### ###MY_MARKER.MY_OTHER_VALUE###. |
||
| 818 | * |
||
| 819 | * @param array $markers array with markers to resolve |
||
| 820 | * @param mixed $variableValue the marker's value, which can be an array of values, an object with certain getter methods or a simple string |
||
| 821 | * @return array with marker as index and value for it |
||
| 822 | */ |
||
| 823 | 25 | protected function resolveVariableMarkers(array $markers, $variableValue) |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Normalizes the various input formats of the markers to a common format. |
||
| 877 | * |
||
| 878 | * Example: |
||
| 879 | * |
||
| 880 | * FILE_MIME_TYPE_STRING_S => file_mime_type_string_s |
||
| 881 | * file_mime_type_string_s => file_mime_type_string_s |
||
| 882 | * fileMimeType_stringS => file_mime_type_string_s |
||
| 883 | * |
||
| 884 | * @param string $selector A string in upper case with underscores, lowercase with underscores, camel case, or a mix. |
||
| 885 | * @return string A lowercased, underscorized version of the given string |
||
| 886 | */ |
||
| 887 | 25 | protected function normalizeString($selector) |
|
| 907 | |||
| 908 | /** |
||
| 909 | * Selects a subpart to work on / to apply all operations to. |
||
| 910 | * |
||
| 911 | * @param string $subpartName subpart name |
||
| 912 | */ |
||
| 913 | 26 | public function workOnSubpart($subpartName) |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Retrieves a subpart from the given html template. |
||
| 920 | * |
||
| 921 | * @param string $subpartName subpart marker name, can be lowercase, doesn't need the ### delimiters |
||
| 922 | * @param string $alternativeTemplate |
||
| 923 | * @return string the html subpart |
||
| 924 | */ |
||
| 925 | 26 | public function getSubpart($subpartName, $alternativeTemplate = '') |
|
| 941 | |||
| 942 | /** |
||
| 943 | * Sets a marker's value. |
||
| 944 | * |
||
| 945 | * @param string $marker marker name, can be lower case, doesn't need the ### delimiters |
||
| 946 | * @param string $content the marker's value |
||
| 947 | */ |
||
| 948 | public function addMarker($marker, $content) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Sets an array of markers with their values. |
||
| 955 | * |
||
| 956 | * @param array $markers array of markers |
||
| 957 | */ |
||
| 958 | public function addMarkerArray(array $markers) |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Sets a subpart's value. |
||
| 967 | * |
||
| 968 | * @param string $subpartMarker subpart name, can be lower case, doesn't need the ### delimiters |
||
| 969 | * @param string $content the subpart's value |
||
| 970 | */ |
||
| 971 | 25 | public function addSubpart($subpartMarker, $content) |
|
| 975 | |||
| 976 | /** |
||
| 977 | * Assigns a variable to the html template. |
||
| 978 | * Simple variables can be used like regular markers or in the form |
||
| 979 | * VAR:"VARIABLE_NAME" (without the quotes). Objects can be used in the |
||
| 980 | * form VAR:"OBJECT_NAME"."PROPERTY_NAME" (without the quotes). |
||
| 981 | * |
||
| 982 | * @param string $key variable key |
||
| 983 | * @param mixed $value variable value |
||
| 984 | */ |
||
| 985 | 25 | public function addVariable($key, $value) |
|
| 991 | |||
| 992 | /** |
||
| 993 | * Adds a named loop. The given array is looped over in the template. |
||
| 994 | * |
||
| 995 | * @param string $loopName loop name |
||
| 996 | * @param string $markerName |
||
| 997 | * @param array $variables variables array |
||
| 998 | */ |
||
| 999 | 23 | public function addLoop($loopName, $markerName, array $variables) |
|
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Gets a list of Markers from the selected subpart. |
||
| 1009 | * |
||
| 1010 | * @param string $template marker name |
||
| 1011 | * @param string $markerPrefix |
||
| 1012 | * @param bool $capturePrefix |
||
| 1013 | * @return array Array of markers |
||
| 1014 | */ |
||
| 1015 | 26 | public function getMarkersFromTemplate( |
|
| 1035 | |||
| 1036 | /** |
||
| 1037 | * returns the markers found in the template |
||
| 1038 | * |
||
| 1039 | * @param string $markerPrefix a prefix to limit the result to markers beginning with the specified prefix |
||
| 1040 | * @return array Array of markers names |
||
| 1041 | */ |
||
| 1042 | 26 | public function findMarkers($markerPrefix = '') |
|
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Gets a list of view helper marker arguments for a given view helper from |
||
| 1050 | * the selected subpart. |
||
| 1051 | * |
||
| 1052 | * @param string $helperMarker marker name, can be lower case, doesn't need the ### delimiters |
||
| 1053 | * @param string $subpart subpart markup to search in |
||
| 1054 | * @param bool $removeDuplicates Optionally determines whether duplicate view helpers are removed. Defaults to TRUE. |
||
| 1055 | * @return array Array of markers |
||
| 1056 | */ |
||
| 1057 | 26 | protected function getViewHelperArgumentLists($helperMarker, $subpart, $removeDuplicates = true) |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * This helper function is used to extract all ViewHelper arguments by a name of the ViewHelper. |
||
| 1070 | * |
||
| 1071 | * Arguments can be: strings or even other markers. |
||
| 1072 | * |
||
| 1073 | * @param string $helperMarker |
||
| 1074 | * @param string $subpart |
||
| 1075 | * @return array |
||
| 1076 | */ |
||
| 1077 | 36 | public static function extractViewHelperArguments($helperMarker, $subpart) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Finds view helpers used in the current subpart being worked on. |
||
| 1093 | * |
||
| 1094 | * @param string $content A string that should be searched for view helpers. |
||
| 1095 | * @return array A list of view helper names used in the template. |
||
| 1096 | */ |
||
| 1097 | 26 | public function findViewHelpers($content) |
|
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Gets a list of given markers from the selected subpart. |
||
| 1123 | * |
||
| 1124 | * @param string $variableMarker marker name, can be lower case, doesn't need the ### delimiters |
||
| 1125 | * @param string $subpart subpart name |
||
| 1126 | * @return array array of markers |
||
| 1127 | */ |
||
| 1128 | 25 | public function getVariableMarkers($variableMarker, $subpart) |
|
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Checks whether a given string is a variable marker |
||
| 1142 | * |
||
| 1143 | * @param string $potentialVariableMarker String to check whether it is a variable marker |
||
| 1144 | * @return bool TRUE if the string is identified being a variable marker, FALSE otherwise |
||
| 1145 | */ |
||
| 1146 | 20 | public function isVariableMarker($potentialVariableMarker) |
|
| 1153 | |||
| 1154 | /** |
||
| 1155 | * @param $templateContent |
||
| 1156 | * @return mixed |
||
| 1157 | */ |
||
| 1158 | 25 | public function setTemplateContent($templateContent) |
|
| 1162 | |||
| 1163 | public function getTemplateContent() |
||
| 1167 | |||
| 1168 | 25 | public function getWorkOnSubpart() |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Sets the debug mode on or off. |
||
| 1175 | * |
||
| 1176 | * @param bool $mode debug mode, TRUE to enable debug mode, FALSE to turn off again, off by default |
||
| 1177 | */ |
||
| 1178 | public function setDebugMode($mode) |
||
| 1182 | } |
||
| 1183 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: