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 |
||
| 21 | abstract class AbstractTimeGraphWidget extends AbstractGraphWidget implements TimeGraphWidgetInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array $dbRows |
||
| 25 | */ |
||
| 26 | private $dbRows; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string $groupingType |
||
| 30 | */ |
||
| 31 | private $groupingType; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var strubg $dateFirnat |
||
| 35 | */ |
||
| 36 | private $dateFormat; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string $sqlDateFormat |
||
| 40 | */ |
||
| 41 | private $sqlDateFormat; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var DateTimeInterface $startDate |
||
| 45 | */ |
||
| 46 | private $startDate; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DateTimeInterface $endDate |
||
| 50 | */ |
||
| 51 | private $endDate; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var DateInterval $dateInterval |
||
| 55 | */ |
||
| 56 | private $dateInterval; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $type The group type. |
||
| 60 | * @throws InvalidArgumentException If the group type is not a valid type. |
||
| 61 | */ |
||
| 62 | public function setGroupingType($type) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function groupingType() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $format The date format. |
||
| 90 | * @throws InvalidArgumentException If the format argument is not a string. |
||
| 91 | * @return UserTimeGraphWidget Chainable |
||
| 92 | */ |
||
| 93 | public function setDateFormat($format) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function dateFormat() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $format The date format. |
||
| 114 | * @throws InvalidArgumentException If the format argument is not a string. |
||
| 115 | * @return UserTimeGraphWidget Chainable |
||
| 116 | */ |
||
| 117 | public function setSqlDateFormat($format) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function sqlDateFormat() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string|DateTimeInterface $ts The start date. |
||
| 138 | * @throws InvalidArgumentException If the date is not a valid datetime format. |
||
| 139 | * @return UserTimeGraphWidget Chainable |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function setStartDate($ts) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return DateTimeInterface |
||
| 163 | */ |
||
| 164 | public function startDate() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string|DateTimeInterface $ts The end date. |
||
| 171 | * @throws InvalidArgumentException If the date is not a valid datetime format. |
||
| 172 | * @return UserTimeGraphWidget Chainable |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function setEndDate($ts) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return DateTimeInterface |
||
| 196 | */ |
||
| 197 | public function endDate() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string|DateInterval $interval The date interval, between "categories". |
||
| 204 | * @throws InvalidArgumentException If the argument is not a string or an interval object. |
||
| 205 | * @return UserTimeGraphWidget Chainable |
||
| 206 | */ |
||
| 207 | public function setDateInterval($interval) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return DateInterval |
||
| 223 | */ |
||
| 224 | public function dateInterval() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return UserTimeGraphWidget Chainable |
||
| 231 | */ |
||
| 232 | View Code Duplication | protected function setGroupingTypeByHour() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return UserTimeGraphWidget Chainable |
||
| 245 | */ |
||
| 246 | View Code Duplication | protected function setGroupingTypeByDay() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return UserTimeGraphWidget Chainable |
||
| 259 | */ |
||
| 260 | View Code Duplication | protected function setGroupingTypeByMonth() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | protected function dbRows() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Fill all rows with 0 value for all series, when it is unset. |
||
| 321 | * |
||
| 322 | * @param array $rows The row structure to fill. |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | protected function fillRows(array $rows) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | abstract protected function objType(); |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | abstract protected function seriesOptions(); |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | abstract protected function categoryFunction(); |
||
| 360 | } |
||
| 361 |
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: