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 |
||
| 14 | class AnalyticModel |
||
| 15 | { |
||
| 16 | protected $collection; |
||
| 17 | protected $route; |
||
| 18 | protected $aggregate; |
||
| 19 | protected $schema; |
||
| 20 | protected $type; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * String collection |
||
| 24 | * @return mixed |
||
| 25 | */ |
||
| 26 | public function getCollection() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Set value of collection |
||
| 33 | * @param mixed $collection string name |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function setCollection($collection) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Route path |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public function getRoute() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set path |
||
| 52 | * @param mixed $route string route |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function setRoute($route) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Mongodb Aggregates |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | public function getAggregate() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set mongodb query |
||
| 71 | * @param mixed $aggregate object type for query data |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function setAggregate($aggregate) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Schema for response |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | public function getSchema() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Schema data |
||
| 90 | * @param mixed $schema object schema |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function setSchema($schema) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Type of response data |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function getType() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Type for representation |
||
| 109 | * @param mixed $type string view |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function setType($type) |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Build query pipeline for aggregate mongo |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getPipeline() |
||
| 144 | } |
||
| 145 |
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.