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 |
||
| 23 | View Code Duplication | class Coordinates extends AbstractEntity implements CoordinatesInterface |
|
|
|
|||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The GeoJSON type |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | * @ODM\Field(type="string") |
||
| 30 | */ |
||
| 31 | protected $type; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The GeoJSON coordinates. |
||
| 35 | * |
||
| 36 | * This is an array which format depends on the $type beeing used. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | * @ODM\Collection |
||
| 40 | */ |
||
| 41 | protected $coordinates; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param $type |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | public function setType($type) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getType() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param array $coordinates |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function setCoordinates(array $coordinates) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getCoordinates() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function toJson() |
||
| 95 | } |
||
| 96 |
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.