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 = 2; |
||
| 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) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set an approximate amount of clusters. |
||
| 114 | * Approximate in that it also depends on the viewport: |
||
| 115 | * less square = less clusters. |
||
| 116 | * |
||
| 117 | * @param int $number |
||
| 118 | */ |
||
| 119 | public function setNumberOfClusters($number) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Coordinate $coordinate |
||
| 126 | */ |
||
| 127 | public function addCoordinate(Coordinate $coordinate) |
||
| 128 | { |
||
| 129 | list($latIndex, $lngIndex) = $this->findCell($coordinate); |
||
| 130 | $coordinateCount = isset($this->coordinates[$latIndex][$lngIndex]) ? count($this->coordinates[$latIndex][$lngIndex]) : 0; |
||
| 131 | |||
| 132 | // cluster already exists, add coordinate to it |
||
| 133 | if (isset($this->clusters[$latIndex][$lngIndex])) { |
||
| 134 | $this->clusters[$latIndex][$lngIndex]->addCoordinate($coordinate, $this->saveCoordinates); |
||
| 135 | |||
| 136 | // there's no cluster yet, but entry limit reached = cluster now, as long as we have more than one location/coordinate |
||
| 137 | } elseif ($coordinateCount >= $this->minLocations - 1 && $coordinateCount > 1) { |
||
| 138 | // initialise cluster with given coordinate |
||
| 139 | $this->clusters[$latIndex][$lngIndex] = new Cluster(); |
||
| 140 | $this->clusters[$latIndex][$lngIndex]->addCoordinate($coordinate, $this->saveCoordinates); |
||
| 141 | |||
| 142 | if ($coordinateCount) { |
||
| 143 | // add existing coordinates |
||
| 144 | foreach ($this->coordinates[$latIndex][$lngIndex] as $coordinate) { |
||
| 145 | $this->clusters[$latIndex][$lngIndex]->addCoordinate($coordinate, $this->saveCoordinates); |
||
| 146 | } |
||
| 147 | |||
| 148 | // save cluster & clear array of individual coordinates (to free up |
||
| 149 | // memory, in case we're dealing with lots of coordinates) |
||
| 150 | unset($this->coordinates[$latIndex][$lngIndex]); |
||
| 151 | } |
||
| 152 | |||
| 153 | // entry limit for clustering not yet reached, save coordinate |
||
| 154 | } else { |
||
| 155 | $this->coordinates[$latIndex][$lngIndex][] = $coordinate; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return Coordinate[] |
||
| 161 | */ |
||
| 162 | public function getCoordinates() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return Cluster[] |
||
| 172 | */ |
||
| 173 | public function getClusters() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Based on given bounds, determine matrix size/structure. |
||
| 181 | */ |
||
| 182 | protected function createMatrix() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Find the lat & lng indices of the matrix cell |
||
| 201 | * the given coordinate fits into. |
||
| 202 | * |
||
| 203 | * @param Coordinate $coordinate |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function findCell(Coordinate $coordinate) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * "Fix" coordinates - when leaping from east 360 to west -359, increase |
||
| 218 | * the west coordinated by 360 to make calculating easier. |
||
| 219 | * |
||
| 220 | * @param Coordinate $coordinate |
||
| 221 | * @return Coordinate |
||
| 222 | */ |
||
| 223 | protected function fixCoordinates(Coordinate $coordinate) |
||
| 238 | /** |
||
| 239 | * North and east coordinates can actually be lower than south & west. |
||
| 240 | * This will happen when the left side of a map is displaying east and |
||
| 241 | * the right side is displaying west. At the center of the map, we'll |
||
| 242 | * suddenly have coordinates jumping from 360 to -359. |
||
| 243 | * To make calculating things easier, we'll just increase the west |
||
| 244 | * (= negative) coordinates by 360, and consider those to now be east |
||
| 245 | * (and east as west). Now, coordinates will go from 360 to 361. |
||
| 246 | * |
||
| 247 | * @param Bounds $bounds |
||
| 248 | * @return Bounds |
||
| 249 | */ |
||
| 250 | protected function fixBounds(Bounds $bounds) |
||
| 278 | } |
||
| 279 |
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.