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 | /** |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | 3 | public static function canUseMemcache() |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Método que guarda un text en un fichero |
||
| 40 | * @param string $data |
||
| 41 | * @param string $path |
||
| 42 | * @throws ConfigException |
||
| 43 | */ |
||
| 44 | 5 | View Code Duplication | private function saveTextToFile($data, $path) |
| 51 | |||
| 52 | /** |
||
| 53 | * Método que extrae el texto de un fichero |
||
| 54 | * @param string $path |
||
| 55 | * @param int $transform |
||
| 56 | * @param boolean $absolute |
||
| 57 | * @return mixed |
||
|
|
|||
| 58 | */ |
||
| 59 | 8 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Método que verifica si un fichero tiene la cache expirada |
||
| 71 | * @param string $path |
||
| 72 | * @param int $expires |
||
| 73 | * @param boolean $absolute |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Método que transforma los datos de salida |
||
| 85 | * @param string $data |
||
| 86 | * @param int $transform |
||
| 87 | * @return array|string|null |
||
| 88 | */ |
||
| 89 | 8 | public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Método que transforma los datos de entrada del fichero |
||
| 110 | * @param string $data |
||
| 111 | * @param int $transform |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 5 | public static function transformData($data, $transform = Cache::TEXT) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Método que guarda en fichero los datos pasados |
||
| 135 | * @param $path |
||
| 136 | * @param $data |
||
| 137 | * @param int $transform |
||
| 138 | * @param boolean $absolute |
||
| 139 | * @param integer $expires |
||
| 140 | */ |
||
| 141 | 5 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Método que verifica si tiene que leer o no un fichero de cache |
||
| 150 | * @param string $path |
||
| 151 | * @param int $expires |
||
| 152 | * @param callable $function |
||
| 153 | * @param int $transform |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | 1 | public function readFromCache($path, $expires = 300, callable $function, $transform = Cache::TEXT) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | 1 | private static function checkAdminSite() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
||
| 180 | * @return integer|boolean |
||
| 181 | */ |
||
| 182 | 1 | public static function needCache() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Método que construye un hash para almacenar la cache |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 1 | public function getRequestCacheHash() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Flush cache when save a registry |
||
| 213 | */ |
||
| 214 | public function flushCache() { |
||
| 220 | } |
||
| 221 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.