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 extends AbstractTableBundleExtension |
||
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 getName() |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function getFunctions() |
||
54 | |||
55 | /** |
||
56 | * Gets the current timestamp, with an offset string |
||
57 | * |
||
58 | * @param string $offset |
||
59 | * |
||
60 | * @return string |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | public function now($offset = null) |
||
79 | |||
80 | /** |
||
81 | * Gets a date with the hour 00:00:00 |
||
82 | * |
||
83 | * @param string $date |
||
84 | * |
||
85 | * @return string |
||
86 | * @throws \Exception |
||
87 | */ |
||
88 | public function startOfDay($date = null) |
||
100 | |||
101 | /** |
||
102 | * Gets the Monday of the week for the specified date with the hour 00:00:00 |
||
103 | * |
||
104 | * @param string $date |
||
105 | * |
||
106 | * @return string |
||
107 | * @throws \Exception |
||
108 | */ |
||
109 | View Code Duplication | public function startOfWeek($date = null) |
|
125 | |||
126 | /** |
||
127 | * Gets the first day of the month for the specified date with the hour 00:00:00 |
||
128 | * |
||
129 | * @param string $date |
||
130 | * |
||
131 | * @return string |
||
132 | * @throws \Exception |
||
133 | */ |
||
134 | View Code Duplication | public function startOfMonth($date = null) |
|
150 | |||
151 | /** |
||
152 | * Gets the first day of the year for the specified date with the hour 00:00:00 |
||
153 | * |
||
154 | * @param string $date |
||
155 | * |
||
156 | * @return string |
||
157 | * @throws \Exception |
||
158 | */ |
||
159 | public function startOfYear($date = null) |
||
174 | |||
175 | /** |
||
176 | * Gets the current users' username |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function currentUser() |
||
184 | |||
185 | /** |
||
186 | * Gets a random value between $min and $max |
||
187 | * |
||
188 | * @param int $min |
||
189 | * @param int $max |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | public function random($min = 0, $max = 10) |
||
197 | |||
198 | /** |
||
199 | * @param \DateTime $date |
||
200 | * @param string $change |
||
201 | * |
||
202 | * @return \DateTime |
||
203 | * @throws \Exception |
||
204 | */ |
||
205 | private function modifyDate(\DateTime $date, $change) |
||
215 | } |
||
216 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: