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 |
||
| 36 | class Submission extends \yii\db\ActiveRecord |
||
| 37 | { |
||
| 38 | const STATUS_NEW = 0; |
||
| 39 | const STATUS_SUCCESS = 1; |
||
| 40 | const STATUS_ERROR = -1; |
||
| 41 | const STATUS_HOPELESS_ERROR = -2; |
||
| 42 | const STATUS_FATAL_ERROR = -3; |
||
| 43 | |||
| 44 | protected $subject = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return null |
||
|
|
|||
| 48 | */ |
||
| 49 | public function getSubject() |
||
| 50 | { |
||
| 51 | $message = $this->form->subject_template; |
||
| 52 | $params = array_reduce( |
||
| 53 | array_keys($this->abstractModel->getAttributes()), |
||
| 54 | function ($arr, $i) { |
||
| 55 | $arr['{'.$i.'}'] = $this->property($i); |
||
| 56 | return $arr; |
||
| 57 | }, |
||
| 58 | [] |
||
| 59 | ); |
||
| 60 | $params['{id}'] = $this->id; |
||
| 61 | $params['{form_name}'] = $this->form->name; |
||
| 62 | return strtr($message, $params); |
||
| 63 | |||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param null $subject |
||
| 68 | */ |
||
| 69 | public function setSubject($subject) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | public static function tableName() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @inheritdoc |
||
| 84 | */ |
||
| 85 | public function rules() |
||
| 86 | { |
||
| 87 | return [ |
||
| 88 | [['form_id'], 'required'], |
||
| 89 | [ |
||
| 90 | ['form_id', 'processed_by_user_id', 'processed', 'form_fill_time', 'is_deleted', 'sending_status'], |
||
| 91 | 'integer' |
||
| 92 | ], |
||
| 93 | [['date_received', 'date_viewed', 'date_processed', 'visit_start_date'], 'safe'], |
||
| 94 | [ |
||
| 95 | [ |
||
| 96 | 'ip', |
||
| 97 | 'user_agent', |
||
| 98 | 'piwik_visitor_id', |
||
| 99 | 'additional_information', |
||
| 100 | 'internal_comment', |
||
| 101 | 'submission_referrer', |
||
| 102 | 'visitor_referrer', |
||
| 103 | 'visitor_landing' |
||
| 104 | ], |
||
| 105 | 'string' |
||
| 106 | ], |
||
| 107 | [['spam'], 'integer'] |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function behaviors() |
||
| 112 | { |
||
| 113 | return [ |
||
| 114 | [ |
||
| 115 | 'class' => HasProperties::className(), |
||
| 116 | ], |
||
| 117 | [ |
||
| 118 | 'class' => \devgroup\TagDependencyHelper\ActiveRecordHelper::className(), |
||
| 119 | ], |
||
| 120 | ]; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | public function attributeLabels() |
||
| 127 | { |
||
| 128 | return [ |
||
| 129 | 'id' => Yii::t('app', 'ID'), |
||
| 130 | 'form_id' => Yii::t('app', 'Form ID'), |
||
| 131 | 'date_received' => Yii::t('app', 'Date Received'), |
||
| 132 | 'ip' => Yii::t('app', 'Ip'), |
||
| 133 | 'user_agent' => Yii::t('app', 'User Agent'), |
||
| 134 | 'piwik_visitor_id' => Yii::t('app', 'Piwik Visitor ID'), |
||
| 135 | 'additional_information' => Yii::t('app', 'Additional Information'), |
||
| 136 | 'date_viewed' => Yii::t('app', 'Date Viewed'), |
||
| 137 | 'date_processed' => Yii::t('app', 'Date Processed'), |
||
| 138 | 'processed_by_user_id' => Yii::t('app', 'Processed By User ID'), |
||
| 139 | 'processed' => Yii::t('app', 'Processed'), |
||
| 140 | 'internal_comment' => Yii::t('app', 'Internal Comment'), |
||
| 141 | 'submission_referrer' => Yii::t('app', 'Submission Referrer'), |
||
| 142 | 'visitor_referrer' => Yii::t('app', 'Visitor Referrer'), |
||
| 143 | 'visitor_landing' => Yii::t('app', 'Visitor Landing'), |
||
| 144 | 'visit_start_date' => Yii::t('app', 'Visit Start Date'), |
||
| 145 | 'form_fill_time' => Yii::t('app', 'Form Fill Time'), |
||
| 146 | 'spam' => Yii::t('app', 'Spam Info'), |
||
| 147 | 'is_deleted' => Yii::t('app', 'Is deleted'), |
||
| 148 | 'sending_status' => Yii::t('app', 'Sending status'), |
||
| 149 | ]; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function search($params, $form_id = null, $show_deleted = 0) |
||
| 153 | { |
||
| 154 | /* @var $query \yii\db\ActiveQuery */ |
||
| 155 | $query = self::find(); |
||
| 156 | if ($form_id != null) { |
||
| 157 | $query->andWhere('form_id = :form_id', [':form_id' => $form_id]); |
||
| 158 | } |
||
| 159 | |||
| 160 | $query->andFilterWhere(['is_deleted' => $show_deleted]); |
||
| 161 | |||
| 162 | $dataProvider = new ActiveDataProvider( |
||
| 163 | [ |
||
| 164 | 'query' => $query, |
||
| 165 | 'pagination' => [ |
||
| 166 | 'pageSize' => 10, |
||
| 167 | ], |
||
| 168 | ] |
||
| 169 | ); |
||
| 170 | if (!($this->load($params))) { |
||
| 171 | return $dataProvider; |
||
| 172 | } |
||
| 173 | $query->andFilterWhere(['id' => $this->id]); |
||
| 174 | $query->andFilterWhere(['like', 'ip', $this->ip]); |
||
| 175 | $query->andFilterWhere(['like', 'user_agent', $this->user_agent]); |
||
| 176 | return $dataProvider; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getForm() |
||
| 183 | |||
| 184 | View Code Duplication | public static function getStatuses() |
|
| 185 | { |
||
| 186 | return [ |
||
| 187 | self::STATUS_NEW => Yii::t('app', 'New'), |
||
| 188 | self::STATUS_SUCCESS => Yii::t('app', 'Success'), |
||
| 194 | } |
||
| 195 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.