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 |
||
| 24 | View Code Duplication | class Location extends AbstractEntity implements LocationInterface |
|
|
|
|||
| 25 | { |
||
| 26 | /** |
||
| 27 | * city name of a job location |
||
| 28 | * |
||
| 29 | * @ODM\Field(type="string") |
||
| 30 | */ |
||
| 31 | protected $city; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * region of a job location. E.g "Hessen" is a region in germany |
||
| 35 | * |
||
| 36 | * @ODM\Field(type="string") |
||
| 37 | */ |
||
| 38 | protected $region; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * postalcode of a job location. |
||
| 42 | * |
||
| 43 | * @var String |
||
| 44 | * @ODM\Field(type="string") |
||
| 45 | */ |
||
| 46 | protected $postalcode; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * coordinates of a job location. |
||
| 50 | * |
||
| 51 | * @var GeoJson |
||
| 52 | * @ODM\EmbedOne(discriminatorField="_entity") |
||
| 53 | */ |
||
| 54 | protected $coordinates; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Country of a job location |
||
| 58 | * @var String |
||
| 59 | * |
||
| 60 | * @ODM\Field(type="string") |
||
| 61 | */ |
||
| 62 | protected $country; |
||
| 63 | |||
| 64 | public function __construct() |
||
| 67 | |||
| 68 | public function preUpdate() |
||
| 71 | |||
| 72 | public function getCoordinates() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param GeoJson $coordinates |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function setCoordinates(GeoJson $coordinates) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return String |
||
| 90 | */ |
||
| 91 | public function getPostalcode() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $postalcode |
||
| 98 | * |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function setPostalcode($postalcode) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | public function getCity() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $city |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setCity($city) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return String |
||
| 128 | */ |
||
| 129 | public function getCountry() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $country |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function setCountry($country) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function getRegion() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $region |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setRegion($region) |
||
| 163 | } |
||
| 164 |
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.