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 |
||
| 21 | class GeocoderAddressRequest extends AbstractGeocoderRequest |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $address; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var mixed[] |
||
| 30 | */ |
||
| 31 | private $components = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Bound|null |
||
| 35 | */ |
||
| 36 | private $bound; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string|null |
||
| 40 | */ |
||
| 41 | private $region; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string $address |
||
| 45 | */ |
||
| 46 | public function __construct($address) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function getAddress() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $address |
||
| 61 | */ |
||
| 62 | public function setAddress($address) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function hasComponents() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return mixed[] |
||
| 77 | */ |
||
| 78 | public function getComponents() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param mixed[] $components |
||
| 85 | */ |
||
| 86 | public function setComponents(array $components) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param mixed[] $components |
||
| 94 | */ |
||
| 95 | public function addComponents(array $components) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $type |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function hasComponent($type) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $type |
||
| 114 | * |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function getComponent($type) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $type |
||
| 124 | * @param mixed $value |
||
| 125 | */ |
||
| 126 | public function setComponent($type, $value) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $type |
||
| 133 | */ |
||
| 134 | public function removeComponent($type) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function hasBound() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return Bound|null |
||
| 149 | */ |
||
| 150 | public function getBound() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Bound|null $bound |
||
| 157 | */ |
||
| 158 | public function setBound(Bound $bound = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function hasRegion() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return string|null |
||
| 173 | */ |
||
| 174 | public function getRegion() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string|null $region |
||
| 181 | */ |
||
| 182 | public function setRegion($region = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function buildQuery() |
||
| 213 | } |
||
| 214 |
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.