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 |
||
| 7 | class Timetable |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Timetable matrix. |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $timetable = null; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * DateTime keyword for the begining of the timetable. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $from = 'today'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Number of days to build the timetable forward. |
||
| 25 | * |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $future = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Starting time for each day. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $startAt = '09:00:00'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Finishing time for each day. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $finishAt = '18:00:00'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Interval between slots in minutes. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $interval = 30; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Services. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $services = ['default']; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Dimensions format of the timetable matrix. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $dimensions = ['date', 'service', 'time']; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Setter for beginning date. |
||
| 67 | * |
||
| 68 | * @param string $relative |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function from($relative) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Setter for number of days forward. |
||
| 81 | * |
||
| 82 | * @param int $days |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function future($days) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Setter for startAt time. |
||
| 95 | * |
||
| 96 | * @param string $time |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function startAt($time) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Setter for finishAt time. |
||
| 109 | * |
||
| 110 | * @param string $time |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function finishAt($time) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Setter for interval. |
||
| 123 | * |
||
| 124 | * @param int $minutes |
||
|
|
|||
| 125 | * |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function interval($interval = 30) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Setter for services. |
||
| 137 | * |
||
| 138 | * @param array $services |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function services($services) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Initialize Timetable. |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function init() |
||
| 172 | |||
| 173 | public function inflateServices() |
||
| 177 | |||
| 178 | public function inflateDates() |
||
| 189 | |||
| 190 | public function inflateTimes() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the Timetable matrix. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function get() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set the capacity for a slot. |
||
| 222 | * |
||
| 223 | * @param string $date |
||
| 224 | * @param string $time |
||
| 225 | * @param string $service |
||
| 226 | * @param int $capacity |
||
| 227 | * |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function capacity($date, $time, $service, $capacity = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get a concrete Timetable path. |
||
| 241 | * |
||
| 242 | * @param array $segments |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | private function dimensions(array $segments) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Setter for the dimensions format. |
||
| 257 | * |
||
| 258 | * @param string $dimensions |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function format($dimensions) |
||
| 274 | |||
| 275 | ///////////// |
||
| 276 | // Helpers // |
||
| 277 | ///////////// |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Helper method for Arr::set |
||
| 281 | * |
||
| 282 | * @param array &$array |
||
| 283 | * @param mixed $key |
||
| 284 | * @param mixed $value |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | private function array_set(&$array, $key, $value) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Helper method for Arr::get |
||
| 295 | * |
||
| 296 | * @param array &$array |
||
| 297 | * @param mixed $key |
||
| 298 | * @param mixed $default |
||
| 299 | * |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | private function array_get($array, $key, $default = null) |
||
| 306 | |||
| 307 | private function array_substitute(&$array1, $array2) |
||
| 313 | } |
||
| 314 |
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.