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 |
||
| 14 | class Transfer extends \Model |
||
| 15 | { |
||
| 16 | public static $cols = [ |
||
| 17 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
| 18 | 'to_user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'toUser'], |
||
| 19 | 'currency_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'currency'], |
||
| 20 | 'amount' => ['type' => 'decimal'], |
||
| 21 | 'code' => ['type' => 'text'], |
||
| 22 | 'comment' => ['type' => 'textarea', 'validator' => 'commentClean'], |
||
| 23 | 'complete' => ['type' => 'bool'], |
||
| 24 | 'canceled' => ['type' => 'bool'], |
||
| 25 | 'date_create' => ['type' => 'dateTime'], |
||
| 26 | ]; |
||
| 27 | public static $labels = [ |
||
| 28 | 'user_id' => 'От кого', |
||
| 29 | 'to_user_id' => 'Кому', |
||
| 30 | 'currency_id' => 'Валюта', |
||
| 31 | 'amount' => 'Сумма', |
||
| 32 | 'code' => 'Код подтверждения', |
||
| 33 | 'comment' => 'Комментарий', |
||
| 34 | 'complete' => 'Завершен', |
||
| 35 | 'canceled' => 'Отменен', |
||
| 36 | 'date_create' => 'Дата создания', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | public static function itemName($item) |
||
| 43 | |||
| 44 | public static $dataManagers = [ |
||
| 45 | 'manager' => [ |
||
| 46 | 'preSort' => [ |
||
| 47 | 'date_create' => 'desc' |
||
| 48 | ], |
||
| 49 | 'name' => 'Переводы', |
||
| 50 | 'cols' => [ |
||
| 51 | 'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
||
| 52 | ], |
||
| 53 | 'actions' => [ |
||
| 54 | 'Money\CancelTransfer', 'Money\CompleteTransfer' |
||
| 55 | ], |
||
| 56 | 'sortable' => [ |
||
| 57 | 'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
||
| 58 | ], |
||
| 59 | 'filters' => [ |
||
| 60 | 'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
||
| 61 | ] |
||
| 62 | ] |
||
| 63 | ]; |
||
| 64 | public static $forms = [ |
||
| 65 | 'transfer' => [ |
||
| 66 | 'name' => 'Перевод средств', |
||
| 67 | 'successText' => 'Операция перевода средств была успешно начата', |
||
| 68 | 'inputs' => [ |
||
| 69 | 'userSearch' => [ |
||
| 70 | 'type' => 'search', |
||
| 71 | 'source' => 'relation', |
||
| 72 | 'relation' => 'toUser', |
||
| 73 | 'showCol' => [ |
||
| 74 | 'type' => 'staticMethod', |
||
| 75 | 'class' => 'Money\Transfer', |
||
| 76 | 'method' => 'itemName', |
||
| 77 | ], |
||
| 78 | 'label' => 'Получатель', |
||
| 79 | 'cols' => [ |
||
| 80 | 'info:first_name', |
||
| 81 | 'info:last_name', |
||
| 82 | 'info:middle_name', |
||
| 83 | 'mail' |
||
| 84 | ], |
||
| 85 | 'col' => 'to_user_id', |
||
| 86 | 'required' => true, |
||
| 87 | 'validator' => 'userSearch' |
||
| 88 | ], |
||
| 89 | 'wallets' => [ |
||
| 90 | 'type' => 'select', |
||
| 91 | 'source' => 'method', |
||
| 92 | 'module' => 'Money', |
||
| 93 | 'method' => 'getUserWallets', |
||
| 94 | 'params' => [null, false, true, true], |
||
| 95 | 'label' => 'Кошелек', |
||
| 96 | 'col' => 'currency_id', |
||
| 97 | 'required' => true |
||
| 98 | ], |
||
| 99 | 'amount' => [ |
||
| 100 | 'type' => 'number', |
||
| 101 | 'validator' => 'amount', |
||
| 102 | 'required' => true |
||
| 103 | ], |
||
| 104 | ], |
||
| 105 | 'map' => [ |
||
| 106 | ['userSearch'], |
||
| 107 | ['wallets', 'amount'], |
||
| 108 | ['comment'] |
||
| 109 | ] |
||
| 110 | ] |
||
| 111 | ]; |
||
| 112 | |||
| 113 | public static function validators() |
||
| 164 | |||
| 165 | View Code Duplication | public static function relations() |
|
| 182 | |||
| 183 | public function name() |
||
| 187 | |||
| 188 | public function cancel() |
||
| 206 | |||
| 207 | public function complete() |
||
| 225 | |||
| 226 | } |
||
| 227 |