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 |
||
| 27 | class Date |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Default variables for Date utility set |
||
| 31 | * |
||
| 32 | * @type array $defaults array of default variables |
||
| 33 | */ |
||
| 34 | public static $defaults = [ |
||
| 35 | "short_date" => "d.m.Y", |
||
| 36 | "short_time" => "H:i", |
||
| 37 | "short_time_with_seconds" => "H:i:s", |
||
| 38 | "short_datetime" => "d.m.Y H:i", |
||
| 39 | "short_datetime_with_seconds" => "d.m.Y H:i:s", |
||
| 40 | |||
| 41 | "seconds" => "seconds", |
||
| 42 | "minutes" => "minutes", |
||
| 43 | "hours" => "hours", |
||
| 44 | "days" => "days", |
||
| 45 | "weeks" => "weeks", |
||
| 46 | "months" => "months", |
||
| 47 | |||
| 48 | "yesterday" => "Yesterday", |
||
| 49 | "today" => "Today", |
||
| 50 | "tomorrow" => "Tomorrow", |
||
| 51 | |||
| 52 | "now" => "Now" |
||
| 53 | ]; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor to prevent new instances of Date class |
||
| 58 | * |
||
| 59 | * @return Date |
||
|
|
|||
| 60 | */ |
||
| 61 | final private function __construct() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Clone method to prevent duplication of Date class |
||
| 67 | * |
||
| 68 | * @return Date |
||
| 69 | */ |
||
| 70 | final private function __clone() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Unserialization method to prevent restoration of Date class |
||
| 76 | * |
||
| 77 | * @return Date |
||
| 78 | */ |
||
| 79 | final private function __wakeup() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets the default variables |
||
| 85 | * |
||
| 86 | * @param array $uDefaults variables to be set |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public static function setDefaults($uDefaults) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Timestamp of beginning of the day |
||
| 97 | * |
||
| 98 | * @param int $uTimestamp timestamp |
||
| 99 | * |
||
| 100 | * @return int timestamp of beginning of the day |
||
| 101 | */ |
||
| 102 | public static function beginningOfDay($uTimestamp) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Transforms given period into a time span |
||
| 109 | * |
||
| 110 | * @param int $uPeriod period |
||
| 111 | * @param bool $uNoDays return null if it passed a day |
||
| 112 | * |
||
| 113 | * @return null|array value and unit |
||
| 114 | */ |
||
| 115 | public static function ago($uPeriod, $uNoDays = false) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Transforms a timestamp to human-readable format |
||
| 150 | * |
||
| 151 | * @param int $uTimestamp timestamp |
||
| 152 | * @param int $uConstantTimestamp constant timestamp |
||
| 153 | * @param bool $uCalculateAgo uses ago method for past |
||
| 154 | * @param bool $uShowHours includes hour and minute |
||
| 155 | * |
||
| 156 | * @return string output |
||
| 157 | */ |
||
| 158 | public static function humanize($uTimestamp, $uConstantTimestamp, $uCalculateAgo = true, $uShowHours = true) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Transforms a timestamp to GMT format |
||
| 205 | * |
||
| 206 | * @param int $uTimestamp timestamp |
||
| 207 | * @param bool $uAddSuffix adds GMT at the end of the output |
||
| 208 | * |
||
| 209 | * @return string output |
||
| 210 | */ |
||
| 211 | public static function toGmt($uTimestamp, $uAddSuffix = true) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Transforms a timestamp to DOS format |
||
| 222 | * |
||
| 223 | * @param int $uTimestamp timestamp |
||
| 224 | * |
||
| 225 | * @return string output |
||
| 226 | */ |
||
| 227 | public static function toDos($uTimestamp) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Transforms a timestamp from GMT format |
||
| 251 | * |
||
| 252 | * @param int $uTimestamp timestamp |
||
| 253 | * |
||
| 254 | * @return string output |
||
| 255 | */ |
||
| 256 | public static function fromDos($uTimestamp) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Transforms a timestamp to database format |
||
| 270 | * |
||
| 271 | * @param int $uTimestamp timestamp |
||
| 272 | * @param string $uFormat destination format |
||
| 273 | * |
||
| 274 | * @return string output |
||
| 275 | */ |
||
| 276 | View Code Duplication | public static function toDb($uTimestamp, $uFormat = "d-m-Y H:i:s") |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Transforms a timestamp from database format |
||
| 295 | * |
||
| 296 | * @param int $uTimestamp timestamp |
||
| 297 | * @param string $uFormat source format |
||
| 298 | * |
||
| 299 | * @return string output |
||
| 300 | */ |
||
| 301 | public static function fromDb($uTimestamp, $uFormat = "d-m-Y H:i:s") |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Transforms a timestamp into another format |
||
| 317 | * |
||
| 318 | * @param int $uTimestamp timestamp |
||
| 319 | * @param string $uSourceFormat source format |
||
| 320 | * @param string $uDestinationFormat destination format |
||
| 321 | * |
||
| 322 | * @return string output |
||
| 323 | */ |
||
| 324 | View Code Duplication | public static function convert($uTimestamp, $uSourceFormat, $uDestinationFormat) |
|
| 338 | } |
||
| 339 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.