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 Cache 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 Cache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Cache |
||
16 | { |
||
17 | /** |
||
18 | * @var \Memcache |
||
19 | */ |
||
20 | protected $memcache = null; |
||
21 | |||
22 | const JSON = 1; |
||
23 | const TEXT = 2; |
||
24 | const GZIP = 3; |
||
25 | const JSONGZ = 4; |
||
26 | const MEMCACHE = 5; |
||
27 | |||
28 | use SingletonTrait; |
||
29 | |||
30 | public function __construct() |
||
34 | |||
35 | 2 | /** |
|
36 | * @return bool |
||
37 | */ |
||
38 | public static function canUseMemcache() |
||
42 | |||
43 | /** |
||
44 | 4 | * Método que guarda un text en un fichero |
|
45 | * @param string $data |
||
46 | 4 | * @param string $path |
|
47 | 4 | * @throws ConfigException |
|
48 | */ |
||
49 | View Code Duplication | private function saveTextToFile($data, $path) |
|
56 | |||
57 | /** |
||
58 | * Método que extrae el texto de un fichero |
||
59 | 6 | * @param string $path |
|
60 | * @param int $transform |
||
61 | 6 | * @param boolean $absolute |
|
62 | 6 | * @return mixed |
|
|
|||
63 | 6 | */ |
|
64 | 4 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
74 | |||
75 | /** |
||
76 | 1 | * Método que verifica si un fichero tiene la cache expirada |
|
77 | * @param string $path |
||
78 | 1 | * @param int $expires |
|
79 | 1 | * @param boolean $absolute |
|
80 | 1 | * @return bool |
|
81 | */ |
||
82 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
||
89 | 6 | ||
90 | /** |
||
91 | * Método que transforma los datos de salida |
||
92 | 6 | * @param string $data |
|
93 | 6 | * @param int $transform |
|
94 | 6 | * @return array|string|null |
|
95 | 4 | */ |
|
96 | 4 | View Code Duplication | public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
115 | |||
116 | /** |
||
117 | 4 | * Método que transforma los datos de entrada del fichero |
|
118 | 4 | * @param string $data |
|
119 | 4 | * @param int $transform |
|
120 | 3 | * @return string |
|
121 | 2 | */ |
|
122 | 2 | View Code Duplication | public static function transformData($data, $transform = Cache::TEXT) |
141 | 4 | ||
142 | /** |
||
143 | 4 | * Método que guarda en fichero los datos pasados |
|
144 | 4 | * @param $path |
|
145 | 4 | * @param $data |
|
146 | 4 | * @param int $transform |
|
147 | * @param boolean $absolute |
||
148 | * @param integer $expires |
||
149 | */ |
||
150 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
||
157 | |||
158 | 1 | /** |
|
159 | 1 | * Método que verifica si tiene que leer o no un fichero de cache |
|
160 | 1 | * @param string $path |
|
161 | 1 | * @param int $expires |
|
162 | 1 | * @param callable $function |
|
163 | * @param int $transform |
||
164 | 1 | * @return mixed |
|
165 | */ |
||
166 | public function readFromCache($path, $expires = 300, $function = null, $transform = Cache::TEXT) |
||
180 | |||
181 | /** |
||
182 | 1 | * @return bool |
|
183 | */ |
||
184 | 1 | private static function checkAdminSite() |
|
188 | 1 | ||
189 | /** |
||
190 | * Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
||
191 | 1 | * @return integer|boolean |
|
192 | */ |
||
193 | public static function needCache() |
||
207 | 1 | ||
208 | /** |
||
209 | 1 | * Método que construye un hash para almacenar la cache |
|
210 | * @return array |
||
211 | */ |
||
212 | public function getRequestCacheHash() |
||
227 | |||
228 | /** |
||
229 | * Flush cache when save a registry |
||
230 | */ |
||
231 | public function flushCache() { |
||
237 | } |
||
238 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.