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 PHPTAL 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 PHPTAL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class PHPTAL |
||
47 | { |
||
48 | //{{{ |
||
49 | /** |
||
50 | * constants for output mode |
||
51 | * @see setOutputMode() |
||
52 | */ |
||
53 | const XHTML = 11; |
||
54 | const XML = 22; |
||
55 | const HTML5 = 55; |
||
56 | |||
57 | /** |
||
58 | * @see getPreFilters() |
||
59 | */ |
||
60 | protected $prefilters = array(); |
||
61 | |||
62 | /** |
||
63 | * Prefilters have been redesigned. Old property is no longer used. |
||
64 | * |
||
65 | * @deprecated |
||
66 | */ |
||
67 | private $_prefilter = 'REMOVED: DO NOT USE'; |
||
68 | protected $_postfilter = null; |
||
69 | |||
70 | /** |
||
71 | * list of template source repositories given to file source resolver |
||
72 | */ |
||
73 | protected $_repositories = array(); |
||
74 | |||
75 | /** |
||
76 | * template path (path that has been set, not necessarily loaded) |
||
77 | */ |
||
78 | protected $_path = null; |
||
79 | |||
80 | /** |
||
81 | * template source resolvers (classes that search for templates by name) |
||
82 | */ |
||
83 | protected $resolvers = array(); |
||
84 | |||
85 | /** |
||
86 | * template source (only set when not working with file) |
||
87 | */ |
||
88 | protected $_source = null; |
||
89 | |||
90 | /** |
||
91 | * destination of PHP intermediate file |
||
92 | */ |
||
93 | protected $_codeFile = null; |
||
94 | |||
95 | /** |
||
96 | * php function generated for the template |
||
97 | */ |
||
98 | protected $_functionName = null; |
||
99 | |||
100 | /** |
||
101 | * set to true when template is ready for execution |
||
102 | */ |
||
103 | protected $_prepared = false; |
||
104 | |||
105 | /** |
||
106 | * associative array of phptal:id => PHPTAL_Trigger |
||
107 | */ |
||
108 | protected $_triggers = array(); |
||
109 | |||
110 | /** |
||
111 | * i18n translator |
||
112 | */ |
||
113 | protected $_translator = null; |
||
114 | |||
115 | /** |
||
116 | * global execution context |
||
117 | */ |
||
118 | protected $_globalContext = null; |
||
119 | |||
120 | /** |
||
121 | * current execution context |
||
122 | */ |
||
123 | protected $_context = null; |
||
124 | |||
125 | /** |
||
126 | * list of on-error caught exceptions |
||
127 | */ |
||
128 | protected $_errors = array(); |
||
129 | |||
130 | /** |
||
131 | * encoding used throughout |
||
132 | */ |
||
133 | protected $_encoding = 'UTF-8'; |
||
134 | |||
135 | /** |
||
136 | * type of syntax used in generated templates |
||
137 | */ |
||
138 | protected $_outputMode = PHPTAL::XHTML; |
||
139 | /** |
||
140 | * should all comments be stripped |
||
141 | */ |
||
142 | |||
143 | // configuration properties |
||
144 | |||
145 | /** |
||
146 | * don't use code cache |
||
147 | */ |
||
148 | protected $_forceReparse = null; |
||
149 | |||
150 | /** |
||
151 | * directory where code cache is |
||
152 | */ |
||
153 | private $_phpCodeDestination; |
||
154 | private $_phpCodeExtension = 'php'; |
||
155 | |||
156 | /** |
||
157 | * number of days |
||
158 | */ |
||
159 | private $_cacheLifetime = 30; |
||
160 | |||
161 | /** |
||
162 | * 1/x |
||
163 | */ |
||
164 | private $_cachePurgeFrequency = 30; |
||
165 | |||
166 | /** |
||
167 | * speeds up calls to external templates |
||
168 | */ |
||
169 | private $externalMacroTemplatesCache = array(); |
||
170 | |||
171 | //}}} |
||
172 | |||
173 | /** |
||
174 | * PHPTAL Constructor. |
||
175 | * |
||
176 | * @param string $path Template file path. |
||
177 | */ |
||
178 | public function __construct($path=false) |
||
197 | |||
198 | /** |
||
199 | * create |
||
200 | * returns a new PHPTAL object |
||
201 | * |
||
202 | * @param string $path Template file path. |
||
203 | * |
||
204 | * @return PHPTAL |
||
205 | */ |
||
206 | public static function create($path=false) |
||
210 | |||
211 | /** |
||
212 | * Clone template state and context. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function __clone() |
||
220 | |||
221 | /** |
||
222 | * Set template from file path. |
||
223 | * |
||
224 | * @param string $path filesystem path, |
||
225 | * or any path that will be accepted by source resolver |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | View Code Duplication | public function setTemplate($path) |
|
240 | |||
241 | /** |
||
242 | * Set template from source. |
||
243 | * |
||
244 | * Should be used only with temporary template sources. |
||
245 | * Use setTemplate() or addSourceResolver() whenever possible. |
||
246 | * |
||
247 | * @param string $src The phptal template source. |
||
248 | * @param string $path Fake and 'unique' template path. |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | View Code Duplication | public function setSource($src, $path = null) |
|
263 | |||
264 | /** |
||
265 | * Specify where to look for templates. |
||
266 | * |
||
267 | * @param mixed $rep string or Array of repositories |
||
268 | * |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function setTemplateRepository($rep) |
||
280 | |||
281 | /** |
||
282 | * Get template repositories. |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getTemplateRepositories() |
||
290 | |||
291 | /** |
||
292 | * Clears the template repositories. |
||
293 | * |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function clearTemplateRepositories() |
||
301 | |||
302 | /** |
||
303 | * Specify how to look for templates. |
||
304 | * |
||
305 | * @param PHPTAL_SourceResolver $resolver instance of resolver |
||
306 | * |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function addSourceResolver(PHPTAL_SourceResolver $resolver) |
||
314 | |||
315 | /** |
||
316 | * Ignore XML/XHTML comments on parsing. |
||
317 | * Comments starting with <!--! are always stripped. |
||
318 | * |
||
319 | * @param bool $bool if true all comments are stripped during parse |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | public function stripComments($bool) |
||
334 | |||
335 | /** |
||
336 | * Set output mode |
||
337 | * XHTML output mode will force elements like <link/>, <meta/> and <img/>, etc. |
||
338 | * to be empty and threats attributes like selected, checked to be |
||
339 | * boolean attributes. |
||
340 | * |
||
341 | * XML output mode outputs XML without such modifications |
||
342 | * and is neccessary to generate RSS feeds properly. |
||
343 | * |
||
344 | * @param int $mode (PHPTAL::XML, PHPTAL::XHTML or PHPTAL::HTML5). |
||
345 | * |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function setOutputMode($mode) |
||
358 | |||
359 | /** |
||
360 | * Get output mode |
||
361 | * @see setOutputMode() |
||
362 | * |
||
363 | * @return output mode constant |
||
364 | */ |
||
365 | public function getOutputMode() |
||
369 | |||
370 | /** |
||
371 | * Set input and ouput encoding. Encoding is case-insensitive. |
||
372 | * |
||
373 | * @param string $enc example: 'UTF-8' |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | public function setEncoding($enc) |
||
388 | |||
389 | /** |
||
390 | * Get input and ouput encoding. |
||
391 | * |
||
392 | * @param string $enc example: 'UTF-8' |
||
393 | * |
||
394 | * @return $this |
||
395 | */ |
||
396 | public function getEncoding() |
||
400 | |||
401 | /** |
||
402 | * Set the storage location for intermediate PHP files. |
||
403 | * The path cannot contain characters that would be interpreted by glob() (e.g. *[]?) |
||
404 | * |
||
405 | * @param string $path Intermediate file path. |
||
406 | * |
||
407 | * @return $this |
||
408 | */ |
||
409 | public function setPhpCodeDestination($path) |
||
415 | |||
416 | /** |
||
417 | * Get the storage location for intermediate PHP files. |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | public function getPhpCodeDestination() |
||
425 | |||
426 | /** |
||
427 | * Set the file extension for intermediate PHP files. |
||
428 | * |
||
429 | * @param string $extension The file extension. |
||
430 | * |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function setPhpCodeExtension($extension) |
||
439 | |||
440 | /** |
||
441 | * Get the file extension for intermediate PHP files. |
||
442 | */ |
||
443 | public function getPhpCodeExtension() |
||
447 | |||
448 | /** |
||
449 | * Flags whether to ignore intermediate php files and to |
||
450 | * reparse templates every time (if set to true). |
||
451 | * |
||
452 | * DON'T USE IN PRODUCTION - this makes PHPTAL many times slower. |
||
453 | * |
||
454 | * @param bool $bool Forced reparse state. |
||
455 | * |
||
456 | * @return $this |
||
457 | */ |
||
458 | public function setForceReparse($bool) |
||
463 | |||
464 | /** |
||
465 | * Get the value of the force reparse state. |
||
466 | */ |
||
467 | public function getForceReparse() |
||
471 | |||
472 | /** |
||
473 | * Set I18N translator. |
||
474 | * |
||
475 | * This sets encoding used by the translator, so be sure to use encoding-dependent |
||
476 | * features of the translator (e.g. addDomain) _after_ calling setTranslator. |
||
477 | * |
||
478 | * @param PHPTAL_TranslationService $t instance |
||
479 | * |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function setTranslator(PHPTAL_TranslationService $t) |
||
488 | |||
489 | |||
490 | /** |
||
491 | * Please use addPreFilter instead. |
||
492 | * |
||
493 | * This method and use of PHPTAL_Filter for prefilters are deprecated. |
||
494 | * |
||
495 | * @see PHPTAL::addPreFilter() |
||
496 | * @deprecated |
||
497 | */ |
||
498 | final public function setPreFilter(PHPTAL_Filter $filter) |
||
503 | |||
504 | /** |
||
505 | * Add new prefilter to filter chain. |
||
506 | * Prefilters are called only once template is compiled. |
||
507 | * |
||
508 | * PreFilters must inherit PHPTAL_PreFilter class. |
||
509 | * (in future this method will allow string with filter name instead of object) |
||
510 | * |
||
511 | * @param mixed $filter PHPTAL_PreFilter object or name of prefilter to add |
||
512 | * |
||
513 | * @return PHPTAL |
||
514 | */ |
||
515 | final public function addPreFilter($filter) |
||
526 | |||
527 | /** |
||
528 | * Array with all prefilter objects *or strings* that are names of prefilter classes. |
||
529 | * (the latter is not implemented in 1.2.1) |
||
530 | * |
||
531 | * Array keys may be non-numeric! |
||
532 | * |
||
533 | * @return array |
||
534 | */ |
||
535 | protected function getPreFilters() |
||
539 | |||
540 | /** |
||
541 | * Returns string that is unique for every different configuration of prefilters. |
||
542 | * Result of prefilters may be cached until this string changes. |
||
543 | * |
||
544 | * You can override this function. |
||
545 | * |
||
546 | * @return string |
||
547 | */ |
||
548 | private function getPreFiltersCacheId() |
||
562 | |||
563 | /** |
||
564 | * Instantiate prefilters |
||
565 | * |
||
566 | * @return array of PHPTAL_[Pre]Filter objects |
||
567 | */ |
||
568 | private function getPreFilterInstances() |
||
579 | |||
580 | /** |
||
581 | * Set template post filter. |
||
582 | * It will be called every time after template generates output. |
||
583 | * |
||
584 | * See PHPTAL_PostFilter class. |
||
585 | * |
||
586 | * @param PHPTAL_Filter $filter filter instance |
||
587 | */ |
||
588 | public function setPostFilter(PHPTAL_Filter $filter) |
||
593 | |||
594 | /** |
||
595 | * Register a trigger for specified phptal:id. |
||
596 | * @param string $id phptal:id to look for |
||
597 | */ |
||
598 | public function addTrigger($id, PHPTAL_Trigger $trigger) |
||
603 | |||
604 | /** |
||
605 | * Returns trigger for specified phptal:id. |
||
606 | * |
||
607 | * @param string $id phptal:id |
||
608 | * |
||
609 | * @return PHPTAL_Trigger or NULL |
||
610 | */ |
||
611 | public function getTrigger($id) |
||
618 | |||
619 | /** |
||
620 | * Set a context variable. |
||
621 | * Use it by setting properties on PHPTAL object. |
||
622 | * |
||
623 | * @param string $varname |
||
624 | * @param mixed $value |
||
625 | * |
||
626 | * @return void |
||
627 | */ |
||
628 | public function __set($varname, $value) |
||
632 | |||
633 | /** |
||
634 | * Set a context variable. |
||
635 | * |
||
636 | * @see PHPTAL::__set() |
||
637 | * @param string $varname name of the variable |
||
638 | * @param mixed $value value of the variable |
||
639 | * |
||
640 | * @return $this |
||
641 | */ |
||
642 | public function set($varname, $value) |
||
647 | |||
648 | /** |
||
649 | * Execute the template code and return generated markup. |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | public function execute() |
||
697 | |||
698 | /** |
||
699 | * Execute and echo template without buffering of the output. |
||
700 | * This function does not allow postfilters nor DOCTYPE/XML declaration. |
||
701 | * |
||
702 | * @return NULL |
||
703 | */ |
||
704 | public function echoExecute() |
||
726 | |||
727 | /** |
||
728 | * Execute a template macro. |
||
729 | * Should be used only from within generated template code! |
||
730 | * |
||
731 | * @param string $path Template macro path |
||
732 | */ |
||
733 | public function executeMacro($path) |
||
737 | |||
738 | /** |
||
739 | * This is PHPTAL's internal function that handles |
||
740 | * execution of macros from templates. |
||
741 | * |
||
742 | * $this is caller's context (the file where execution had originally started) |
||
743 | * |
||
744 | * @param PHPTAL $local_tpl is PHPTAL instance of the file in which macro is defined |
||
745 | * (it will be different from $this if it's external macro call) |
||
746 | * @access private |
||
747 | */ |
||
748 | final public function _executeMacroOfTemplate($path, PHPTAL $local_tpl) |
||
787 | |||
788 | /** |
||
789 | * ensure that getCodePath will return up-to-date path |
||
790 | */ |
||
791 | private function setCodeFile() |
||
796 | |||
797 | protected function resetPrepared() |
||
803 | |||
804 | /** |
||
805 | * Prepare template without executing it. |
||
806 | */ |
||
807 | public function prepare() |
||
872 | |||
873 | /** |
||
874 | * get how long compiled templates and phptal:cache files are kept, in days |
||
875 | */ |
||
876 | public function getCacheLifetime() |
||
880 | |||
881 | /** |
||
882 | * set how long compiled templates and phptal:cache files are kept |
||
883 | * |
||
884 | * @param $days number of days |
||
885 | */ |
||
886 | public function setCacheLifetime($days) |
||
891 | |||
892 | /** |
||
893 | * PHPTAL will scan cache and remove old files on every nth compile |
||
894 | * Set to 0 to disable cleanups |
||
895 | */ |
||
896 | public function setCachePurgeFrequency($n) |
||
901 | |||
902 | /** |
||
903 | * how likely cache cleaning can happen |
||
904 | * @see self::setCachePurgeFrequency() |
||
905 | */ |
||
906 | public function getCachePurgeFrequency() |
||
910 | |||
911 | |||
912 | /** |
||
913 | * Removes all compiled templates from cache that |
||
914 | * are older than getCacheLifetime() days |
||
915 | */ |
||
916 | public function cleanUpGarbage() |
||
940 | |||
941 | /** |
||
942 | * Removes content cached with phptal:cache for currently set template |
||
943 | * Must be called after setSource/setTemplate. |
||
944 | */ |
||
945 | public function cleanUpCache() |
||
957 | |||
958 | /** |
||
959 | * Returns the path of the intermediate PHP code file. |
||
960 | * |
||
961 | * The returned file may be used to cleanup (unlink) temporary files |
||
962 | * generated by temporary templates or more simply for debug. |
||
963 | * |
||
964 | * @return string |
||
965 | */ |
||
966 | public function getCodePath() |
||
971 | |||
972 | /** |
||
973 | * Returns the generated template function name. |
||
974 | * @return string |
||
975 | */ |
||
976 | public function getFunctionName() |
||
1005 | |||
1006 | /** |
||
1007 | * Returns prefix used for function name. |
||
1008 | * Function name is also base name for the template. |
||
1009 | * |
||
1010 | * @param int $timestamp unix timestamp with template modification date |
||
1011 | * |
||
1012 | * @return string |
||
1013 | */ |
||
1014 | private function getFunctionNamePrefix($timestamp) |
||
1020 | |||
1021 | /** |
||
1022 | * Returns template translator. |
||
1023 | * @return PHPTAL_TranslationService |
||
1024 | */ |
||
1025 | public function getTranslator() |
||
1029 | |||
1030 | /** |
||
1031 | * Returns array of exceptions caught by tal:on-error attribute. |
||
1032 | * |
||
1033 | * @return array<Exception> |
||
1034 | */ |
||
1035 | public function getErrors() |
||
1039 | |||
1040 | /** |
||
1041 | * Public for phptal templates, private for user. |
||
1042 | * |
||
1043 | * @return void |
||
1044 | * @access private |
||
1045 | */ |
||
1046 | public function addError(Exception $error) |
||
1050 | |||
1051 | /** |
||
1052 | * Returns current context object. |
||
1053 | * Use only in Triggers. |
||
1054 | * |
||
1055 | * @return PHPTAL_Context |
||
1056 | */ |
||
1057 | public function getContext() |
||
1061 | |||
1062 | /** |
||
1063 | * only for use in generated template code |
||
1064 | * |
||
1065 | * @access private |
||
1066 | */ |
||
1067 | public function getGlobalContext() |
||
1071 | |||
1072 | /** |
||
1073 | * only for use in generated template code |
||
1074 | * |
||
1075 | * @access private |
||
1076 | */ |
||
1077 | final public function pushContext() |
||
1082 | |||
1083 | /** |
||
1084 | * only for use in generated template code |
||
1085 | * |
||
1086 | * @access private |
||
1087 | */ |
||
1088 | final public function popContext() |
||
1093 | |||
1094 | /** |
||
1095 | * Parse currently set template, prefilter and generate PHP code. |
||
1096 | * |
||
1097 | * @return string (compiled PHP code) |
||
1098 | */ |
||
1099 | protected function parse() |
||
1129 | |||
1130 | /** |
||
1131 | * Search template source location. |
||
1132 | * @return void |
||
1133 | */ |
||
1134 | protected function findTemplate() |
||
1164 | |||
1165 | /** |
||
1166 | * Removed |
||
1167 | * |
||
1168 | * @deprecated |
||
1169 | * @return void |
||
1170 | */ |
||
1171 | final public static function setIncludePath() |
||
1174 | |||
1175 | /** |
||
1176 | * Restore include path to state before PHPTAL modified it. |
||
1177 | * |
||
1178 | * @deprecated |
||
1179 | * @return void |
||
1180 | */ |
||
1181 | final public static function restoreIncludePath() |
||
1184 | |||
1185 | /** |
||
1186 | * Suitable for callbacks from SPL autoload |
||
1187 | * |
||
1188 | * @param string $class class name to load |
||
1189 | * |
||
1190 | * @return void |
||
1191 | */ |
||
1192 | final public static function autoload($class) |
||
1205 | |||
1206 | /** |
||
1207 | * Sets up PHPTAL's autoloader. |
||
1208 | * |
||
1209 | * If you have to use your own autoloader to load PHPTAL files, |
||
1210 | * use spl_autoload_unregister(array('PHPTAL','autoload')); |
||
1211 | * |
||
1212 | * @return void |
||
1213 | */ |
||
1214 | final public static function autoloadRegister() |
||
1235 | } |
||
1236 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.