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 |
||
| 11 | class Cache |
||
|
1 ignored issue
–
show
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Connection to a set of memcache servers |
||
| 15 | * |
||
| 16 | * @var Memcache |
||
| 17 | */ |
||
| 18 | public static $server = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Truing to connect flag |
||
| 22 | * |
||
| 23 | * @var boolean |
||
| 24 | */ |
||
| 25 | public static $connectTrying = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Connected flag |
||
| 29 | * |
||
| 30 | * @var boolean |
||
| 31 | */ |
||
| 32 | public static $connected = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Try connect to memcache server |
||
| 36 | */ |
||
| 37 | public static function connect() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get chached value |
||
| 48 | * |
||
| 49 | * If value not present, call callback |
||
| 50 | * |
||
| 51 | * @param string $name |
||
| 52 | * @param array $params |
||
| 53 | * @param callable $callback |
||
| 54 | * @return boolean |
||
| 55 | */ |
||
| 56 | public static function get($name, $params = [], $callback = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set value to cache |
||
| 82 | * |
||
| 83 | * @param string $name |
||
| 84 | * @param array $params |
||
| 85 | * @param mixed $val |
||
| 86 | * @param int $lifeTime |
||
| 87 | * @return boolean |
||
| 88 | */ |
||
| 89 | public static function set($name, $params = [], $val = '', $lifeTime = 3600) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Move file to cache folder and return path |
||
| 102 | * |
||
| 103 | * Also resize image when given resize params |
||
| 104 | * |
||
| 105 | * @param string $file |
||
| 106 | * @param array $options |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public static function file($file, $options = []) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get cache dir for app |
||
| 133 | * |
||
| 134 | * @param App $app |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public static function getDir($app = null) |
||
| 146 | |||
| 147 | } |
||
| 148 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.