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 | class GoogleMapField extends \FormField { |
||
|
|
|||
| 10 | |||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | * @config |
||
| 14 | */ |
||
| 15 | private static $allowed_actions = [ |
||
| 16 | 'updateMarkers', |
||
| 17 | 'deleteMarkers', |
||
| 18 | 'zoomChanged', |
||
| 19 | 'coordinatesChanged', |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Google maps api key |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | * @config |
||
| 27 | */ |
||
| 28 | private static $api = ''; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var SS_List |
||
| 32 | * @config |
||
| 33 | */ |
||
| 34 | protected $markers = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | * @config |
||
| 39 | */ |
||
| 40 | private static $options = [ |
||
| 41 | 'map' => [ |
||
| 42 | 'center' => [ |
||
| 43 | 'lat' => 55.1309504, |
||
| 44 | 'lng' => 24.5499231, |
||
| 45 | ], |
||
| 46 | ], |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var MapBlock |
||
| 51 | */ |
||
| 52 | protected $block; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * GoogleMapField constructor. |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * @param MapBlock $block |
||
| 59 | * @param \SS_List|null $markers |
||
| 60 | * @param array $options |
||
| 61 | */ |
||
| 62 | public function __construct($name, MapBlock $block, \SS_List $markers = null, $options = []) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getApi() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $api |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | public function setApi($api) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param array $options |
||
| 93 | * |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | public function setOptions($options = []) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getTranslations() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function getOptions() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $data |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | private static function array_keys_to_objects(array $data = []) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param SS_List $markers |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function setMarkers(SS_List $markers) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return bool|string |
||
| 169 | */ |
||
| 170 | public function getMarkers() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Action to delete markers. Should be triggered InstanceId of marker. |
||
| 190 | * |
||
| 191 | * @param \SS_HTTPRequest $request |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function deleteMarkers(\SS_HTTPRequest $request) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Action to update markers. Should be triggered InstanceId and other fields which has @see Marker has. |
||
| 206 | * |
||
| 207 | * @param \SS_HTTPRequest $request |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function updateMarkers(\SS_HTTPRequest $request) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Action to update zoom in/out changes. |
||
| 240 | * |
||
| 241 | * @param \SS_HTTPRequest $request |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function zoomChanged(\SS_HTTPRequest $request) { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Action to update coordinates changes. |
||
| 257 | * |
||
| 258 | * @param \SS_HTTPRequest $request |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function coordinatesChanged(\SS_HTTPRequest $request) { |
|
| 270 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.