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 BuiltInFunctionsExtension implements UqlExtensionInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var TokenStorageInterface |
||
| 13 | */ |
||
| 14 | private $tokenStorage; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var CurrentDateTimeProvider |
||
| 18 | */ |
||
| 19 | private $dateTimeProvider; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param TokenStorageInterface $tokenStorage |
||
| 23 | * @param CurrentDateTimeProvider $dateTimeProvider |
||
| 24 | */ |
||
| 25 | public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | public function getFunctions() |
||
| 35 | { |
||
| 36 | return [ |
||
| 37 | new UqlFunction('now', $this, 'now'), |
||
| 38 | new UqlFunction('startOfDay', $this, 'startOfDay'), |
||
| 39 | new UqlFunction('startOfWeek', $this, 'startOfWeek'), |
||
| 40 | new UqlFunction('startOfMonth', $this, 'startOfMonth'), |
||
| 41 | new UqlFunction('startOfYear', $this, 'startOfYear'), |
||
| 42 | new UqlFunction('currentUser', $this, 'currentUser'), |
||
| 43 | new UqlFunction('random', $this, 'random') |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Gets the current timestamp, with an offset string |
||
| 49 | * |
||
| 50 | * @param string $offset |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | * @throws \Exception |
||
| 54 | */ |
||
| 55 | public function now($offset = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets a date with the hour 00:00:00 |
||
| 74 | * |
||
| 75 | * @param string $date |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | * @throws \Exception |
||
| 79 | */ |
||
| 80 | public function startOfDay($date = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets the Monday of the week for the specified date with the hour 00:00:00 |
||
| 95 | * |
||
| 96 | * @param string $date |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | * @throws \Exception |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function startOfWeek($date = null) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the first day of the month for the specified date with the hour 00:00:00 |
||
| 120 | * |
||
| 121 | * @param string $date |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function startOfMonth($date = null) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Gets the first day of the year for the specified date with the hour 00:00:00 |
||
| 145 | * |
||
| 146 | * @param string $date |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function startOfYear($date = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Gets the current users' username |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function currentUser() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Gets a random value between $min and $max |
||
| 179 | * |
||
| 180 | * @param int $min |
||
| 181 | * @param int $max |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function random($min = 0, $max = 10) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param \DateTime $date |
||
| 192 | * @param string $change |
||
| 193 | * |
||
| 194 | * @return \DateTime |
||
| 195 | * @throws \Exception |
||
| 196 | */ |
||
| 197 | private function modifyDate(\DateTime $date, $change) |
||
| 207 | } |
||
| 208 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: