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 |
||
| 8 | class BuiltInFunctionsExtension implements UqlExtensionInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var TokenStorageInterface |
||
| 12 | */ |
||
| 13 | private $tokenStorage; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var CurrentDateTimeProvider |
||
| 17 | */ |
||
| 18 | private $dateTimeProvider; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param TokenStorageInterface $tokenStorage |
||
| 22 | * @param CurrentDateTimeProvider $dateTimeProvider |
||
| 23 | */ |
||
| 24 | public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
||
| 25 | { |
||
| 26 | $this->tokenStorage = $tokenStorage; |
||
| 27 | $this->dateTimeProvider = $dateTimeProvider; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | public function getFunctions() |
||
| 34 | { |
||
| 35 | return [ |
||
| 36 | new UqlFunction('now', $this, 'now'), |
||
| 37 | new UqlFunction('startOfDay', $this, 'startOfDay'), |
||
| 38 | new UqlFunction('startOfWeek', $this, 'startOfWeek'), |
||
| 39 | new UqlFunction('startOfMonth', $this, 'startOfMonth'), |
||
| 40 | new UqlFunction('startOfYear', $this, 'startOfYear'), |
||
| 41 | new UqlFunction('currentUser', $this, 'currentUser'), |
||
| 42 | new UqlFunction('random', $this, 'random') |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Gets the current timestamp, with an offset string |
||
| 48 | * |
||
| 49 | * @param string $offset |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | public function now($offset = null) |
||
| 55 | { |
||
| 56 | $now = clone $this->dateTimeProvider->get(); |
||
| 57 | |||
| 58 | if ($offset) { |
||
|
|
|||
| 59 | $interval = \DateInterval::createFromDateString($offset); |
||
| 60 | $now->add($interval); |
||
| 61 | |||
| 62 | if ($now == $this->dateTimeProvider->get()) { |
||
| 63 | // The date didn't change therefore we assume the given offset is not valid |
||
| 64 | throw new \Exception($offset . ' is not a valid date/time interval.'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | return $now->format(\DateTime::ISO8601); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets a date with the hour 00:00:00 |
||
| 73 | * |
||
| 74 | * @param string $date |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | * @throws \Exception |
||
| 78 | */ |
||
| 79 | public function startOfDay($date = null) |
||
| 80 | { |
||
| 81 | $now = clone $this->dateTimeProvider->get(); |
||
| 82 | |||
| 83 | if ($date) { |
||
| 84 | $now = $this->modifyDate($now, $date); |
||
| 85 | } |
||
| 86 | |||
| 87 | $now->setTime(0, 0, 0); |
||
| 88 | |||
| 89 | return $now->format(\DateTime::ISO8601); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the Monday of the week for the specified date with the hour 00:00:00 |
||
| 94 | * |
||
| 95 | * @param string $date |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | * @throws \Exception |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function startOfWeek($date = null) |
|
| 101 | { |
||
| 102 | $now = clone $this->dateTimeProvider->get(); |
||
| 103 | |||
| 104 | if ($date) { |
||
| 105 | $now = $this->modifyDate($now, $date); |
||
| 106 | } |
||
| 107 | |||
| 108 | $year = $now->format('o'); // o = ISO-8601 year number |
||
| 109 | $week = $now->format('W'); // W = ISO-8601 week number of year, weeks starting on Monday |
||
| 110 | |||
| 111 | $startOfWeek = $now->setISODate($year, $week); |
||
| 112 | $startOfWeek->setTime(0, 0, 0); |
||
| 113 | |||
| 114 | return $startOfWeek->format(\DateTime::ISO8601); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Gets the first day of the month for the specified date with the hour 00:00:00 |
||
| 119 | * |
||
| 120 | * @param string $date |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | * @throws \Exception |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function startOfMonth($date = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Gets the first day of the year for the specified date with the hour 00:00:00 |
||
| 144 | * |
||
| 145 | * @param string $date |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | * @throws \Exception |
||
| 149 | */ |
||
| 150 | public function startOfYear($date = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Gets the current users' username |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function currentUser() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets a random value between $min and $max |
||
| 178 | * |
||
| 179 | * @param int $min |
||
| 180 | * @param int $max |
||
| 181 | * |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | public function random($min = 0, $max = 10) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param \DateTime $date |
||
| 191 | * @param string $change |
||
| 192 | * |
||
| 193 | * @return \DateTime |
||
| 194 | * @throws \Exception |
||
| 195 | */ |
||
| 196 | private function modifyDate(\DateTime $date, $change) |
||
| 206 | } |
||
| 207 |
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: