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 |
||
| 17 | class Cache |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var \Memcache |
||
| 21 | */ |
||
| 22 | protected $memcache = null; |
||
| 23 | |||
| 24 | const JSON = 1; |
||
| 25 | const TEXT = 2; |
||
| 26 | const GZIP = 3; |
||
| 27 | const JSONGZ = 4; |
||
| 28 | const MEMCACHE = 5; |
||
| 29 | |||
| 30 | const CACHE_SESSION_VAR = '__CACHE__'; |
||
| 31 | |||
| 32 | use SingletonTrait; |
||
| 33 | |||
| 34 | 3 | public function __construct() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | 3 | public static function canUseMemcache() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $data |
||
| 49 | * @param string $path |
||
| 50 | * @throws GeneratorException |
||
| 51 | * @throws ConfigException |
||
| 52 | */ |
||
| 53 | 7 | View Code Duplication | private function saveTextToFile($data, $path) |
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $path |
||
| 63 | * @param int $transform |
||
| 64 | * @param boolean $absolute |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | 8 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $path |
||
| 80 | * @param int $expires |
||
| 81 | * @param boolean $absolute |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $data |
||
|
|
|||
| 94 | * @param int $transform |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | 8 | View Code Duplication | public static function extractDataWithFormat($data = null, $transform = Cache::TEXT) |
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $data |
||
| 119 | * @param int $transform |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 7 | View Code Duplication | public static function transformData($data, $transform = Cache::TEXT) |
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $path |
||
| 144 | * @param mixed $data |
||
| 145 | * @param int $transform |
||
| 146 | * @param bool $absolute |
||
| 147 | * @throws GeneratorException |
||
| 148 | * @throws ConfigException |
||
| 149 | */ |
||
| 150 | 7 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $path |
||
| 160 | * @param int $expires |
||
| 161 | * @param null $function |
||
| 162 | * @param int $transform |
||
| 163 | * @return mixed|null |
||
| 164 | * @throws GeneratorException |
||
| 165 | * @throws ConfigException |
||
| 166 | */ |
||
| 167 | 1 | public function readFromCache($path, $expires = 300, $function = null, $transform = Cache::TEXT) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 1 | private static function checkAdminSite() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return integer|boolean |
||
| 192 | */ |
||
| 193 | 1 | public static function needCache() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 2 | public function getRequestCacheHash() |
|
| 225 | |||
| 226 | 1 | public function flushCache() { |
|
| 239 | } |
||
| 240 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.