Total Complexity | 46 |
Total Lines | 239 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Smarty_Template_Cached 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.
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_Template_Cached, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Smarty_Template_Cached extends Smarty_Template_Resource_Base |
||
18 | { |
||
19 | /** |
||
20 | * Cache Is Valid |
||
21 | * |
||
22 | * @var boolean |
||
23 | */ |
||
24 | public $valid = null; |
||
25 | |||
26 | /** |
||
27 | * CacheResource Handler |
||
28 | * |
||
29 | * @var Smarty_CacheResource |
||
30 | */ |
||
31 | public $handler = null; |
||
32 | |||
33 | /** |
||
34 | * Template Cache Id (Smarty_Internal_Template::$cache_id) |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $cache_id = null; |
||
39 | |||
40 | /** |
||
41 | * saved cache lifetime in seconds |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | public $cache_lifetime = 0; |
||
46 | |||
47 | /** |
||
48 | * Id for cache locking |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $lock_id = null; |
||
53 | |||
54 | /** |
||
55 | * flag that cache is locked by this instance |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | public $is_locked = false; |
||
60 | |||
61 | /** |
||
62 | * Source Object |
||
63 | * |
||
64 | * @var Smarty_Template_Source |
||
65 | */ |
||
66 | public $source = null; |
||
67 | |||
68 | /** |
||
69 | * Nocache hash codes of processed compiled templates |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | public $hashes = array(); |
||
74 | |||
75 | /** |
||
76 | * Flag if this is a cache resource |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | public $isCache = true; |
||
81 | |||
82 | /** |
||
83 | * create Cached Object container |
||
84 | * |
||
85 | * @param Smarty_Internal_Template $_template template object |
||
86 | * |
||
87 | * @throws \SmartyException |
||
88 | */ |
||
89 | public function __construct(Smarty_Internal_Template $_template) |
||
90 | { |
||
91 | $this->compile_id = $_template->compile_id; |
||
92 | $this->cache_id = $_template->cache_id; |
||
93 | $this->source = $_template->source; |
||
94 | if (!class_exists('Smarty_CacheResource', false)) { |
||
95 | include SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php'; |
||
96 | } |
||
97 | $this->handler = Smarty_CacheResource::load($_template->smarty); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Smarty_Internal_Template $_template |
||
102 | * |
||
103 | * @return Smarty_Template_Cached |
||
104 | */ |
||
105 | public static function load(Smarty_Internal_Template $_template) |
||
106 | { |
||
107 | $_template->cached = new Smarty_Template_Cached($_template); |
||
108 | $_template->cached->handler->populate($_template->cached, $_template); |
||
109 | // caching enabled ? |
||
110 | if (!$_template->caching || $_template->source->handler->recompiled |
||
111 | ) { |
||
112 | $_template->cached->valid = false; |
||
113 | } |
||
114 | return $_template->cached; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Render cache template |
||
119 | * |
||
120 | * @param \Smarty_Internal_Template $_template |
||
121 | * @param bool $no_output_filter |
||
122 | * |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | public function render(Smarty_Internal_Template $_template, $no_output_filter = true) |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Check if cache is valid, lock cache if required |
||
149 | * |
||
150 | * @param \Smarty_Internal_Template $_template |
||
151 | * |
||
152 | * @return bool flag true if cache is valid |
||
153 | */ |
||
154 | public function isCached(Smarty_Internal_Template $_template) |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Process cached template |
||
227 | * |
||
228 | * @param Smarty_Internal_Template $_template template object |
||
229 | * @param bool $update flag if called because cache update |
||
230 | */ |
||
231 | public function process(Smarty_Internal_Template $_template, $update = false) |
||
232 | { |
||
233 | if ($this->handler->process($_template, $this, $update) === false) { |
||
234 | $this->valid = false; |
||
235 | } |
||
236 | if ($this->valid) { |
||
237 | $this->processed = true; |
||
238 | } else { |
||
239 | $this->processed = false; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Read cache content from handler |
||
245 | * |
||
246 | * @param Smarty_Internal_Template $_template template object |
||
247 | * |
||
248 | * @return string|false content |
||
249 | */ |
||
250 | public function read(Smarty_Internal_Template $_template) |
||
256 | } |
||
257 | } |
||
258 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.