Complex classes like Smarty_Internal_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 Smarty_Internal_Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Smarty_Internal_Template extends Smarty_Internal_TemplateBase |
||
28 | { |
||
29 | /** |
||
30 | * Template object cache |
||
31 | * |
||
32 | * @var Smarty_Internal_Template[] |
||
33 | */ |
||
34 | public static $tplObjCache = array(); |
||
35 | |||
36 | /** |
||
37 | * Template object cache for Smarty::isCached() === true |
||
38 | * |
||
39 | * @var Smarty_Internal_Template[] |
||
40 | */ |
||
41 | public static $isCacheTplObj = array(); |
||
42 | |||
43 | /** |
||
44 | * Sub template Info Cache |
||
45 | * - index name |
||
46 | * - value use count |
||
47 | * |
||
48 | * @var int[] |
||
49 | */ |
||
50 | public static $subTplInfo = array(); |
||
51 | |||
52 | /** |
||
53 | * This object type (Smarty = 1, template = 2, data = 4) |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | public $_objType = 2; |
||
58 | |||
59 | /** |
||
60 | * Global smarty instance |
||
61 | * |
||
62 | * @var Smarty |
||
63 | */ |
||
64 | public $smarty = null; |
||
65 | |||
66 | /** |
||
67 | * Source instance |
||
68 | * |
||
69 | * @var Smarty_Template_Source|Smarty_Template_Config |
||
70 | */ |
||
71 | public $source = null; |
||
72 | |||
73 | /** |
||
74 | * Inheritance runtime extension |
||
75 | * |
||
76 | * @var Smarty_Internal_Runtime_Inheritance |
||
77 | */ |
||
78 | public $inheritance = null; |
||
79 | |||
80 | /** |
||
81 | * Template resource |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | public $template_resource = null; |
||
86 | |||
87 | /** |
||
88 | * flag if compiled template is invalid and must be (re)compiled |
||
89 | * |
||
90 | * @var bool |
||
91 | */ |
||
92 | public $mustCompile = null; |
||
93 | |||
94 | /** |
||
95 | * Template Id |
||
96 | * |
||
97 | * @var null|string |
||
98 | */ |
||
99 | public $templateId = null; |
||
100 | |||
101 | /** |
||
102 | * Scope in which variables shall be assigned |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | public $scope = 0; |
||
107 | |||
108 | /** |
||
109 | * Flag which is set while rending a cache file |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | public $isRenderingCache = false; |
||
114 | |||
115 | /** |
||
116 | * Callbacks called before rendering template |
||
117 | * |
||
118 | * @var callback[] |
||
119 | */ |
||
120 | public $startRenderCallbacks = array(); |
||
121 | |||
122 | /** |
||
123 | * Callbacks called after rendering template |
||
124 | * |
||
125 | * @var callback[] |
||
126 | */ |
||
127 | public $endRenderCallbacks = array(); |
||
128 | |||
129 | /** |
||
130 | * Create template data object |
||
131 | * Some of the global Smarty settings copied to template scope |
||
132 | * It load the required template resources and caching plugins |
||
133 | * |
||
134 | * @param string $template_resource template resource string |
||
135 | * @param Smarty $smarty Smarty instance |
||
136 | * @param null|\Smarty_Internal_Template|\Smarty|\Smarty_Internal_Data $_parent back pointer to parent |
||
137 | * object with variables or |
||
138 | * null |
||
139 | * @param mixed $_cache_id cache id or null |
||
140 | * @param mixed $_compile_id compile id or null |
||
141 | * @param bool|int|null $_caching use caching? |
||
142 | * @param int|null $_cache_lifetime cache life-time in |
||
143 | * seconds |
||
144 | * @param bool $_isConfig |
||
145 | * |
||
146 | * @throws \SmartyException |
||
147 | */ |
||
148 | public function __construct( |
||
174 | |||
175 | /** |
||
176 | * render template |
||
177 | * |
||
178 | * @param bool $no_output_filter if true do not run output filter |
||
179 | * @param null|bool $display true: display, false: fetch null: sub-template |
||
180 | * |
||
181 | * @return string |
||
182 | * @throws \Exception |
||
183 | * @throws \SmartyException |
||
184 | */ |
||
185 | public function render($no_output_filter = true, $display = null) |
||
260 | |||
261 | /** |
||
262 | * Runtime function to render sub-template |
||
263 | * |
||
264 | * @param string $template template name |
||
265 | * @param mixed $cache_id cache id |
||
266 | * @param mixed $compile_id compile id |
||
267 | * @param integer $caching cache mode |
||
268 | * @param integer $cache_lifetime life time of cache data |
||
269 | * @param array $data passed parameter template variables |
||
270 | * @param int $scope scope in which {include} should execute |
||
271 | * @param bool $forceTplCache cache template object |
||
272 | * @param string $uid file dependency uid |
||
273 | * @param string $content_func function name |
||
274 | * |
||
275 | * @throws \Exception |
||
276 | * @throws \SmartyException |
||
277 | */ |
||
278 | public function _subTemplateRender( |
||
389 | |||
390 | /** |
||
391 | * Get called sub-templates and save call count |
||
392 | */ |
||
393 | public function _subTemplateRegister() |
||
403 | |||
404 | /** |
||
405 | * Check if this is a sub template |
||
406 | * |
||
407 | * @return bool true is sub template |
||
408 | */ |
||
409 | public function _isSubTpl() |
||
413 | |||
414 | /** |
||
415 | * Assign variable in scope |
||
416 | * |
||
417 | * @param string $varName variable name |
||
418 | * @param mixed $value value |
||
419 | * @param bool $nocache nocache flag |
||
420 | * @param int $scope scope into which variable shall be assigned |
||
421 | */ |
||
422 | public function _assignInScope($varName, $value, $nocache = false, $scope = 0) |
||
439 | |||
440 | /** |
||
441 | * Check if plugins are callable require file otherwise |
||
442 | * |
||
443 | * @param array $plugins required plugins |
||
444 | * |
||
445 | * @throws \SmartyException |
||
446 | */ |
||
447 | public function _checkPlugins($plugins) |
||
473 | |||
474 | /** |
||
475 | * This function is executed automatically when a compiled or cached template file is included |
||
476 | * - Decode saved properties from compiled template and cache files |
||
477 | * - Check if compiled or cache file is valid |
||
478 | * |
||
479 | * @param \Smarty_Internal_Template $tpl |
||
480 | * @param array $properties special template properties |
||
481 | * @param bool $cache flag if called from cache file |
||
482 | * |
||
483 | * @return bool flag if compiled or cache file is valid |
||
484 | * @throws \SmartyException |
||
485 | */ |
||
486 | public function _decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false) |
||
550 | |||
551 | /** |
||
552 | * Compiles the template |
||
553 | * If the template is not evaluated the compiled template is saved on disk |
||
554 | * |
||
555 | * @throws \Exception |
||
556 | */ |
||
557 | public function compileTemplateSource() |
||
561 | |||
562 | /** |
||
563 | * Writes the content to cache resource |
||
564 | * |
||
565 | * @param string $content |
||
566 | * |
||
567 | * @return bool |
||
568 | */ |
||
569 | public function writeCachedContent($content) |
||
573 | |||
574 | /** |
||
575 | * Get unique template id |
||
576 | * |
||
577 | * @return string |
||
578 | * @throws \SmartyException |
||
579 | */ |
||
580 | public function _getTemplateId() |
||
585 | |||
586 | /** |
||
587 | * runtime error not matching capture tags |
||
588 | * |
||
589 | * @throws \SmartyException |
||
590 | */ |
||
591 | public function capture_error() |
||
595 | |||
596 | /** |
||
597 | * Load compiled object |
||
598 | * |
||
599 | * @param bool $force force new compiled object |
||
600 | */ |
||
601 | public function loadCompiled($force = false) |
||
607 | |||
608 | /** |
||
609 | * Load cached object |
||
610 | * |
||
611 | * @param bool $force force new cached object |
||
612 | */ |
||
613 | public function loadCached($force = false) |
||
619 | |||
620 | /** |
||
621 | * Load inheritance object |
||
622 | */ |
||
623 | public function _loadInheritance() |
||
624 | { |
||
625 | if (!isset($this->inheritance)) { |
||
626 | $this->inheritance = new Smarty_Internal_Runtime_Inheritance(); |
||
627 | } |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Unload inheritance object |
||
632 | */ |
||
633 | public function _cleanUp() |
||
634 | { |
||
635 | $this->startRenderCallbacks = array(); |
||
636 | $this->endRenderCallbacks = array(); |
||
637 | $this->inheritance = null; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Load compiler object |
||
642 | * |
||
643 | * @throws \SmartyException |
||
644 | */ |
||
645 | public function loadCompiler() |
||
657 | |||
658 | /** |
||
659 | * Handle unknown class methods |
||
660 | * |
||
661 | * @param string $name unknown method-name |
||
662 | * @param array $args argument array |
||
663 | * |
||
664 | * @return mixed |
||
665 | */ |
||
666 | public function __call($name, $args) |
||
675 | |||
676 | /** |
||
677 | * get Smarty property in template context |
||
678 | * |
||
679 | * @param string $property_name property name |
||
680 | * |
||
681 | * @return mixed|Smarty_Template_Cached |
||
682 | * @throws SmartyException |
||
683 | */ |
||
684 | public function __get($property_name) |
||
704 | |||
705 | /** |
||
706 | * set Smarty property in template context |
||
707 | * |
||
708 | * @param string $property_name property name |
||
709 | * @param mixed $value value |
||
710 | * |
||
711 | * @throws SmartyException |
||
712 | */ |
||
713 | public function __set($property_name, $value) |
||
730 | |||
731 | /** |
||
732 | * Template data object destructor |
||
733 | */ |
||
734 | public function __destruct() |
||
740 | } |
||
741 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.