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 |
||
| 14 | class Cache |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var \Memcache |
||
| 18 | */ |
||
| 19 | protected $memcache = null; |
||
| 20 | |||
| 21 | const JSON = 1; |
||
| 22 | const TEXT = 2; |
||
| 23 | const GZIP = 3; |
||
| 24 | const JSONGZ = 4; |
||
| 25 | const MEMCACHE = 5; |
||
| 26 | |||
| 27 | use SingletonTrait; |
||
| 28 | |||
| 29 | 1 | public function init() { |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | 5 | public static function canUseMemcache() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Método que guarda un text en un fichero |
||
| 46 | * @param string $data |
||
| 47 | * @param string $path |
||
| 48 | * @throws ConfigException |
||
| 49 | */ |
||
| 50 | 4 | View Code Duplication | private function saveTextToFile($data, $path) |
| 58 | |||
| 59 | /** |
||
| 60 | * Método que extrae el texto de un fichero |
||
| 61 | * @param string $path |
||
| 62 | * @param int $transform |
||
| 63 | * @param boolean $absolute |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | 5 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Método que verifica si un fichero tiene la cache expirada |
||
| 80 | * @param string $path |
||
| 81 | * @param int $expires |
||
| 82 | * @param boolean $absolute |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Método que transforma los datos de salida |
||
| 94 | * @param string $data |
||
| 95 | * @param int $transform |
||
| 96 | * @return array|string|null |
||
| 97 | */ |
||
| 98 | 5 | public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Método que transforma los datos de entrada del fichero |
||
| 121 | * @param string $data |
||
| 122 | * @param int $transform |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 4 | public static function transformData($data, $transform = Cache::TEXT) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Método que guarda en fichero los datos pasados |
||
| 149 | * @param $path |
||
| 150 | * @param $data |
||
| 151 | * @param int $transform |
||
| 152 | * @param boolean $absolute |
||
| 153 | * @param integer $expires |
||
| 154 | */ |
||
| 155 | 4 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Método que verifica si tiene que leer o no un fichero de cache |
||
| 168 | * @param string $path |
||
| 169 | * @param int $expires |
||
| 170 | * @param callable $function |
||
| 171 | * @param int $transform |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | 1 | public function readFromCache($path, $expires = 300, callable $function, $transform = Cache::TEXT) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | 1 | private static function checkAdminSite() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
||
| 204 | * @return integer|boolean |
||
| 205 | */ |
||
| 206 | 1 | public static function needCache() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Método que construye un hash para almacenar la cache |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 1 | public function getRequestCacheHash() |
|
| 231 | } |
||
| 232 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..