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 references_Cache_Lite 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 references_Cache_Lite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class references_Cache_Lite |
||
|
|||
29 | { |
||
30 | |||
31 | // --- Private properties --- |
||
32 | |||
33 | /** |
||
34 | * Directory where to put the cache files |
||
35 | * (make sure to add a trailing slash) |
||
36 | * |
||
37 | * @var string $_cacheDir |
||
38 | */ |
||
39 | public $_cacheDir = '/tmp/'; |
||
40 | |||
41 | /** |
||
42 | * Enable / disable caching |
||
43 | * |
||
44 | * (can be very usefull for the debug of cached scripts) |
||
45 | * |
||
46 | * @var boolean $_caching |
||
47 | */ |
||
48 | public $_caching = true; |
||
49 | |||
50 | /** |
||
51 | * Cache lifetime (in seconds) |
||
52 | * |
||
53 | * If null, the cache is valid forever. |
||
54 | * |
||
55 | * @var int $_lifeTime |
||
56 | */ |
||
57 | public $_lifeTime = 3600; |
||
58 | |||
59 | /** |
||
60 | * Enable / disable fileLocking |
||
61 | * |
||
62 | * (can avoid cache corruption under bad circumstances) |
||
63 | * |
||
64 | * @var boolean $_fileLocking |
||
65 | */ |
||
66 | public $_fileLocking = true; |
||
67 | |||
68 | /** |
||
69 | * Timestamp of the last valid cache |
||
70 | * |
||
71 | * @var int $_refreshTime |
||
72 | */ |
||
73 | public $_refreshTime; |
||
74 | |||
75 | /** |
||
76 | * File name (with path) |
||
77 | * |
||
78 | * @var string $_file |
||
79 | */ |
||
80 | public $_file; |
||
81 | |||
82 | /** |
||
83 | * File name (without path) |
||
84 | * |
||
85 | * @var string $_fileName |
||
86 | */ |
||
87 | public $_fileName; |
||
88 | |||
89 | /** |
||
90 | * Enable / disable write control (the cache is read just after writing to detect corrupt entries) |
||
91 | * |
||
92 | * Enable write control will lightly slow the cache writing but not the cache reading |
||
93 | * Write control can detect some corrupt cache files but maybe it's not a perfect control |
||
94 | * |
||
95 | * @var boolean $_writeControl |
||
96 | */ |
||
97 | public $_writeControl = true; |
||
98 | |||
99 | /** |
||
100 | * Enable / disable read control |
||
101 | * |
||
102 | * If enabled, a control key is embeded in cache file and this key is compared with the one |
||
103 | * calculated after the reading. |
||
104 | * |
||
105 | * @var boolean $_writeControl |
||
106 | */ |
||
107 | public $_readControl = true; |
||
108 | |||
109 | /** |
||
110 | * Type of read control (only if read control is enabled) |
||
111 | * |
||
112 | * Available values are : |
||
113 | * 'md5' for a md5 hash control (best but slowest) |
||
114 | * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) |
||
115 | * 'strlen' for a length only test (fastest) |
||
116 | * |
||
117 | * @var boolean $_readControlType |
||
118 | */ |
||
119 | public $_readControlType = 'crc32'; |
||
120 | |||
121 | /** |
||
122 | * Pear error mode (when raiseError is called) |
||
123 | * |
||
124 | * (see PEAR doc) |
||
125 | * |
||
126 | * @see setToDebug() |
||
127 | * @var int $_pearErrorMode |
||
128 | */ |
||
129 | public $_pearErrorMode = REFERENCES_CACHE_LITE_ERROR_RETURN; |
||
130 | |||
131 | /** |
||
132 | * Current cache id |
||
133 | * |
||
134 | * @var string $_id |
||
135 | */ |
||
136 | public $_id; |
||
137 | |||
138 | /** |
||
139 | * Current cache group |
||
140 | * |
||
141 | * @var string $_group |
||
142 | */ |
||
143 | public $_group; |
||
144 | |||
145 | /** |
||
146 | * Enable / Disable "Memory Caching" |
||
147 | * |
||
148 | * NB : There is no lifetime for memory caching ! |
||
149 | * |
||
150 | * @var boolean $_memoryCaching |
||
151 | */ |
||
152 | public $_memoryCaching = false; |
||
153 | |||
154 | /** |
||
155 | * Enable / Disable "Only Memory Caching" |
||
156 | * (be carefull, memory caching is "beta quality") |
||
157 | * |
||
158 | * @var boolean $_onlyMemoryCaching |
||
159 | */ |
||
160 | public $_onlyMemoryCaching = false; |
||
161 | |||
162 | /** |
||
163 | * Memory caching array |
||
164 | * |
||
165 | * @var array $_memoryCachingArray |
||
166 | */ |
||
167 | public $_memoryCachingArray = array(); |
||
168 | |||
169 | /** |
||
170 | * Memory caching counter |
||
171 | * |
||
172 | * @var int $memoryCachingCounter |
||
173 | */ |
||
174 | public $_memoryCachingCounter = 0; |
||
175 | |||
176 | /** |
||
177 | * Memory caching limit |
||
178 | * |
||
179 | * @var int $memoryCachingLimit |
||
180 | */ |
||
181 | public $_memoryCachingLimit = 1000; |
||
182 | |||
183 | /** |
||
184 | * File Name protection |
||
185 | * |
||
186 | * if set to true, you can use any cache id or group name |
||
187 | * if set to false, it can be faster but cache ids and group names |
||
188 | * will be used directly in cache file names so be carefull with |
||
189 | * special characters... |
||
190 | * |
||
191 | * @var boolean $fileNameProtection |
||
192 | */ |
||
193 | public $_fileNameProtection = true; |
||
194 | |||
195 | /** |
||
196 | * Enable / disable automatic serialization |
||
197 | * |
||
198 | * it can be used to save directly datas which aren't strings |
||
199 | * (but it's slower) |
||
200 | * |
||
201 | * @var boolean $_serialize |
||
202 | */ |
||
203 | public $_automaticSerialization = false; |
||
204 | |||
205 | /** |
||
206 | * Disable / Tune the automatic cleaning process |
||
207 | * |
||
208 | * The automatic cleaning process destroy too old (for the given life time) |
||
209 | * cache files when a new cache file is written. |
||
210 | * 0 => no automatic cache cleaning |
||
211 | * 1 => systematic cache cleaning |
||
212 | * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write |
||
213 | * |
||
214 | * @var int $_automaticCleaning |
||
215 | */ |
||
216 | public $_automaticCleaningFactor = 0; |
||
217 | |||
218 | /** |
||
219 | * Nested directory level |
||
220 | * |
||
221 | * Set the hashed directory structure level. 0 means "no hashed directory |
||
222 | * structure", 1 means "one level of directory", 2 means "two levels"... |
||
223 | * This option can speed up Cache_Lite only when you have many thousands of |
||
224 | * cache file. Only specific benchs can help you to choose the perfect value |
||
225 | * for you. Maybe, 1 or 2 is a good start. |
||
226 | * |
||
227 | * @var int $_hashedDirectoryLevel |
||
228 | */ |
||
229 | public $_hashedDirectoryLevel = 0; |
||
230 | |||
231 | /** |
||
232 | * Umask for hashed directory structure |
||
233 | * |
||
234 | * @var int $_hashedDirectoryUmask |
||
235 | */ |
||
236 | public $_hashedDirectoryUmask = 0700; |
||
237 | |||
238 | /** |
||
239 | * API break for error handling in REFERENCES_CACHE_LITE_ERROR_RETURN mode |
||
240 | * |
||
241 | * In REFERENCES_CACHE_LITE_ERROR_RETURN mode, error handling was not good because |
||
242 | * for example save() method always returned a boolean (a PEAR_Error object |
||
243 | * would be better in REFERENCES_CACHE_LITE_ERROR_RETURN mode). To correct this without |
||
244 | * breaking the API, this option (false by default) can change this handling. |
||
245 | * |
||
246 | * @var boolean |
||
247 | */ |
||
248 | public $_errorHandlingAPIBreak = false; |
||
249 | |||
250 | // --- Public methods --- |
||
251 | |||
252 | /** |
||
253 | * Constructor |
||
254 | * |
||
255 | * $options is an assoc. Available options are : |
||
256 | * $options = array( |
||
257 | * 'cacheDir' => directory where to put the cache files (string) , |
||
258 | * 'caching' => enable / disable caching (boolean) , |
||
259 | * 'lifeTime' => cache lifetime in seconds (int) , |
||
260 | * 'fileLocking' => enable / disable fileLocking (boolean) , |
||
261 | * 'writeControl' => enable / disable write control (boolean) , |
||
262 | * 'readControl' => enable / disable read control (boolean) , |
||
263 | * 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string) , |
||
264 | * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int) , |
||
265 | * 'memoryCaching' => enable / disable memory caching (boolean) , |
||
266 | * 'onlyMemoryCaching' => enable / disable only memory caching (boolean) , |
||
267 | * 'memoryCachingLimit' => max nbr of records to store into memory caching (int) , |
||
268 | * 'fileNameProtection' => enable / disable automatic file name protection (boolean) , |
||
269 | * 'automaticSerialization' => enable / disable automatic serialization (boolean) , |
||
270 | * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int) , |
||
271 | * 'hashedDirectoryLevel' => level of the hashed directory system (int) , |
||
272 | * 'hashedDirectoryUmask' => umask for hashed directory structure (int) , |
||
273 | * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean) |
||
274 | * ); |
||
275 | * |
||
276 | * @param array $options options |
||
277 | * @access public |
||
278 | */ |
||
279 | public function __construct($options = array(null)) |
||
285 | |||
286 | /** |
||
287 | * Generic way to set a Cache_Lite option |
||
288 | * |
||
289 | * see Cache_Lite constructor for available options |
||
290 | * |
||
291 | * @var string $name name of the option |
||
292 | * @var mixed $value value of the option |
||
293 | * @access public |
||
294 | */ |
||
295 | public function setOption($name, $value) |
||
321 | |||
322 | /** |
||
323 | * Test if a cache is available and (if yes) return it |
||
324 | * |
||
325 | * @param string $id cache id |
||
326 | * @param string $group name of the cache group |
||
327 | * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
||
328 | * @return string data of the cache (else : false) |
||
329 | * @access public |
||
330 | */ |
||
331 | public function get($id, $group = 'default', $doNotTestCacheValidity = false) |
||
373 | |||
374 | /** |
||
375 | * Save some data in a cache file |
||
376 | * |
||
377 | * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on) |
||
378 | * @param string $id cache id |
||
379 | * @param string $group name of the cache group |
||
380 | * @return boolean true if no problem (else : false or a PEAR_Error object) |
||
381 | * @access public |
||
382 | */ |
||
383 | public function save($data, $id = null, $group = 'default') |
||
427 | |||
428 | /** |
||
429 | * Remove a cache file |
||
430 | * |
||
431 | * @param string $id cache id |
||
432 | * @param string $group name of the cache group |
||
433 | * @param boolean $checkbeforeunlink check if file exists before removing it |
||
434 | * @return boolean true if no problem |
||
435 | * @access public |
||
436 | */ |
||
437 | public function remove($id, $group = 'default', $checkbeforeunlink = false) |
||
457 | |||
458 | /** |
||
459 | * Clean the cache |
||
460 | * |
||
461 | * if no group is specified all cache files will be destroyed |
||
462 | * else only cache files of the specified group will be destroyed |
||
463 | * |
||
464 | * @param bool|string $group name of the cache group |
||
465 | * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
||
466 | * 'callback_myFunction' |
||
467 | * @return bool true if no problem |
||
468 | * @access public |
||
469 | */ |
||
470 | public function clean($group = false, $mode = 'ingroup') |
||
474 | |||
475 | /** |
||
476 | * Set to debug mode |
||
477 | * |
||
478 | * When an error is found, the script will stop and the message will be displayed |
||
479 | * (in debug mode only). |
||
480 | * |
||
481 | * @access public |
||
482 | */ |
||
483 | public function setToDebug() |
||
487 | |||
488 | /** |
||
489 | * Set a new life time |
||
490 | * |
||
491 | * @param int $newLifeTime new life time (in seconds) |
||
492 | * @access public |
||
493 | */ |
||
494 | public function setLifeTime($newLifeTime) |
||
499 | |||
500 | /** |
||
501 | * Save the state of the caching memory array into a cache file cache |
||
502 | * |
||
503 | * @param string $id cache id |
||
504 | * @param string $group name of the cache group |
||
505 | * @access public |
||
506 | */ |
||
507 | public function saveMemoryCachingState($id, $group = 'default') |
||
518 | |||
519 | /** |
||
520 | * Load the state of the caching memory array from a given cache file cache |
||
521 | * |
||
522 | * @param string $id cache id |
||
523 | * @param string $group name of the cache group |
||
524 | * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
||
525 | * @access public |
||
526 | */ |
||
527 | public function getMemoryCachingState($id, $group = 'default', $doNotTestCacheValidity = false) |
||
537 | |||
538 | /** |
||
539 | * Return the cache last modification time |
||
540 | * |
||
541 | * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
||
542 | * |
||
543 | * @return int last modification time |
||
544 | */ |
||
545 | public function lastModified() |
||
549 | |||
550 | /** |
||
551 | * Trigger a PEAR error |
||
552 | * |
||
553 | * To improve performances, the PEAR.php file is included dynamically. |
||
554 | * The file is so included only when an error is triggered. So, in most |
||
555 | * cases, the file isn't included and perfs are much better. |
||
556 | * |
||
557 | * @param string $msg error message |
||
558 | * @param int $code error code |
||
559 | * @access public |
||
560 | * @return object |
||
561 | */ |
||
562 | public function raiseError($msg, $code) |
||
568 | |||
569 | /** |
||
570 | * Extend the life of a valid cache file |
||
571 | * |
||
572 | * see http://pear.php.net/bugs/bug.php?id=6681 |
||
573 | * |
||
574 | * @access public |
||
575 | */ |
||
576 | public function extendLife() |
||
580 | |||
581 | // --- Private methods --- |
||
582 | |||
583 | /** |
||
584 | * Compute & set the refresh time |
||
585 | * |
||
586 | * @access private |
||
587 | */ |
||
588 | public function _setRefreshTime() |
||
596 | |||
597 | /** |
||
598 | * Remove a file |
||
599 | * |
||
600 | * @param string $file complete file path and name |
||
601 | * @return boolean true if no problem |
||
602 | * @access private |
||
603 | */ |
||
604 | public function _unlink($file) |
||
612 | |||
613 | /** |
||
614 | * Recursive function for cleaning cache file in the given directory |
||
615 | * |
||
616 | * @param string $dir directory complete path (with a trailing slash) |
||
617 | * @param bool|string $group name of the cache group |
||
618 | * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
||
619 | * 'callback_myFunction' |
||
620 | * @return bool true if no problem |
||
621 | * @access private |
||
622 | */ |
||
623 | public function _cleanDir($dir, $group = false, $mode = 'ingroup') |
||
687 | |||
688 | /** |
||
689 | * Add some date in the memory caching array |
||
690 | * |
||
691 | * @param string $data data to cache |
||
692 | * @access private |
||
693 | */ |
||
694 | public function _memoryCacheAdd($data) |
||
704 | |||
705 | /** |
||
706 | * Make a file name (with path) |
||
707 | * |
||
708 | * @param string $id cache id |
||
709 | * @param string $group name of the group |
||
710 | * @access private |
||
711 | */ |
||
712 | public function _setFileName($id, $group) |
||
729 | |||
730 | /** |
||
731 | * Read the cache file and return the content |
||
732 | * |
||
733 | * @return string content of the cache file (else : false or a PEAR_Error object) |
||
734 | * @access private |
||
735 | */ |
||
736 | public function _read() |
||
779 | |||
780 | /** |
||
781 | * Write the given data in the cache file |
||
782 | * |
||
783 | * @param string $data data to put in cache |
||
784 | * @return boolean true if ok (a PEAR_Error object else) |
||
785 | * @access private |
||
786 | */ |
||
787 | public function _write($data) |
||
821 | |||
822 | /** |
||
823 | * Write the given data in the cache file and control it just after to avoir corrupted cache entries |
||
824 | * |
||
825 | * @param string $data data to put in cache |
||
826 | * @return boolean true if the test is ok (else : false or a PEAR_Error object) |
||
827 | * @access private |
||
828 | */ |
||
829 | public function _writeAndControl($data) |
||
845 | |||
846 | /** |
||
847 | * Make a control key with the string containing datas |
||
848 | * |
||
849 | * @param string $data data |
||
850 | * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
||
851 | * @return string control key |
||
852 | * @access private |
||
853 | */ |
||
854 | public function _hash($data, $controlType) |
||
867 | } |
||
868 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.