| Total Complexity | 72 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like KML 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.
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 KML, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class KML implements GeoAdapter |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \DOMDocument |
||
| 31 | */ |
||
| 32 | protected $xmlObject; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string Name-space string. eg 'georss:' |
||
| 36 | */ |
||
| 37 | private $nss = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Read KML string into geometry objects |
||
| 41 | * |
||
| 42 | * @param string $kml A KML string |
||
| 43 | * @return Geometry|GeometryCollection |
||
| 44 | */ |
||
| 45 | public function read(string $kml): Geometry |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $text |
||
| 52 | * @return Geometry|GeometryCollection |
||
| 53 | * @throws \Exception |
||
| 54 | */ |
||
| 55 | public function geomFromText(string $text): Geometry |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return Geometry|GeometryCollection |
||
| 79 | */ |
||
| 80 | protected function geomFromXML(): Geometry |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param \DOMNode $xml |
||
| 127 | * @param string $nodeName |
||
| 128 | * @return \DOMNode[] |
||
| 129 | */ |
||
| 130 | protected function childElements(\DOMNode $xml, string $nodeName = ''): array |
||
| 131 | { |
||
| 132 | $children = []; |
||
| 133 | foreach ($xml->childNodes as $child) { |
||
| 134 | if ($child->nodeName == $nodeName) { |
||
| 135 | $children[] = $child; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $children; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param \DOMNode $xml |
||
| 144 | * @return Point |
||
| 145 | */ |
||
| 146 | protected function parsePoint(\DOMNode $xml): Point |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param \DOMNode $xml |
||
| 164 | * @return LineString |
||
| 165 | */ |
||
| 166 | protected function parseLineString(\DOMNode $xml): LineString |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param \DOMNode $xml |
||
| 197 | * @return Polygon |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | protected function parsePolygon(\DOMNode $xml): Polygon |
||
| 201 | { |
||
| 202 | $components = []; |
||
| 203 | |||
| 204 | /** @noinspection SpellCheckingInspection */ |
||
| 205 | $outerBoundaryIs = $this->childElements($xml, 'outerboundaryis'); |
||
| 206 | if (empty($outerBoundaryIs)) { |
||
| 207 | return new Polygon(); |
||
| 208 | } |
||
| 209 | $outerBoundaryElement = $outerBoundaryIs[0]; |
||
| 210 | /** @noinspection SpellCheckingInspection */ |
||
| 211 | $outerRingElement = @$this->childElements($outerBoundaryElement, 'linearring')[0]; |
||
| 212 | $components[] = $this->parseLineString($outerRingElement); |
||
| 213 | |||
| 214 | if (count($components) != 1) { |
||
| 215 | throw new \Exception("Invalid KML"); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** @noinspection SpellCheckingInspection */ |
||
| 219 | $innerBoundaryElementIs = $this->childElements($xml, 'innerboundaryis'); |
||
| 220 | foreach ($innerBoundaryElementIs as $innerBoundaryElement) { |
||
| 221 | /** @noinspection SpellCheckingInspection */ |
||
| 222 | foreach ($this->childElements($innerBoundaryElement, 'linearring') as $innerRingElement) { |
||
| 223 | $components[] = $this->parseLineString($innerRingElement); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | return new Polygon($components); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param \DOMNode $xml |
||
| 232 | * @return GeometryCollection |
||
| 233 | */ |
||
| 234 | protected function parseGeometryCollection(\DOMNode $xml): GeometryCollection |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param \DOMNode $xml |
||
| 257 | * @return array<array> |
||
| 258 | */ |
||
| 259 | protected function extractCoordinates(\DOMNode $xml): array |
||
| 260 | { |
||
| 261 | $coordinateElements = $this->childElements($xml, 'coordinates'); |
||
| 262 | $coordinates = []; |
||
| 263 | |||
| 264 | if (!empty($coordinateElements)) { |
||
| 265 | $coordinateSets = explode(' ', preg_replace('/[\r\n\s\t]+/', ' ', $coordinateElements[0]->nodeValue)); |
||
| 266 | |||
| 267 | foreach ($coordinateSets as $setString) { |
||
| 268 | $setString = trim($setString); |
||
| 269 | if ($setString) { |
||
| 270 | $setArray = explode(',', $setString); |
||
| 271 | if (count($setArray) >= 2) { |
||
| 272 | $coordinates[] = $setArray; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | return $coordinates; |
||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Serialize geometries into a KML string. |
||
| 284 | * |
||
| 285 | * @param Geometry $geometry |
||
| 286 | * @param string $namespace |
||
| 287 | * @return string The KML string representation of the input geometries |
||
| 288 | */ |
||
| 289 | public function write(Geometry $geometry, string $namespace = ''): string |
||
| 290 | { |
||
| 291 | $namespace = trim($namespace); |
||
| 292 | if (!empty($namespace)) { |
||
| 293 | $this->nss = $namespace . ':'; |
||
| 294 | } |
||
| 295 | |||
| 296 | return $this->geometryToKML($geometry); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Geometry $geometry |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | private function geometryToKML(Geometry $geometry): string |
||
| 304 | { |
||
| 305 | $type = $geometry->geometryType(); |
||
| 306 | switch ($type) { |
||
| 307 | case Geometry::POINT: |
||
| 308 | /** @var Point $geometry */ |
||
| 309 | return $this->pointToKML($geometry); |
||
| 310 | case Geometry::LINESTRING: |
||
| 311 | /** @var LineString $geometry */ |
||
| 312 | return $this->linestringToKML($geometry); |
||
| 313 | case Geometry::POLYGON: |
||
| 314 | /** @var Polygon $geometry */ |
||
| 315 | return $this->polygonToKML($geometry); |
||
| 316 | case Geometry::MULTI_POINT: |
||
| 317 | case Geometry::MULTI_LINESTRING: |
||
| 318 | case Geometry::MULTI_POLYGON: |
||
| 319 | case Geometry::GEOMETRY_COLLECTION: |
||
| 320 | /** @var Collection $geometry */ |
||
| 321 | return $this->collectionToKML($geometry); |
||
| 322 | } |
||
| 323 | return ''; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param Point $geometry |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function pointToKML(Geometry $geometry): string |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param LineString $geometry |
||
| 343 | * @param string $type |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | private function linestringToKML(Geometry $geometry, $type = null): string |
||
| 347 | { |
||
| 348 | if (!isset($type)) { |
||
| 349 | $type = $geometry->geometryType(); |
||
| 350 | } |
||
| 351 | |||
| 352 | $str = '<' . $this->nss . $type . ">\n"; |
||
| 353 | |||
| 354 | if (!$geometry->isEmpty()) { |
||
| 355 | $str .= '<' . $this->nss . 'coordinates>'; |
||
| 356 | $i = 0; |
||
| 357 | foreach ($geometry->getComponents() as $comp) { |
||
| 358 | if ($i != 0) { |
||
| 359 | $str .= ' '; |
||
| 360 | } |
||
| 361 | $str .= $comp->getX() . ',' . $comp->getY(); |
||
| 362 | $i++; |
||
| 363 | } |
||
| 364 | |||
| 365 | $str .= '</' . $this->nss . 'coordinates>'; |
||
| 366 | } |
||
| 367 | |||
| 368 | $str .= '</' . $this->nss . $type . ">\n"; |
||
| 369 | |||
| 370 | return $str; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param Polygon $geometry |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function polygonToKML(Geometry $geometry): string |
||
| 378 | { |
||
| 379 | /** @var LineString[] $components */ |
||
| 380 | $components = $geometry->getComponents(); |
||
| 381 | $str = ''; |
||
| 382 | if (!empty($components)) { |
||
| 383 | /** @noinspection PhpParamsInspection */ |
||
| 384 | $str = '<' . $this->nss . 'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</' . $this->nss . 'outerBoundaryIs>'; |
||
| 385 | foreach (array_slice($components, 1) as $comp) { |
||
| 386 | $str .= '<' . $this->nss . 'innerBoundaryIs>' . $this->linestringToKML($comp) . '</' . $this->nss . 'innerBoundaryIs>'; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | return '<' . $this->nss . "Polygon>\n" . $str . '</' . $this->nss . "Polygon>\n"; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param Collection $geometry |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function collectionToKML(Geometry $geometry): string |
||
| 407 | } |
||
| 408 | } |
||
| 409 |