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 |
||
| 10 | class Service implements CacheInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Cache. |
||
| 14 | * |
||
| 15 | * @var object |
||
| 16 | */ |
||
| 17 | protected $manager; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Config. |
||
| 21 | * |
||
| 22 | * @var Config |
||
| 23 | */ |
||
| 24 | protected $config; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Cache directory. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $dir; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Cache constructor. |
||
| 35 | * @param object $config |
||
| 36 | * @param object $manager |
||
| 37 | * @param null $path |
||
| 38 | */ |
||
| 39 | 34 | public function __construct($config = null, $manager = null, $path = null) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Instantiate the config. |
||
| 48 | * |
||
| 49 | * @param $config |
||
| 50 | * @return Config|mixed |
||
| 51 | */ |
||
| 52 | 34 | public function instantiateConfig($config) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Instantiate the cache manager. |
||
| 59 | * |
||
| 60 | * @param $config |
||
| 61 | * @param $manager |
||
| 62 | * @param $path |
||
| 63 | * @return NetteManager|mixed |
||
| 64 | */ |
||
| 65 | 34 | public function instantiateManager($config, $manager, $path) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Check if cache is enabled. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | 32 | protected function enabled() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Fetches a value from the cache. |
||
| 84 | * |
||
| 85 | * @param string $key |
||
| 86 | * @param null $default |
||
| 87 | * @return mixed|null |
||
| 88 | */ |
||
| 89 | 32 | public function get($key, $default = null) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Create a cache key. |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | * @throws Exception |
||
| 101 | */ |
||
| 102 | 32 | public function makeKey() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
| 115 | * |
||
| 116 | * @param string $key |
||
| 117 | * @param mixed $value |
||
| 118 | * @param null $ttl |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 32 | public function set($key, $value, $ttl = null) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Delete an item from the cache by its unique key. |
||
| 132 | * |
||
| 133 | * @param string $key |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function delete($key) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Wipe clean the entire cache's keys. |
||
| 143 | */ |
||
| 144 | 34 | public function clear() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Obtains multiple cache items by their unique keys. |
||
| 151 | * |
||
| 152 | * @param $keys |
||
| 153 | * @param null $default |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getMultiple($keys, $default = null) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
| 163 | * |
||
| 164 | * @param $values |
||
| 165 | * @param null $ttl |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function setMultiple($values, $ttl = null) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Deletes multiple cache items in a single operation. |
||
| 175 | * |
||
| 176 | * @param $keys |
||
| 177 | * @return bool|void |
||
| 178 | */ |
||
| 179 | public function deleteMultiple($keys) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Determines whether an item is present in the cache. |
||
| 186 | * |
||
| 187 | * @param string $key |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function has($key) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get an item from the cache, or store the default value. |
||
| 197 | * |
||
| 198 | * @param string $key |
||
| 199 | * @param \DateTimeInterface|\DateInterval|float|int $minutes |
||
| 200 | * @param Closure $callback |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function remember($key, $minutes, Closure $callback) |
|
| 213 | } |
||
| 214 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.