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 FBasket extends FCollectionUI |
||
| 10 | {
|
||
| 11 | static $templateCounter = 0; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Пример: |
||
| 15 | * <pre> |
||
| 16 | * $this->basket = new FBasket('basket');
|
||
| 17 | * $this->basket->collectionItemsForSum = array('options', 'parameters');
|
||
| 18 | * или |
||
| 19 | * $this->basket->collectionItemsForSum = FBasket::COLLECTION_ITEMS_ROOT; |
||
| 20 | * </pre> |
||
| 21 | * |
||
| 22 | * Ключи collectionItems стоимость которых нужно считать, если елементы не сгруппированы по индексу, то нужно указать ключ FBasket::COLLECTION_ITEMS_ROOT |
||
| 23 | * @var mixed |
||
| 24 | */ |
||
| 25 | public $collectionItemsForSum; |
||
| 26 | |||
| 27 | public $classFastOrderShowButton = 'fast-order-show-{keyCollection}';
|
||
| 28 | |||
| 29 | public $classFastOrderCloseButton = 'fast-order-close-{keyCollection}';
|
||
| 30 | |||
| 31 | public $classSubmitFastOrderButton = 'fast-order-submit-{keyCollection}';
|
||
| 32 | |||
| 33 | public $classRepeatOrderButton = 'repeat-order-{keyCollection}';
|
||
| 34 | |||
| 35 | public $classTooltip = 'tooltip-{keyCollection}';
|
||
| 36 | |||
| 37 | public $fastOrderFormId = 'fast-order-form-{keyCollection}';
|
||
| 38 | |||
| 39 | public $fastOrderFormSuccessId = 'fast-order-form-success-{keyCollection}';
|
||
| 40 | |||
| 41 | protected $fastOrderFormPopupId = 'fast-order-popup-{keyCollection}';
|
||
| 42 | |||
| 43 | protected $fastOrderPopupContainer = 'div'; |
||
| 44 | |||
| 45 | protected $templates; |
||
| 46 | |||
| 47 | protected $templateId = 'template-{keyCollection}-';
|
||
| 48 | |||
| 49 | public function collectionItemSum($index = null) |
||
| 63 | |||
| 64 | public function getSumTotal() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Строит кнопку для быстрого заказа |
||
| 81 | * Пример: |
||
| 82 | * <pre> |
||
| 83 | * $this->basket->buttonFastOrder( |
||
| 84 | * $model, |
||
| 85 | * 'Купить в один клик', |
||
| 86 | * array('class' => 'red'),
|
||
| 87 | * array( |
||
| 88 | * 'name' => $data->name, |
||
| 89 | * 'url' => $data->url, |
||
| 90 | * 'img' => $image ? $image->pre : '', |
||
| 91 | * 'description:selector' => '.parent-block .description' |
||
| 92 | * )); |
||
| 93 | * </pre> |
||
| 94 | * @param array|FCollectionElementBehavior|CActiveRecord $model |
||
| 95 | * @param string $text текст кнопки |
||
| 96 | * @param array $htmlOptions |
||
| 97 | * @param array $formData массив данных которые будут подставлятся в попап быстрого заказа. Вместо данных можно использовать селектор для копирования содержимого блока. Формат задания селектора array('description:selector' => '.parent-block .description')
|
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function buttonFastOrder($model, $text = '', $htmlOptions = array(), $formData = array()) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Кнопка закрытия попапа быстрого заказа |
||
| 113 | * @param array $htmlOptions |
||
| 114 | * @param string $text |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function buttonFastOrderClose($htmlOptions = array(), $text = '') |
||
| 124 | |||
| 125 | 7 | public function buttonSubmitFastOrder($text = '', $htmlOptions = array()) |
|
| 131 | |||
| 132 | public function buttonRepeatOrder($orderId, $text = '', $htmlOptions = array()) |
||
| 139 | |||
| 140 | 7 | public function beginFastOrderPopup($htmlOptions = array()) |
|
| 145 | |||
| 146 | 7 | public function endFastOrderPopup() |
|
| 150 | |||
| 151 | 7 | public function beginTemplate($htmlOptions = array(), $tag = 'div') |
|
| 159 | |||
| 160 | 7 | public function endTemplate($tag = 'div') |
|
| 169 | |||
| 170 | 7 | protected function registerScripts() |
|
| 179 | |||
| 180 | 7 | protected function registerScriptButtonFastOrder() |
|
| 253 | |||
| 254 | 7 | protected function registerScriptButtonFastOrderClose() |
|
| 262 | |||
| 263 | 7 | protected function registerScriptButtonSubmitFastOrder() |
|
| 277 | |||
| 278 | 7 | View Code Duplication | protected function registerScriptButtonRepeatOrder() |
| 293 | } |
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.