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 |
||
| 12 | abstract class AbstractPayment extends Widget |
||
| 13 | { |
||
| 14 | /** @property Order $order*/ |
||
| 15 | public $order; |
||
| 16 | /** @property OrderTransaction $transaction*/ |
||
| 17 | public $transaction; |
||
| 18 | |||
| 19 | public function __construct($config = []) |
||
| 20 | { |
||
| 21 | parent::__construct($config); |
||
| 22 | |||
| 23 | if (!$this->transaction instanceof OrderTransaction) { |
||
| 24 | $this->transaction = new OrderTransaction(); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | abstract public function content(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return mixed |
||
| 35 | */ |
||
| 36 | abstract public function checkResult($hash = ''); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return string|null |
||
| 40 | */ |
||
| 41 | public function customCheck() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param bool $success |
||
| 48 | * @param integer|null $orderId |
||
| 49 | */ |
||
| 50 | // protected function redirect($success, $orderId = null) |
||
| 51 | // { |
||
| 52 | // $url = ['/shop/payment/' . ($success ? 'success' : 'error')]; |
||
| 53 | // if (!is_null($orderId)) { |
||
| 54 | // $url['id'] = $orderId; |
||
| 55 | // } |
||
| 56 | // \Yii::$app->response->redirect($url); |
||
| 57 | // } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param integer $id |
||
| 61 | * @return OrderTransaction |
||
| 62 | * @throws \yii\web\NotFoundHttpException |
||
| 63 | */ |
||
| 64 | protected function loadTransaction($id) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param array $params |
||
| 74 | * @param bool|true $scheme |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | protected function createResultUrl(array $params = [], $scheme = true) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $params |
||
| 89 | * @param bool|true $scheme |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | View Code Duplication | protected function createErrorUrl(array $params = [], $scheme = true) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param array $params |
||
| 106 | * @param bool|true $scheme |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | View Code Duplication | protected function createSuccessUrl(array $params = [], $scheme = true) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $params |
||
| 122 | * @param bool|true $scheme |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | View Code Duplication | protected function createFailUrl(array $params = [], $scheme = true) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param array $params |
||
| 139 | * @param bool|true $scheme |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | View Code Duplication | protected function createCancelUrl(array $params = [], $scheme = true) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param PaymentType $type |
||
| 155 | * @param array $params |
||
| 156 | * @param bool|true $scheme |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | protected function createCustomCheckUrl(PaymentType $type, array $params = [], $scheme = true) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $url |
||
| 171 | * @return \yii\web\Response |
||
| 172 | */ |
||
| 173 | protected function redirect($url = '') |
||
| 177 | } |
||
| 178 |