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 |
||
| 4 | class SimpleOffer extends BaseModel |
||
| 5 | { |
||
| 6 | /** |
||
| 7 | * @inheritdoc |
||
| 8 | */ |
||
| 9 | public static $tag = 'offer'; |
||
| 10 | /** |
||
| 11 | * @inheritdoc |
||
| 12 | */ |
||
| 13 | public static $tagProperties = [ |
||
| 14 | 'id', |
||
| 15 | 'bid', |
||
| 16 | 'cbid', |
||
| 17 | 'available' |
||
| 18 | ]; |
||
| 19 | |||
| 20 | public $id; |
||
| 21 | public $bid; |
||
| 22 | public $cbid; |
||
| 23 | public $available; |
||
| 24 | |||
| 25 | public $url; |
||
| 26 | public $price; |
||
| 27 | public $oldprice; |
||
| 28 | public $currencyId; |
||
| 29 | public $categoryId; |
||
| 30 | public $market_Category; |
||
| 31 | public $store; |
||
| 32 | public $pickup; |
||
| 33 | public $delivery; |
||
| 34 | public $local_Delivery_Cost; |
||
| 35 | public $name; |
||
| 36 | public $vendor; |
||
| 37 | public $vendorCode; |
||
| 38 | public $description; |
||
| 39 | public $sales_Notes; |
||
| 40 | public $manufacturer_Warranty; |
||
| 41 | public $country_Of_Origin; |
||
| 42 | public $adult; |
||
| 43 | public $age; |
||
| 44 | public $barcode; |
||
| 45 | public $cpa; |
||
| 46 | public $params = []; |
||
| 47 | public $pictures = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | */ |
||
| 52 | 6 | public function getYmlAttributes() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | 6 | public function rules() |
|
| 83 | { |
||
| 84 | return [ |
||
| 85 | [ |
||
| 86 | 4 | ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'], |
|
| 87 | 4 | 'required', |
|
| 88 | 6 | ], |
|
| 89 | [ |
||
| 90 | 4 | ['sales_Notes'], |
|
| 91 | 4 | 'string', |
|
| 92 | 4 | 'max' => 50, |
|
| 93 | 4 | ], |
|
| 94 | [ |
||
| 95 | 4 | ['name', 'vendor'], |
|
| 96 | 4 | 'string', |
|
| 97 | 4 | 'max' => 120, |
|
| 98 | 4 | ], |
|
| 99 | [ |
||
| 100 | 4 | ['delivery'], |
|
| 101 | 4 | 'string', |
|
| 102 | 4 | 'max' => 4, |
|
| 103 | 4 | ], |
|
| 104 | [ |
||
| 105 | 4 | ['id', 'categoryId', 'bid', 'cbid'], |
|
| 106 | 4 | 'integer', |
|
| 107 | 4 | ], |
|
| 108 | [ |
||
| 109 | 4 | ['url'], |
|
| 110 | 4 | 'url', |
|
| 111 | 4 | ], |
|
| 112 | [ |
||
| 113 | 4 | ['price', 'oldprice'], |
|
| 114 | 4 | 'double', |
|
| 115 | 4 | ], |
|
| 116 | [ |
||
| 117 | [ |
||
| 118 | 4 | 'currencyId', |
|
| 119 | 4 | ], |
|
| 120 | 4 | 'in', |
|
| 121 | 'range' => [ |
||
| 122 | 4 | 'RUR', |
|
| 123 | 4 | 'UAH', |
|
| 124 | 4 | 'BYR', |
|
| 125 | 4 | 'KZT', |
|
| 126 | 4 | 'USD', |
|
| 127 | 'EUR' |
||
| 128 | 4 | ], |
|
| 129 | 4 | ], |
|
| 130 | [ |
||
| 131 | 4 | ['description'], |
|
| 132 | 4 | 'string', |
|
| 133 | 4 | ], |
|
| 134 | [ |
||
| 135 | 4 | ['pictures'], |
|
| 136 | 6 | function ($attribute, $params) { |
|
| 137 | 6 | if (count($this->pictures) > 10) { |
|
| 138 | $this->addError('pictures', 'maximum 10 pictures'); |
||
| 139 | } |
||
| 140 | 6 | } |
|
| 141 | 4 | ], |
|
| 142 | [ |
||
| 143 | 4 | ['params'], |
|
| 144 | 4 | 'each', |
|
| 145 | 4 | 'rule' => ['string'] |
|
| 146 | 4 | ], |
|
| 147 | [ |
||
| 148 | 4 | ['pictures'], |
|
| 149 | 4 | 'each', |
|
| 150 | 4 | 'rule' => ['url'] |
|
| 151 | 4 | ] |
|
| 152 | 4 | ]; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param array $params |
||
| 157 | */ |
||
| 158 | 6 | public function setParams(array $params) |
|
| 159 | { |
||
| 160 | 6 | $this->params = $params; |
|
| 161 | 6 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param array $pictures |
||
| 165 | */ |
||
| 166 | 6 | public function setPictures(array $pictures) |
|
| 167 | { |
||
| 168 | 6 | $this->pictures = $pictures; |
|
| 169 | 6 | } |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritdoc |
||
| 173 | */ |
||
| 174 | 6 | protected function getYmlBody() |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $attribute |
||
| 196 | * @param string $value |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | View Code Duplication | protected function getYmlParamTag($attribute, $value) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $value |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 6 | View Code Duplication | protected function getYmlPictureTag($value) |
| 224 | } |
||
| 225 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.