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 |
||
| 19 | class Answer extends \hipanel\base\Model |
||
| 20 | { |
||
| 21 | use \hipanel\base\ModelTrait; |
||
| 22 | |||
| 23 | public static $i18nDictionary = 'hipanel:ticket'; |
||
| 24 | |||
| 25 | public static function tableName() |
||
| 26 | { |
||
| 27 | return 'thread'; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function init() |
||
| 31 | { |
||
| 32 | $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']); |
||
| 33 | $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function load($data, $formName = null) |
||
| 37 | { |
||
| 38 | $result = parent::load($data, $formName); |
||
| 39 | $this->prepareSpentTime(); |
||
| 40 | |||
| 41 | return $result; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function beforeChange($event) |
||
|
|
|||
| 45 | { |
||
| 46 | $this->switchId(); |
||
| 47 | |||
| 48 | return true; |
||
| 49 | } |
||
| 50 | |||
| 51 | private function switchId() |
||
| 52 | { |
||
| 53 | if ($this->scenario === 'update') { |
||
| 54 | $this->id = $this->answer_id; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function behaviors() |
|
| 62 | { |
||
| 63 | return [ |
||
| 64 | [ |
||
| 65 | 'class' => 'hipanel\behaviors\File', |
||
| 66 | 'attribute' => 'file', |
||
| 67 | 'targetAttribute' => 'file_ids', |
||
| 68 | 'scenarios' => ['create', 'answer'], |
||
| 69 | ], |
||
| 70 | ]; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function attributes() |
||
| 77 | { |
||
| 78 | return [ |
||
| 79 | 'id', |
||
| 80 | 'answer_id', |
||
| 81 | 'message', |
||
| 82 | 'private', |
||
| 83 | 'is_private', |
||
| 84 | 'is_answer', |
||
| 85 | 'changed_num', |
||
| 86 | 'author', |
||
| 87 | 'author_id', |
||
| 88 | 'author_seller', |
||
| 89 | 'seller_id', |
||
| 90 | 'account', |
||
| 91 | 'email_hash', |
||
| 92 | 'create_time', |
||
| 93 | 'spent', |
||
| 94 | 'spent_hours', |
||
| 95 | 'is_moved', |
||
| 96 | 'ip', |
||
| 97 | 'file', |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function rules() |
||
| 105 | { |
||
| 106 | return [ |
||
| 107 | [ |
||
| 108 | ['id', 'answer_id', 'spent', 'spent_hours'], |
||
| 109 | 'integer', |
||
| 110 | 'enableClientValidation' => false, |
||
| 111 | ], |
||
| 112 | [ |
||
| 113 | ['message', 'id', 'answer_id'], |
||
| 114 | 'required', |
||
| 115 | 'on' => 'update', |
||
| 116 | ], |
||
| 117 | [ |
||
| 118 | ['spent', '!spent_hours'], |
||
| 119 | 'safe', |
||
| 120 | 'on' => 'update', |
||
| 121 | ], |
||
| 122 | [ |
||
| 123 | ['is_private'], |
||
| 124 | 'boolean', |
||
| 125 | 'on' => 'update', |
||
| 126 | 'when' => function () { |
||
| 127 | return Yii::$app->user->can('support'); |
||
| 128 | }, |
||
| 129 | ], |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function attributeLabels() |
||
| 137 | { |
||
| 138 | return $this->mergeAttributeLabels([ |
||
| 139 | 'author' => Yii::t('hipanel:ticket', 'Author'), |
||
| 140 | 'author_id' => Yii::t('hipanel:ticket', 'Author'), |
||
| 141 | 'recipient' => Yii::t('hipanel:ticket', 'Recipient'), |
||
| 142 | 'is_private' => Yii::t('hipanel:ticket', 'Make private'), |
||
| 143 | 'responsible' => Yii::t('hipanel:ticket', 'Assignee'), |
||
| 144 | 'responsible_id' => Yii::t('hipanel:ticket', 'Assignee'), |
||
| 145 | 'spent' => Yii::t('hipanel:ticket', 'Spent time'), |
||
| 146 | 'create_time' => Yii::t('hipanel:ticket', 'Created'), |
||
| 147 | 'a_reply_time' => Yii::t('hipanel:ticket', 'a_reply_time'), |
||
| 148 | 'file' => Yii::t('hipanel:ticket', 'Files'), |
||
| 149 | 'lastanswer' => Yii::t('hipanel:ticket', 'Last answer'), |
||
| 150 | 'author_seller' => Yii::t('hipanel:ticket', 'Seller'), |
||
| 151 | ]); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getClient() |
||
| 158 | |||
| 159 | public function getClient_id() |
||
| 163 | |||
| 164 | public function getSeller() |
||
| 168 | |||
| 169 | public function getSeller_id() |
||
| 173 | |||
| 174 | public function prepareSpentTime() |
||
| 178 | |||
| 179 | public function getThread() |
||
| 183 | |||
| 184 | public function getFiles() |
||
| 188 | |||
| 189 | public function scenarioActions() |
||
| 190 | { |
||
| 195 | |||
| 196 | public function canSetSpent() |
||
| 197 | { |
||
| 198 | return $this->thread->canSetSpent(); |
||
| 199 | } |
||
| 200 | } |
||
| 201 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.