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 LotPresenter extends Presenter |
||
| 9 | { |
||
| 10 | const END_DATE = 'expire_date'; |
||
| 11 | |||
| 12 | const PUBLIC_DATE = 'public_date'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Render lot's name... |
||
| 16 | * |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | public function renderName() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Render drafted lot's name.. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function renderDraftedName() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function cover($size = null, $default) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Render end date from expiration_date timestamp. |
||
| 54 | * |
||
| 55 | * @param string $format |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function endDate($format = 'm/d/Y') |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Calc. difference from (expire day and today), |
||
| 73 | * end_date MUST be bigger than now date. |
||
| 74 | * |
||
| 75 | * @return \DateInterval |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function diffEndDate() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get expire date as string. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | |||
| 95 | |||
| 96 | View Code Duplication | public function getExpireDateAsString($format = 'd.m.Y') |
|
| 110 | |||
| 111 | View Code Duplication | public function getPublicDateAsString($format = 'd.m.Y') |
|
| 124 | |||
| 125 | |||
| 126 | public function renderPrice($price, $currency) |
||
| 130 | |||
| 131 | public function renderNewPrice() |
||
| 135 | |||
| 136 | public function renderOldPrice() |
||
| 140 | |||
| 141 | public function renderCurrency() |
||
| 145 | |||
| 146 | public function renderSalePercent() |
||
| 150 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.