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 |
||
41 | class Template |
||
42 | { |
||
43 | const CLEAN_TEMPLATE_YES = true; |
||
44 | const CLEAN_TEMPLATE_NO = false; |
||
45 | |||
46 | protected $prefix; |
||
47 | protected $cObj; |
||
48 | protected $templateFile; |
||
49 | protected $template; |
||
50 | protected $workOnSubpart; |
||
51 | protected $viewHelperIncludePath; |
||
52 | protected $helpers = []; |
||
53 | protected $loadedHelperFiles = []; |
||
54 | protected $variables = []; |
||
55 | protected $markers = []; |
||
56 | protected $subparts = []; |
||
57 | protected $loops = []; |
||
58 | |||
59 | protected $debugMode = false; |
||
60 | |||
61 | /** |
||
62 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
63 | */ |
||
64 | protected $logger = null; |
||
65 | |||
66 | /** |
||
67 | * Constructor for the html marker template engine. |
||
68 | * |
||
69 | * @param ContentObjectRenderer $contentObject content object |
||
70 | * @param string $templateFile path to the template file |
||
71 | * @param string $subpart name of the subpart to work on |
||
72 | */ |
||
73 | 26 | public function __construct( |
|
86 | |||
87 | /** |
||
88 | * @return \TYPO3\CMS\Core\Service\MarkerBasedTemplateService |
||
89 | */ |
||
90 | 26 | protected function getTemplateService() |
|
94 | |||
95 | /** |
||
96 | * Copy constructor, sets the clone object's template content to original |
||
97 | * object's work subpart. |
||
98 | * |
||
99 | */ |
||
100 | 25 | public function __clone() |
|
104 | |||
105 | /** |
||
106 | * Loads the content of a html template file. Resolves paths beginning with |
||
107 | * "EXT:". |
||
108 | * |
||
109 | * @param string $htmlFile path to html template file |
||
110 | */ |
||
111 | 26 | public function loadHtmlFile($htmlFile) |
|
124 | |||
125 | /** |
||
126 | * Sets the content for the template we're working on |
||
127 | * |
||
128 | * @param string $templateContent the template's content - usually HTML |
||
129 | * @return void |
||
130 | */ |
||
131 | 25 | public function setWorkingTemplateContent($templateContent) |
|
135 | |||
136 | /** |
||
137 | * Finds the view helpers in the template and loads them. |
||
138 | * |
||
139 | * @param string $content |
||
140 | */ |
||
141 | 26 | protected function initializeViewHelpers($content) |
|
159 | |||
160 | /** |
||
161 | * Adds an include path where the template engine should look for template |
||
162 | * view helpers. |
||
163 | * |
||
164 | * @param string $extensionKey Extension key |
||
165 | * @param string $viewHelperPath Path inside the extension to look for view helpers |
||
166 | */ |
||
167 | 26 | public function addViewHelperIncludePath($extensionKey, $viewHelperPath) |
|
171 | |||
172 | /** |
||
173 | * Adds a view helper |
||
174 | * |
||
175 | * @param string $helperName view helper name |
||
176 | * @param array $arguments optional array of arguments |
||
177 | * @return bool |
||
178 | */ |
||
179 | 26 | public function addViewHelper($helperName, array $arguments = []) |
|
210 | |||
211 | 26 | protected function loadViewHelper($helperKey) |
|
249 | |||
250 | /** |
||
251 | * adds an already instantiated view helper |
||
252 | * |
||
253 | * @param $helperName |
||
254 | * @param ViewHelper $helperObject |
||
255 | * @return bool |
||
256 | */ |
||
257 | 26 | public function addViewHelperObject($helperName, ViewHelper $helperObject) |
|
270 | |||
271 | /** |
||
272 | * Renders the template and fills its markers. |
||
273 | * |
||
274 | * @param bool $cleanTemplate |
||
275 | * @return string the rendered html template with markers replaced with their content |
||
276 | */ |
||
277 | 26 | public function render($cleanTemplate = false) |
|
332 | |||
333 | /** |
||
334 | * Escapes marker hashes and the pipe symbol so that they will not be |
||
335 | * executed in templates. |
||
336 | * |
||
337 | * @param string $content Content potentially containing markers |
||
338 | * @return string Content with markers escaped |
||
339 | */ |
||
340 | 25 | public static function escapeMarkers($content) |
|
349 | |||
350 | /** |
||
351 | * cleans the template from non-replaced markers and subparts |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | 26 | public function cleanTemplate() |
|
393 | |||
394 | /** |
||
395 | * Renders view helpers, detects whether it is a regular marker view helper |
||
396 | * or a subpart view helper and passes rendering on to more specialized |
||
397 | * render methods for each type. |
||
398 | * |
||
399 | * @param string $content The content to process by view helpers |
||
400 | * @return string the view helper processed content |
||
401 | */ |
||
402 | 26 | protected function renderViewHelpers($content) |
|
422 | |||
423 | /** |
||
424 | * Renders single marker view helpers. |
||
425 | * |
||
426 | * @param ViewHelper $viewHelper View helper instance to execute. |
||
427 | * @param string $helperKey The view helper marker key. |
||
428 | * @param string $content Markup that contains the unsubstituted view helper marker. |
||
429 | * @return string Markup with the view helper replaced by the content it returned. |
||
430 | */ |
||
431 | 24 | protected function renderMarkerViewHelper( |
|
468 | |||
469 | /** |
||
470 | * Renders subpart view helpers. |
||
471 | * |
||
472 | * @param SubpartViewHelper $viewHelper View helper instance to execute. |
||
473 | * @param string $helperKey The view helper marker key. |
||
474 | * @param string $content Markup that contains the unsubstituted view helper subpart. |
||
475 | * @return string Markup with the view helper replaced by the content it returned. |
||
476 | */ |
||
477 | protected function renderSubpartViewHelper( |
||
535 | |||
536 | /** |
||
537 | * Renders the loop for a given loop name. |
||
538 | * |
||
539 | * @param string $loopName Key from $this->loops to render |
||
540 | */ |
||
541 | 23 | protected function renderLoop($loopName) |
|
616 | |||
617 | /** |
||
618 | * Processes marker in a loop that start with LOOP:. |
||
619 | * |
||
620 | * This is useful especially for calling view helpers with the current |
||
621 | * iteration's value as a parameter. |
||
622 | * |
||
623 | * @param string $content |
||
624 | * @param string $loopName |
||
625 | * @param array $markers |
||
626 | * @param string $currentIterationValue |
||
627 | * @return string |
||
628 | */ |
||
629 | 20 | protected function processInLoopMarkers( |
|
666 | |||
667 | /** |
||
668 | * Some marker subparts must be protected and only rendered by their |
||
669 | * according commands. This method filters these protected markers from |
||
670 | * others when rendering loops so that they are not replaced and left in |
||
671 | * the template for rendering by the correct command. |
||
672 | * |
||
673 | * @param array $loopMarkers An array of loop markers found during rendering of a loop. |
||
674 | * @return array The array with protected subpart markers removed. |
||
675 | */ |
||
676 | 20 | protected function filterProtectedLoops($loopMarkers) |
|
688 | |||
689 | /** |
||
690 | * Processes conditions: finds and evaluates them in HTML code. |
||
691 | * |
||
692 | * @param string $content HTML |
||
693 | * @return string |
||
694 | */ |
||
695 | 26 | protected function processConditions($content) |
|
735 | |||
736 | /** |
||
737 | * Finds conditions in HTML code. |
||
738 | * |
||
739 | * Conditions are subparts with markers in the form of |
||
740 | * |
||
741 | * ###IF:comparand1|operator|comparand2### |
||
742 | * Some content only visible if the condition evaluates as TRUE |
||
743 | * ###IF:comparand1|operator|comparand2### |
||
744 | * |
||
745 | * The returned result is an array of arrays describing a found condition. |
||
746 | * Each conditions is described as follows: |
||
747 | * [marker] the complete marker used to specify the condition |
||
748 | * [content] the content wrapped by the condition |
||
749 | * [operator] the condition operator |
||
750 | * [comparand1] and [comparand2] the comparands. |
||
751 | * |
||
752 | * @param string $content HTML |
||
753 | * @return array An array describing the conditions found |
||
754 | */ |
||
755 | 26 | protected function findConditions($content) |
|
779 | |||
780 | /** |
||
781 | * Evaluates conditions. |
||
782 | * |
||
783 | * Supported operators are ==, !=, <, <=, >, >=, % |
||
784 | * |
||
785 | * @param string $comparand1 First comparand |
||
786 | * @param string $comparand2 Second comparand |
||
787 | * @param string $operator Operator |
||
788 | * @return bool Boolean evaluation of the condition. |
||
789 | * @throws \InvalidArgumentException for unknown $operator |
||
790 | */ |
||
791 | 20 | protected function evaluateCondition($comparand1, $comparand2, $operator) |
|
827 | |||
828 | /** |
||
829 | * Resolves variables to marker. Markers can be simple markers like |
||
830 | * ###MY_MARKER## or "nested" markers which divide their sub values by a |
||
831 | * dot: ###MY_MARKER.MY_VALUE### ###MY_MARKER.MY_OTHER_VALUE###. |
||
832 | * |
||
833 | * @param array $markers array with markers to resolve |
||
834 | * @param mixed $variableValue the marker's value, which can be an array of values, an object with certain getter methods or a simple string |
||
835 | * @return array with marker as index and value for it |
||
836 | */ |
||
837 | 25 | protected function resolveVariableMarkers(array $markers, $variableValue) |
|
888 | |||
889 | /** |
||
890 | * Normalizes the various input formats of the markers to a common format. |
||
891 | * |
||
892 | * Example: |
||
893 | * |
||
894 | * FILE_MIME_TYPE_STRING_S => file_mime_type_string_s |
||
895 | * file_mime_type_string_s => file_mime_type_string_s |
||
896 | * fileMimeType_stringS => file_mime_type_string_s |
||
897 | * |
||
898 | * @param string $selector A string in upper case with underscores, lowercase with underscores, camel case, or a mix. |
||
899 | * @return string A lowercased, underscorized version of the given string |
||
900 | */ |
||
901 | 25 | protected function normalizeString($selector) |
|
921 | |||
922 | /** |
||
923 | * Selects a subpart to work on / to apply all operations to. |
||
924 | * |
||
925 | * @param string $subpartName subpart name |
||
926 | */ |
||
927 | 26 | public function workOnSubpart($subpartName) |
|
931 | |||
932 | /** |
||
933 | * Retrieves a subpart from the given html template. |
||
934 | * |
||
935 | * @param string $subpartName subpart marker name, can be lowercase, doesn't need the ### delimiters |
||
936 | * @param string $alternativeTemplate |
||
937 | * @return string the html subpart |
||
938 | */ |
||
939 | 26 | public function getSubpart($subpartName, $alternativeTemplate = '') |
|
955 | |||
956 | /** |
||
957 | * Sets a marker's value. |
||
958 | * |
||
959 | * @param string $marker marker name, can be lower case, doesn't need the ### delimiters |
||
960 | * @param string $content the marker's value |
||
961 | */ |
||
962 | public function addMarker($marker, $content) |
||
966 | |||
967 | /** |
||
968 | * Sets an array of markers with their values. |
||
969 | * |
||
970 | * @param array $markers array of markers |
||
971 | */ |
||
972 | public function addMarkerArray(array $markers) |
||
978 | |||
979 | /** |
||
980 | * Sets a subpart's value. |
||
981 | * |
||
982 | * @param string $subpartMarker subpart name, can be lower case, doesn't need the ### delimiters |
||
983 | * @param string $content the subpart's value |
||
984 | */ |
||
985 | 25 | public function addSubpart($subpartMarker, $content) |
|
989 | |||
990 | /** |
||
991 | * Assigns a variable to the html template. |
||
992 | * Simple variables can be used like regular markers or in the form |
||
993 | * VAR:"VARIABLE_NAME" (without the quotes). Objects can be used in the |
||
994 | * form VAR:"OBJECT_NAME"."PROPERTY_NAME" (without the quotes). |
||
995 | * |
||
996 | * @param string $key variable key |
||
997 | * @param mixed $value variable value |
||
998 | */ |
||
999 | 25 | public function addVariable($key, $value) |
|
1005 | |||
1006 | /** |
||
1007 | * Adds a named loop. The given array is looped over in the template. |
||
1008 | * |
||
1009 | * @param string $loopName loop name |
||
1010 | * @param string $markerName |
||
1011 | * @param array $variables variables array |
||
1012 | */ |
||
1013 | 23 | public function addLoop($loopName, $markerName, array $variables) |
|
1020 | |||
1021 | /** |
||
1022 | * Gets a list of Markers from the selected subpart. |
||
1023 | * |
||
1024 | * @param string $template marker name |
||
1025 | * @param string $markerPrefix |
||
1026 | * @param bool $capturePrefix |
||
1027 | * @return array Array of markers |
||
1028 | */ |
||
1029 | 26 | public function getMarkersFromTemplate( |
|
1049 | |||
1050 | /** |
||
1051 | * returns the markers found in the template |
||
1052 | * |
||
1053 | * @param string $markerPrefix a prefix to limit the result to markers beginning with the specified prefix |
||
1054 | * @return array Array of markers names |
||
1055 | */ |
||
1056 | 26 | public function findMarkers($markerPrefix = '') |
|
1061 | |||
1062 | /** |
||
1063 | * Gets a list of view helper marker arguments for a given view helper from |
||
1064 | * the selected subpart. |
||
1065 | * |
||
1066 | * @param string $helperMarker marker name, can be lower case, doesn't need the ### delimiters |
||
1067 | * @param string $subpart subpart markup to search in |
||
1068 | * @param bool $removeDuplicates Optionally determines whether duplicate view helpers are removed. Defaults to TRUE. |
||
1069 | * @return array Array of markers |
||
1070 | */ |
||
1071 | 26 | protected function getViewHelperArgumentLists($helperMarker, $subpart, $removeDuplicates = true) |
|
1081 | |||
1082 | /** |
||
1083 | * This helper function is used to extract all ViewHelper arguments by a name of the ViewHelper. |
||
1084 | * |
||
1085 | * Arguments can be: strings or even other markers. |
||
1086 | * |
||
1087 | * @param string $helperMarker |
||
1088 | * @param string $subpart |
||
1089 | * @return array |
||
1090 | */ |
||
1091 | 36 | public static function extractViewHelperArguments($helperMarker, $subpart) |
|
1104 | |||
1105 | /** |
||
1106 | * Finds view helpers used in the current subpart being worked on. |
||
1107 | * |
||
1108 | * @param string $content A string that should be searched for view helpers. |
||
1109 | * @return array A list of view helper names used in the template. |
||
1110 | */ |
||
1111 | 26 | public function findViewHelpers($content) |
|
1134 | |||
1135 | /** |
||
1136 | * Gets a list of given markers from the selected subpart. |
||
1137 | * |
||
1138 | * @param string $variableMarker marker name, can be lower case, doesn't need the ### delimiters |
||
1139 | * @param string $subpart subpart name |
||
1140 | * @return array array of markers |
||
1141 | */ |
||
1142 | 25 | public function getVariableMarkers($variableMarker, $subpart) |
|
1153 | |||
1154 | /** |
||
1155 | * Checks whether a given string is a variable marker |
||
1156 | * |
||
1157 | * @param string $potentialVariableMarker String to check whether it is a variable marker |
||
1158 | * @return bool TRUE if the string is identified being a variable marker, FALSE otherwise |
||
1159 | */ |
||
1160 | 20 | public function isVariableMarker($potentialVariableMarker) |
|
1167 | |||
1168 | /** |
||
1169 | * @param $templateContent |
||
1170 | * @return mixed |
||
1171 | */ |
||
1172 | 25 | public function setTemplateContent($templateContent) |
|
1176 | |||
1177 | public function getTemplateContent() |
||
1181 | |||
1182 | 25 | public function getWorkOnSubpart() |
|
1186 | |||
1187 | /** |
||
1188 | * Sets the debug mode on or off. |
||
1189 | * |
||
1190 | * @param bool $mode debug mode, TRUE to enable debug mode, FALSE to turn off again, off by default |
||
1191 | */ |
||
1192 | public function setDebugMode($mode) |
||
1196 | } |
||
1197 |
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: