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 |
||
| 23 | class Polygon implements PolygonInterface, \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var CoordinateCollection|CoordinateInterface[] |
||
| 27 | */ |
||
| 28 | private $coordinates; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var BoundingBoxInterface |
||
| 32 | */ |
||
| 33 | private $boundingBox; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | private $hasCoordinate = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var integer |
||
| 42 | */ |
||
| 43 | private $precision = 8; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param null|array|CoordinateCollection $coordinates |
||
| 47 | */ |
||
| 48 | 15 | public function __construct($coordinates = null) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param CoordinateInterface $coordinate |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | 2 | public function pointInPolygon(CoordinateInterface $coordinate) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param CoordinateInterface $coordinate |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 3 | public function pointOnBoundary(CoordinateInterface $coordinate) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param CoordinateInterface $coordinate |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | 3 | public function pointOnVertex(CoordinateInterface $coordinate) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritDoc} |
||
| 252 | */ |
||
| 253 | 15 | public function getCoordinates() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritDoc} |
||
| 260 | */ |
||
| 261 | public function setCoordinates(CoordinateCollection $coordinates) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | public function toArray() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritDoc} |
||
| 279 | */ |
||
| 280 | public function jsonSerialize() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritDoc} |
||
| 287 | */ |
||
| 288 | public function offsetExists($offset) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritDoc} |
||
| 295 | */ |
||
| 296 | public function offsetGet($offset) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritDoc} |
||
| 303 | */ |
||
| 304 | public function offsetSet($offset, $value) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritDoc} |
||
| 312 | */ |
||
| 313 | public function offsetUnset($offset) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritDoc} |
||
| 322 | */ |
||
| 323 | 4 | public function count() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritDoc} |
||
| 330 | */ |
||
| 331 | public function getIterator() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritDoc} |
||
| 338 | */ |
||
| 339 | 4 | public function get($key) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritDoc} |
||
| 346 | */ |
||
| 347 | 8 | public function set($key, CoordinateInterface $coordinate = null) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritDoc} |
||
| 370 | */ |
||
| 371 | 1 | public function add(CoordinateInterface $coordinate) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritDoc} |
||
| 383 | */ |
||
| 384 | public function remove($key) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return integer |
||
| 398 | */ |
||
| 399 | 5 | public function getPrecision() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @param integer $precision |
||
| 406 | * @return $this |
||
| 407 | */ |
||
| 408 | public function setPrecision($precision) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return BoundingBoxInterface |
||
| 418 | */ |
||
| 419 | 1 | public function getBoundingBox() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param BoundingBoxInterface $boundingBox |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function setBoundingBox(BoundingBoxInterface $boundingBox) |
||
| 434 | } |
||
| 435 |