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 |
||
| 12 | class Clusterer |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Bounds |
||
| 16 | */ |
||
| 17 | protected $bounds; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Amount of coordinates in one cell needed to start clustering. |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $minLocations = 1; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $numberOfClusters = 50; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Cluster[][] |
||
| 33 | */ |
||
| 34 | protected $clusters = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Coordinate[][][] |
||
| 38 | */ |
||
| 39 | protected $coordinates = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $coefficientLat = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $coefficientLng = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $spanBoundsLat = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $spanBoundsLng = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $saveCoordinates = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param Bounds $bounds |
||
| 68 | */ |
||
| 69 | public function __construct(Bounds $bounds) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Enables coordinate saving in clusters. |
||
| 81 | * |
||
| 82 | * Note that while it allows you to retrieve all Coordinate objects |
||
| 83 | * in a cluster, this does not scale. At some point, if you keep |
||
| 84 | * adding coordinates into clusters, you'll run out of memory |
||
| 85 | * because we're saving all those coordinates. |
||
| 86 | * If you don't need the exact information of coordinates in a |
||
| 87 | * cluster, leave this disabled. |
||
| 88 | * |
||
| 89 | * @param bool $save |
||
| 90 | * @throws Exception |
||
| 91 | */ |
||
| 92 | public function setSaveCoordinates($save) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set the minimum amount of locations before clustering. |
||
| 103 | * |
||
| 104 | * @param int $limit |
||
| 105 | */ |
||
| 106 | public function setMinClusterLocations($limit) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set an approximate amount of clusters. |
||
| 113 | * Approximate in that it also depends on the viewport: |
||
| 114 | * less square = less clusters. |
||
| 115 | * |
||
| 116 | * @param int $number |
||
| 117 | */ |
||
| 118 | public function setNumberOfClusters($number) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Coordinate $coordinate |
||
| 125 | */ |
||
| 126 | public function addCoordinate(Coordinate $coordinate) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return Coordinate[] |
||
| 160 | */ |
||
| 161 | public function getCoordinates() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return Cluster[] |
||
| 171 | */ |
||
| 172 | public function getClusters() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Based on given bounds, determine matrix size/structure. |
||
| 180 | */ |
||
| 181 | protected function createMatrix() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Find the lat & lng indices of the matrix cell |
||
| 200 | * the given coordinate fits into. |
||
| 201 | * |
||
| 202 | * @param Coordinate $coordinate |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function findCell(Coordinate $coordinate) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * "Fix" coordinates - when leaping from east 360 to west -359, increase |
||
| 217 | * the west coordinated by 360 to make calculating easier. |
||
| 218 | * |
||
| 219 | * @param Coordinate $coordinate |
||
| 220 | * @return Coordinate |
||
| 221 | */ |
||
| 222 | protected function fixCoordinates(Coordinate $coordinate) |
||
| 237 | /** |
||
| 238 | * North and east coordinates can actually be lower than south & west. |
||
| 239 | * This will happen when the left side of a map is displaying east and |
||
| 240 | * the right side is displaying west. At the center of the map, we'll |
||
| 241 | * suddenly have coordinates jumping from 360 to -359. |
||
| 242 | * To make calculating things easier, we'll just increase the west |
||
| 243 | * (= negative) coordinates by 360, and consider those to now be east |
||
| 244 | * (and east as west). Now, coordinates will go from 360 to 361. |
||
| 245 | * |
||
| 246 | * @param Bounds $bounds |
||
| 247 | * @return Bounds |
||
| 248 | */ |
||
| 249 | protected function fixBounds(Bounds $bounds) |
||
| 277 | } |
||
| 278 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.