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 |
||
| 24 | abstract class AbstractTimeGraphWidget extends AbstractGraphWidget implements TimeGraphWidgetInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array $dbRows |
||
| 28 | */ |
||
| 29 | private $dbRows; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The date grouping type can be "hour", "day" or "month". |
||
| 33 | * |
||
| 34 | * @var string $groupingType |
||
| 35 | */ |
||
| 36 | private $groupingType; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string $dateFirnat |
||
| 40 | */ |
||
| 41 | private $dateFormat; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string $sqlDateFormat |
||
| 45 | */ |
||
| 46 | private $sqlDateFormat; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DateTimeInterface $startDate |
||
| 50 | */ |
||
| 51 | private $startDate; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var DateTimeInterface $endDate |
||
| 55 | */ |
||
| 56 | private $endDate; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var DateInterval $dateInterval |
||
| 60 | */ |
||
| 61 | private $dateInterval; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $type The group type. |
||
| 65 | * @throws InvalidArgumentException If the group type is not a valid type. |
||
| 66 | * @return TimeGraphWidgetInterface Chainable |
||
| 67 | */ |
||
| 68 | public function setGroupingType($type) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function groupingType() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $format The date format. |
||
| 96 | * @throws InvalidArgumentException If the format argument is not a string. |
||
| 97 | * @return TimeGraphWidgetInterface Chainable |
||
| 98 | */ |
||
| 99 | public function setDateFormat($format) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function dateFormat() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $format The date format. |
||
| 120 | * @throws InvalidArgumentException If the format argument is not a string. |
||
| 121 | * @return TimeGraphWidgetInterface Chainable |
||
| 122 | */ |
||
| 123 | public function setSqlDateFormat($format) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function sqlDateFormat() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string|DateTimeInterface $ts The start date. |
||
| 144 | * @throws InvalidArgumentException If the date is not a valid datetime format. |
||
| 145 | * @return TimeGraphWidgetInterface Chainable |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function setStartDate($ts) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return DateTimeInterface |
||
| 169 | */ |
||
| 170 | public function startDate() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string|DateTimeInterface $ts The end date. |
||
| 177 | * @throws InvalidArgumentException If the date is not a valid datetime format. |
||
| 178 | * @return TimeGraphWidgetInterface Chainable |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function setEndDate($ts) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @return DateTimeInterface |
||
| 202 | */ |
||
| 203 | public function endDate() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string|DateInterval $interval The date interval, between "categories". |
||
| 210 | * @throws InvalidArgumentException If the argument is not a string or an interval object. |
||
| 211 | * @return TimeGraphWidgetInterface Chainable |
||
| 212 | */ |
||
| 213 | public function setDateInterval($interval) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return DateInterval |
||
| 229 | */ |
||
| 230 | public function dateInterval() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return TimeGraphWidgetInterface Chainable |
||
| 237 | */ |
||
| 238 | View Code Duplication | protected function setGroupingTypeByHour() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return TimeGraphWidgetInterface Chainable |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function setGroupingTypeByDay() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return TimeGraphWidgetInterface Chainable |
||
| 265 | */ |
||
| 266 | View Code Duplication | protected function setGroupingTypeByMonth() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | protected function dbRows() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Fill all rows with 0 value for all series, when it is unset. |
||
| 327 | * |
||
| 328 | * @param array $rows The row structure to fill. |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | protected function fillRows(array $rows) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return array Categories structure. |
||
| 354 | */ |
||
| 355 | public function categories() |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * @return array Series structure. |
||
| 365 | */ |
||
| 366 | public function series() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | abstract protected function objType(); |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | abstract protected function seriesOptions(); |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | abstract protected function categoryFunction(); |
||
| 398 | } |
||
| 399 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: