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 | 2 | public function __construct() |
|
34 | |||
35 | /** |
||
36 | * @return bool |
||
37 | */ |
||
38 | 2 | public static function canUseMemcache() |
|
42 | |||
43 | /** |
||
44 | * Método que guarda un text en un fichero |
||
45 | * @param string $data |
||
46 | * @param string $path |
||
47 | * @throws ConfigException |
||
48 | */ |
||
49 | 4 | View Code Duplication | private function saveTextToFile($data, $path) |
56 | |||
57 | /** |
||
58 | * Método que extrae el texto de un fichero |
||
59 | * @param string $path |
||
60 | * @param int $transform |
||
61 | * @param boolean $absolute |
||
62 | * @return mixed |
||
|
|||
63 | */ |
||
64 | 6 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
74 | |||
75 | /** |
||
76 | * Método que verifica si un fichero tiene la cache expirada |
||
77 | * @param string $path |
||
78 | * @param int $expires |
||
79 | * @param boolean $absolute |
||
80 | * @return bool |
||
81 | */ |
||
82 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
89 | |||
90 | /** |
||
91 | * Método que transforma los datos de salida |
||
92 | * @param string $data |
||
93 | * @param int $transform |
||
94 | * @return array|string|null |
||
95 | */ |
||
96 | 6 | View Code Duplication | public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
115 | |||
116 | /** |
||
117 | * Método que transforma los datos de entrada del fichero |
||
118 | * @param string $data |
||
119 | * @param int $transform |
||
120 | * @return string |
||
121 | */ |
||
122 | 4 | View Code Duplication | public static function transformData($data, $transform = Cache::TEXT) |
141 | |||
142 | /** |
||
143 | * Método que guarda en fichero los datos pasados |
||
144 | * @param $path |
||
145 | * @param $data |
||
146 | * @param int $transform |
||
147 | * @param boolean $absolute |
||
148 | * @param integer $expires |
||
149 | */ |
||
150 | 4 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
157 | |||
158 | /** |
||
159 | * Método que verifica si tiene que leer o no un fichero de cache |
||
160 | * @param string $path |
||
161 | * @param int $expires |
||
162 | * @param callable $function |
||
163 | * @param int $transform |
||
164 | * @return mixed |
||
165 | */ |
||
166 | 1 | public function readFromCache($path, $expires = 300, $function = null, $transform = Cache::TEXT) |
|
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | private static function checkAdminSite() |
||
188 | |||
189 | /** |
||
190 | * Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
||
191 | * @return integer|boolean |
||
192 | */ |
||
193 | 1 | public static function needCache() |
|
207 | |||
208 | /** |
||
209 | * Método que construye un hash para almacenar la cache |
||
210 | * @return array |
||
211 | */ |
||
212 | 1 | 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.