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 |
||
| 9 | View Code Duplication | class CartRequest extends Model |
|
|
|
|||
| 10 | { |
||
| 11 | |||
| 12 | protected $currency = null; |
||
| 13 | |||
| 14 | protected $items = null; |
||
| 15 | |||
| 16 | protected $delivery = null; |
||
| 17 | |||
| 18 | protected $mappingClasses = [ |
||
| 19 | 'items' => 'Yandex\Market\Partner\Models\Items', |
||
| 20 | 'delivery' => 'Yandex\Market\Partner\Models\Delivery' |
||
| 21 | ]; |
||
| 22 | |||
| 23 | protected $propNameMap = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Retrieve the currency property |
||
| 27 | * |
||
| 28 | * @return string|null |
||
| 29 | */ |
||
| 30 | public function getCurrency() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Set the currency property |
||
| 37 | * |
||
| 38 | * @param string $currency |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | public function setCurrency($currency) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Retrieve the items property |
||
| 49 | * |
||
| 50 | * @return Items|null |
||
| 51 | */ |
||
| 52 | public function getItems() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set the items property |
||
| 59 | * |
||
| 60 | * @param Items $items |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function setItems($items) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Retrieve the delivery property |
||
| 71 | * |
||
| 72 | * @return Delivery|null |
||
| 73 | */ |
||
| 74 | public function getDelivery() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the delivery property |
||
| 81 | * |
||
| 82 | * @param Delivery $delivery |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function setDelivery($delivery) |
||
| 90 | } |
||
| 91 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.