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 Fitter implements FitterInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Empty filler |
||
| 14 | * |
||
| 15 | * In body fit process, newly created key will use this as value. This |
||
| 16 | * will not affect head, which will use key as filler. |
||
| 17 | * |
||
| 18 | * Default value also set in ListView configs with key 'fitEmptyFiller'. |
||
| 19 | * |
||
| 20 | * @see ListView::getDefaultConfigs() |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $emptyFiller = ' '; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Fit mode |
||
| 28 | * |
||
| 29 | * Default value also set in ListView configs with key 'fitMode'. |
||
| 30 | * |
||
| 31 | * @see ListView::getDefaultConfigs() |
||
| 32 | * @see FitMode |
||
| 33 | * |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $mode = FitMode::TO_TITLE; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | * |
||
| 42 | * Skip if their key are same, or either is empty. |
||
| 43 | * |
||
| 44 | * @throws InvalidFitModeException |
||
| 45 | */ |
||
| 46 | public function fit(ListDto $listDto) |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Fit each row in body with given keys |
||
| 93 | * |
||
| 94 | * If row index is not in given keys, it will be dropped. If given keys is |
||
| 95 | * not in row index, it will be created with filling value. |
||
| 96 | * |
||
| 97 | * @param ListDto $listDto |
||
| 98 | * @param array $keys |
||
| 99 | */ |
||
| 100 | protected function fitBody(ListDto $listDto, array $keys) |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Fit head with given keys |
||
| 129 | * |
||
| 130 | * Drop head key not in given keys, and create new if given keys is not |
||
| 131 | * exists in head array. |
||
| 132 | * |
||
| 133 | * @param ListDto $listDto |
||
| 134 | * @param array $keys |
||
| 135 | */ |
||
| 136 | protected function fitHead(ListDto $listDto, array $keys) |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function setEmptyFiller($emptyFiller) |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function setMode($mode) |
||
| 177 | } |
||
| 178 |
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.