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:
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 | /** |
||
30 | * @return bool |
||
31 | */ |
||
32 | 3 | public static function canUseMemcache() |
|
36 | |||
37 | /** |
||
38 | * Método que guarda un text en un fichero |
||
39 | * @param string $data |
||
40 | * @param string $path |
||
41 | * @throws ConfigException |
||
42 | */ |
||
43 | 5 | View Code Duplication | private function saveTextToFile($data, $path) |
50 | |||
51 | /** |
||
52 | * Método que extrae el texto de un fichero |
||
53 | * @param string $path |
||
54 | * @param int $transform |
||
55 | * @param boolean $absolute |
||
56 | * @return mixed |
||
57 | */ |
||
58 | 7 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
67 | |||
68 | /** |
||
69 | * Método que verifica si un fichero tiene la cache expirada |
||
70 | * @param string $path |
||
71 | * @param int $expires |
||
72 | * @param boolean $absolute |
||
73 | * @return bool |
||
74 | */ |
||
75 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
81 | |||
82 | /** |
||
83 | * Método que transforma los datos de salida |
||
84 | * @param string $data |
||
85 | * @param int $transform |
||
86 | * @return array|string|null |
||
87 | */ |
||
88 | 7 | public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
|
106 | |||
107 | /** |
||
108 | * Método que transforma los datos de entrada del fichero |
||
109 | * @param string $data |
||
110 | * @param int $transform |
||
111 | * @return string |
||
112 | */ |
||
113 | 5 | public static function transformData($data, $transform = Cache::TEXT) |
|
131 | |||
132 | /** |
||
133 | * Método que guarda en fichero los datos pasados |
||
134 | * @param $path |
||
135 | * @param $data |
||
136 | * @param int $transform |
||
137 | * @param boolean $absolute |
||
138 | * @param integer $expires |
||
139 | */ |
||
140 | 5 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
146 | |||
147 | /** |
||
148 | * Método que verifica si tiene que leer o no un fichero de cache |
||
149 | * @param string $path |
||
150 | * @param int $expires |
||
151 | * @param callable $function |
||
152 | * @param int $transform |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 1 | public function readFromCache($path, $expires = 300, callable $function, $transform = Cache::TEXT) |
|
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | 1 | private static function checkAdminSite() |
|
176 | |||
177 | /** |
||
178 | * Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
||
179 | * @return integer|boolean |
||
180 | */ |
||
181 | 1 | public static function needCache() |
|
192 | |||
193 | /** |
||
194 | * Método que construye un hash para almacenar la cache |
||
195 | * @return string |
||
196 | */ |
||
197 | 1 | public function getRequestCacheHash() |
|
206 | } |
||
207 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.