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 |
||
| 9 | class CFastTrackCache |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Cache is disabled to start with. |
||
| 13 | */ |
||
| 14 | private $enabled = false; |
||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * Path to the cache directory. |
||
| 20 | */ |
||
| 21 | private $path; |
||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Filename of current cache item. |
||
| 27 | */ |
||
| 28 | private $filename; |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Container with items to store as cached item. |
||
| 34 | */ |
||
| 35 | private $container; |
||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Enable or disable cache. |
||
| 41 | * |
||
| 42 | * @param boolean $enable set to true to enable, false to disable |
||
|
|
|||
| 43 | * |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | public function enable($enabled) |
||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Set the path to the cache dir which must exist. |
||
| 56 | * |
||
| 57 | * @param string $path to the cache dir. |
||
| 58 | * |
||
| 59 | * @throws Exception when $path is not a directory. |
||
| 60 | * |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function setCacheDir($path) |
||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Set the filename to store in cache, use the querystring to create that |
||
| 78 | * filename. |
||
| 79 | * |
||
| 80 | * @param array $clear items to clear in $_GET when creating the filename. |
||
| 81 | * |
||
| 82 | * @return string as filename created. |
||
| 83 | */ |
||
| 84 | public function setFilename($clear) |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Add header items. |
||
| 109 | * |
||
| 110 | * @param string $header add this as header. |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function addHeader($header) |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Add header items on output, these are not output when 304. |
||
| 124 | * |
||
| 125 | * @param string $header add this as header. |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function addHeaderOnOutput($header) |
||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Set path to source image to. |
||
| 139 | * |
||
| 140 | * @param string $source path to source image file. |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setSource($source) |
||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Set last modified of source image, use to check for 304. |
||
| 154 | * |
||
| 155 | * @param string $lastModified |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function setLastModified($lastModified) |
||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Get filename of cached item. |
||
| 169 | * |
||
| 170 | * @return string as filename. |
||
| 171 | */ |
||
| 172 | public function getFilename() |
||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Write current item to cache. |
||
| 181 | * |
||
| 182 | * @return boolean if cache file was written. |
||
| 183 | */ |
||
| 184 | public function writeToCache() |
||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Output current item from cache, if available. |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function output() |
||
| 241 | } |
||
| 242 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.