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 |
||
| 10 | class BuiltInFunctionsExtension extends AbstractTableBundleExtension |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var TokenStorageInterface |
||
| 14 | */ |
||
| 15 | private $tokenStorage; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var CurrentDateTimeProvider |
||
| 19 | */ |
||
| 20 | private $dateTimeProvider; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param TokenStorageInterface $tokenStorage |
||
| 24 | * @param CurrentDateTimeProvider $dateTimeProvider |
||
| 25 | */ |
||
| 26 | public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | public function getName() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function getFunctions() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Gets the current timestamp, with an offset string |
||
| 56 | * |
||
| 57 | * @param string $offset |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | public function now($offset = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Gets a date with the hour 00:00:00 |
||
| 81 | * |
||
| 82 | * @param string $date |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | * @throws \Exception |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function startOfDay($date = null) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Gets a date with the hour 23:59:59 |
||
| 102 | * |
||
| 103 | * @param string $date |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | * @throws \Exception |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function endOfDay($date = null) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Gets the current users' username |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function currentUser() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets a random value between $min and $max |
||
| 133 | * |
||
| 134 | * @param int $min |
||
| 135 | * @param int $max |
||
| 136 | * |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function random($min = 0, $max = 10) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param \DateTime $date |
||
| 146 | * @param string $change |
||
| 147 | * |
||
| 148 | * @return \DateTime |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | private function modifyDate(\DateTime $date, $change) |
||
| 161 | } |
||
| 162 |
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: