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 |
||
| 28 | class CacheProvider |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Has the cache been attempted |
||
| 32 | * |
||
| 33 | * @var bool $attempted |
||
| 34 | */ |
||
| 35 | private $attempted = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * How long for the cache to be fresh |
||
| 39 | * |
||
| 40 | * @var int $expire |
||
| 41 | */ |
||
| 42 | private $expire; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Has the cache been found |
||
| 46 | * |
||
| 47 | * @var bool $hit |
||
| 48 | */ |
||
| 49 | private $hit = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The loaded Vars object from cache |
||
| 53 | * |
||
| 54 | * @var \M1\Vars\Vars $loaded_vars |
||
| 55 | */ |
||
| 56 | private $loaded_vars; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The cache file name |
||
| 60 | * |
||
| 61 | * @var string $name |
||
| 62 | */ |
||
| 63 | private $name; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The specific path for the cache folder |
||
| 67 | * |
||
| 68 | * @var string $path |
||
| 69 | */ |
||
| 70 | private $path; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Is the cache turned on |
||
| 74 | * |
||
| 75 | * @var boolean $provide |
||
| 76 | */ |
||
| 77 | private $provide; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * If cached, the time when the class was cached |
||
| 81 | * |
||
| 82 | * @var int $time |
||
| 83 | */ |
||
| 84 | private $time; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Creates a new instance of the cacheProvider for Vars |
||
| 88 | * |
||
| 89 | * @param string|array $resource The main configuration resource |
||
| 90 | * @param array $options The options being used for Vars |
||
| 91 | */ |
||
| 92 | public function __construct($resource, $options) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Checks the cache to see if there is a valid cache available |
||
| 103 | * |
||
| 104 | * @return bool Returns true if has the cached resource |
||
| 105 | */ |
||
| 106 | public function checkCache() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Load the cached file into $this->loaded_vars |
||
| 123 | */ |
||
| 124 | public function load() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Transfer the contents of the parent Vars object into a file for cache |
||
| 132 | * |
||
| 133 | * @param \M1\Vars\Vars $provide Does the cache want to be on or off |
||
|
|
|||
| 134 | */ |
||
| 135 | public function makeCache(Vars $vars) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns if cache is on or off |
||
| 145 | * |
||
| 146 | * @return bool Is cache on or off |
||
| 147 | */ |
||
| 148 | public function getProvide() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the cache on or off |
||
| 155 | * |
||
| 156 | * @param bool $provide Does the cache want to be on or off |
||
| 157 | * |
||
| 158 | * @return \M1\Vars\Cache\CacheProvider |
||
| 159 | */ |
||
| 160 | public function setProvide($provide) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns if the cache has been attempted |
||
| 168 | * |
||
| 169 | * @return bool Has the cache been attempted |
||
| 170 | */ |
||
| 171 | public function getAttempted() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the cache path |
||
| 178 | * |
||
| 179 | * @return string The cache path |
||
| 180 | */ |
||
| 181 | public function getPath() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets the cache path |
||
| 188 | * |
||
| 189 | * @param string $cache_path The cache path to set |
||
| 190 | * |
||
| 191 | * @throws \InvalidArgumentException If the cache path does not exist or is not writable |
||
| 192 | * |
||
| 193 | * @return \M1\Vars\Cache\CacheProvider |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function setPath($path) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Returns how long the cache lasts for |
||
| 214 | * |
||
| 215 | * @return int Cache expire time |
||
| 216 | */ |
||
| 217 | public function getExpire() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns how long the cache lasts for |
||
| 224 | * |
||
| 225 | * @return int Cache expire time |
||
| 226 | */ |
||
| 227 | public function isHit() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns when the cache was made |
||
| 234 | * |
||
| 235 | * @return int Cache creation time |
||
| 236 | */ |
||
| 237 | public function getTime() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Sets the time when the Vars was cached |
||
| 244 | * |
||
| 245 | * @param int $time Time when vars cached was created |
||
| 246 | * |
||
| 247 | * @return \M1\Vars\Cache\CacheProvider The cacheProvider object |
||
| 248 | */ |
||
| 249 | public function setTime($time) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the loaded Vars object |
||
| 257 | * |
||
| 258 | * @return \M1\Vars\Vars The loaded Vars object |
||
| 259 | */ |
||
| 260 | public function getLoadedVars() |
||
| 264 | |||
| 265 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.