Complex classes like Polygon often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Polygon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Polygon implements PolygonInterface, \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable |
||
| 25 | { |
||
| 26 | const TYPE = 'POLYGON'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var CoordinateCollection |
||
| 30 | */ |
||
| 31 | private $coordinates; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var BoundingBoxInterface |
||
| 35 | */ |
||
| 36 | private $boundingBox; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var boolean |
||
| 40 | */ |
||
| 41 | private $hasCoordinate = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var integer |
||
| 45 | */ |
||
| 46 | private $precision = 8; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param null|array|CoordinateCollection $coordinates |
||
| 50 | */ |
||
| 51 | 34 | public function __construct($coordinates = null) |
|
| 52 | { |
||
| 53 | 34 | if (is_array($coordinates) || null === $coordinates) { |
|
| 54 | 21 | $this->coordinates = new CoordinateCollection; |
|
| 55 | 17 | } elseif ($coordinates instanceof CoordinateCollection) { |
|
| 56 | 15 | $this->coordinates = $coordinates; |
|
| 57 | 15 | $this->hasCoordinate = $coordinates->count() > 0; |
|
| 58 | } else { |
||
| 59 | 2 | throw new \InvalidArgumentException; |
|
| 60 | } |
||
| 61 | |||
| 62 | 34 | $this->boundingBox = new BoundingBox($this); |
|
| 63 | |||
| 64 | 34 | if (is_array($coordinates)) { |
|
| 65 | 3 | $this->set($coordinates); |
|
| 66 | } |
||
| 67 | 34 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function getGeometryType() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return Ellipsoid |
||
| 79 | */ |
||
| 80 | 17 | public function getEllipsoid() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return Coordinate |
||
| 87 | */ |
||
| 88 | 1 | public function getCoordinate() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | 8 | public function isEmpty() |
|
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * @param CoordinateInterface $coordinate |
||
| 104 | * @return boolean |
||
| 105 | */ |
||
| 106 | 4 | public function pointInPolygon(CoordinateInterface $coordinate) |
|
| 107 | { |
||
| 108 | 4 | if (!$this->hasCoordinate) { |
|
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | 4 | if (!$this->boundingBox->pointInBoundingBox($coordinate)) { |
|
| 113 | 2 | return false; |
|
| 114 | } |
||
| 115 | |||
| 116 | 2 | if ($this->pointOnVertex($coordinate)) { |
|
| 117 | return true; |
||
| 118 | } |
||
| 119 | |||
| 120 | 2 | if ($this->pointOnBoundary($coordinate)) { |
|
| 121 | return true; |
||
| 122 | } |
||
| 123 | |||
| 124 | 2 | $total = $this->count(); |
|
| 125 | 2 | $intersections = 0; |
|
| 126 | 2 | for ($i = 1; $i < $total; $i++) { |
|
| 127 | 2 | $currentVertex = $this->get($i - 1); |
|
| 128 | 2 | $nextVertex = $this->get($i); |
|
| 129 | |||
| 130 | 2 | if (bccomp( |
|
| 131 | 2 | $coordinate->getLatitude(), |
|
| 132 | 2 | min($currentVertex->getLatitude(), $nextVertex->getLatitude()), |
|
| 133 | 2 | $this->getPrecision() |
|
| 134 | 2 | ) === 1 && |
|
| 135 | 2 | bccomp( |
|
| 136 | 2 | $coordinate->getLatitude(), |
|
| 137 | 2 | max($currentVertex->getLatitude(), $nextVertex->getLatitude()), |
|
| 138 | 2 | $this->getPrecision() |
|
| 139 | 2 | ) <= 0 && |
|
| 140 | 2 | bccomp( |
|
| 141 | 2 | $coordinate->getLongitude(), |
|
| 142 | 2 | max($currentVertex->getLongitude(), $nextVertex->getLongitude()), |
|
| 143 | 2 | $this->getPrecision() |
|
| 144 | 2 | ) <= 0 && |
|
| 145 | 2 | bccomp( |
|
| 146 | 2 | $currentVertex->getLatitude(), |
|
| 147 | 2 | $nextVertex->getLatitude(), |
|
| 148 | 2 | $this->getPrecision() |
|
| 149 | 2 | ) !== 0 |
|
| 150 | ) { |
||
| 151 | $xinters = |
||
| 152 | 2 | ($coordinate->getLatitude() - $currentVertex->getLatitude()) * |
|
| 153 | 2 | ($nextVertex->getLongitude() - $currentVertex->getLongitude()) / |
|
| 154 | 2 | ($nextVertex->getLatitude() - $currentVertex->getLatitude()) + |
|
| 155 | 2 | $currentVertex->getLongitude(); |
|
| 156 | |||
| 157 | 2 | if (bccomp( |
|
| 158 | 2 | $currentVertex->getLongitude(), |
|
| 159 | 2 | $nextVertex->getLongitude(), |
|
| 160 | 2 | $this->getPrecision() |
|
| 161 | 2 | ) === 0 || |
|
| 162 | 2 | bccomp( |
|
| 163 | 2 | $coordinate->getLongitude(), |
|
| 164 | 2 | $xinters, |
|
| 165 | 2 | $this->getPrecision() |
|
| 166 | 2 | ) <= 0 |
|
| 167 | ) { |
||
| 168 | 2 | $intersections++; |
|
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | 2 | if ($intersections % 2 != 0) { |
|
| 174 | 2 | return true; |
|
| 175 | } |
||
| 176 | |||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param CoordinateInterface $coordinate |
||
| 182 | * @return boolean |
||
| 183 | */ |
||
| 184 | 6 | public function pointOnBoundary(CoordinateInterface $coordinate) |
|
| 185 | { |
||
| 186 | 6 | $total = $this->count(); |
|
| 187 | 6 | for ($i = 1; $i <= $total; $i++) { |
|
| 188 | 6 | $currentVertex = $this->get($i - 1); |
|
| 189 | 6 | $nextVertex = $this->get($i); |
|
| 190 | |||
| 191 | 6 | if (null === $nextVertex) { |
|
| 192 | 6 | $nextVertex = $this->get(0); |
|
| 193 | } |
||
| 194 | |||
| 195 | // Check if coordinate is on a horizontal boundary |
||
| 196 | 6 | if (bccomp( |
|
| 197 | 6 | $currentVertex->getLatitude(), |
|
| 198 | 6 | $nextVertex->getLatitude(), |
|
| 199 | 6 | $this->getPrecision() |
|
| 200 | 6 | ) === 0 && |
|
| 201 | bccomp( |
||
| 202 | $currentVertex->getLatitude(), |
||
| 203 | $coordinate->getLatitude(), |
||
| 204 | $this->getPrecision() |
||
| 205 | 6 | ) === 0 && |
|
| 206 | bccomp( |
||
| 207 | $coordinate->getLongitude(), |
||
| 208 | min($currentVertex->getLongitude(), $nextVertex->getLongitude()), |
||
| 209 | $this->getPrecision() |
||
| 210 | 6 | ) === 1 && |
|
| 211 | bccomp( |
||
| 212 | $coordinate->getLongitude(), |
||
| 213 | max($currentVertex->getLongitude(), $nextVertex->getLongitude()), |
||
| 214 | $this->getPrecision() |
||
| 215 | 6 | ) === -1 |
|
| 216 | ) { |
||
| 217 | return true; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Check if coordinate is on a boundary |
||
| 221 | 6 | if (bccomp( |
|
| 222 | 6 | $coordinate->getLatitude(), |
|
| 223 | 6 | min($currentVertex->getLatitude(), $nextVertex->getLatitude()), |
|
| 224 | 6 | $this->getPrecision() |
|
| 225 | 6 | ) === 1 && |
|
| 226 | 4 | bccomp( |
|
| 227 | 4 | $coordinate->getLatitude(), |
|
| 228 | 4 | max($currentVertex->getLatitude(), $nextVertex->getLatitude()), |
|
| 229 | 4 | $this->getPrecision() |
|
| 230 | 6 | ) <= 0 && |
|
| 231 | 4 | bccomp( |
|
| 232 | 4 | $coordinate->getLongitude(), |
|
| 233 | 4 | max($currentVertex->getLongitude(), $nextVertex->getLongitude()), |
|
| 234 | 4 | $this->getPrecision() |
|
| 235 | 6 | ) <= 0 && |
|
| 236 | 4 | bccomp( |
|
| 237 | 4 | $currentVertex->getLatitude(), |
|
| 238 | 4 | $nextVertex->getLatitude(), |
|
| 239 | 4 | $this->getPrecision() |
|
| 240 | 6 | ) !== 0 |
|
| 241 | ) { |
||
| 242 | $xinters = |
||
| 243 | 4 | ($coordinate->getLatitude() - $currentVertex->getLatitude()) * |
|
| 244 | 4 | ($nextVertex->getLongitude() - $currentVertex->getLongitude()) / |
|
| 245 | 4 | ($nextVertex->getLatitude() - $currentVertex->getLatitude()) + |
|
| 246 | 4 | $currentVertex->getLongitude(); |
|
| 247 | |||
| 248 | 4 | if (bccomp($xinters, $coordinate->getLongitude(), $this->getPrecision()) === 0) { |
|
| 249 | 2 | return true; |
|
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | 4 | return false; |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param CoordinateInterface $coordinate |
||
| 259 | * @return boolean |
||
| 260 | */ |
||
| 261 | 6 | public function pointOnVertex(CoordinateInterface $coordinate) |
|
| 262 | { |
||
| 263 | 6 | foreach ($this->coordinates as $vertexCoordinate) { |
|
| 264 | 6 | if (bccomp( |
|
| 265 | 6 | $vertexCoordinate->getLatitude(), |
|
| 266 | 6 | $coordinate->getLatitude(), |
|
| 267 | 6 | $this->getPrecision() |
|
| 268 | 6 | ) === 0 && |
|
| 269 | 4 | bccomp( |
|
| 270 | 4 | $vertexCoordinate->getLongitude(), |
|
| 271 | 4 | $coordinate->getLongitude(), |
|
| 272 | 4 | $this->getPrecision() |
|
| 273 | 6 | ) === 0 |
|
| 274 | ) { |
||
| 275 | 6 | return true; |
|
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | 4 | return false; |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritDoc} |
||
| 284 | */ |
||
| 285 | 34 | public function getCoordinates() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritDoc} |
||
| 292 | */ |
||
| 293 | public function setCoordinates(CoordinateCollection $coordinates) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function toArray() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritDoc} |
||
| 311 | */ |
||
| 312 | public function jsonSerialize() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritDoc} |
||
| 319 | */ |
||
| 320 | public function offsetExists($offset) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritDoc} |
||
| 327 | */ |
||
| 328 | public function offsetGet($offset) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritDoc} |
||
| 335 | */ |
||
| 336 | public function offsetSet($offset, $value) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritDoc} |
||
| 344 | */ |
||
| 345 | public function offsetUnset($offset) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * {@inheritDoc} |
||
| 354 | */ |
||
| 355 | 7 | public function count() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritDoc} |
||
| 362 | */ |
||
| 363 | public function getIterator() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritDoc} |
||
| 370 | */ |
||
| 371 | 7 | public function get($key) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritDoc} |
||
| 378 | */ |
||
| 379 | 16 | public function set($key, CoordinateInterface $coordinate = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritDoc} |
||
| 402 | */ |
||
| 403 | 1 | public function add(CoordinateInterface $coordinate) |
|
| 404 | { |
||
| 405 | 1 | $retval = $this->coordinates->add($coordinate); |
|
| 406 | |||
| 407 | 1 | $this->hasCoordinate = true; |
|
| 408 | 1 | $this->boundingBox->setPolygon($this); |
|
| 409 | |||
| 410 | 1 | return $retval; |
|
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritDoc} |
||
| 415 | */ |
||
| 416 | public function remove($key) |
||
| 417 | { |
||
| 418 | $retval = $this->coordinates->remove($key); |
||
| 419 | |||
| 420 | if (!count($this->coordinates)) { |
||
| 421 | $this->hasCoordinate = false; |
||
| 422 | } |
||
| 423 | $this->boundingBox->setPolygon($this); |
||
| 424 | |||
| 425 | return $retval; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return integer |
||
| 430 | */ |
||
| 431 | 22 | public function getPrecision() |
|
| 432 | { |
||
| 433 | 22 | return $this->precision; |
|
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param integer $precision |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | 1 | public function setPrecision($precision) |
|
| 441 | { |
||
| 442 | 1 | $this->boundingBox->setPrecision($precision); |
|
| 443 | 1 | $this->precision = $precision; |
|
| 444 | |||
| 445 | 1 | return $this; |
|
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return BoundingBoxInterface |
||
| 450 | */ |
||
| 451 | 2 | public function getBoundingBox() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @param BoundingBoxInterface $boundingBox |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | public function setBoundingBox(BoundingBoxInterface $boundingBox) |
||
| 466 | } |
||
| 467 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.