| Total Complexity | 82 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like geoPHP 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 geoPHP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class geoPHP |
||
| 19 | { |
||
| 20 | // Earth radius constants in meters |
||
| 21 | |||
| 22 | /** WGS84 semi-major axis (a), aka equatorial radius */ |
||
| 23 | const EARTH_WGS84_SEMI_MAJOR_AXIS = 6378137.0; |
||
| 24 | /** WGS84 semi-minor axis (b), aka polar radius */ |
||
| 25 | const EARTH_WGS84_SEMI_MINOR_AXIS = 6356752.314245; |
||
| 26 | /** WGS84 inverse flattening */ |
||
| 27 | const EARTH_WGS84_FLATTENING = 298.257223563; |
||
| 28 | |||
| 29 | /** WGS84 semi-major axis (a), aka equatorial radius */ |
||
| 30 | const EARTH_GRS80_SEMI_MAJOR_AXIS = 6378137.0; |
||
| 31 | /** GRS80 semi-minor axis */ |
||
| 32 | const EARTH_GRS80_SEMI_MINOR_AXIS = 6356752.314140; |
||
| 33 | /** GRS80 inverse flattening */ |
||
| 34 | const EARTH_GRS80_FLATTENING = 298.257222100882711; |
||
| 35 | |||
| 36 | /** IUGG mean radius R1 = (2a + b) / 3 */ |
||
| 37 | const EARTH_MEAN_RADIUS = 6371008.8; |
||
| 38 | /** IUGG R2: Earth's authalic ("equal area") radius is the radius of a hypothetical perfect sphere |
||
| 39 | * which has the same surface area as the reference ellipsoid. */ |
||
| 40 | const EARTH_AUTHALIC_RADIUS = 6371007.2; |
||
| 41 | |||
| 42 | const CLASS_NAMESPACE = 'geoPHP\\'; |
||
| 43 | |||
| 44 | private static $adapterMap = [ |
||
| 45 | 'wkt' => 'WKT', |
||
| 46 | 'ewkt' => 'EWKT', |
||
| 47 | 'wkb' => 'WKB', |
||
| 48 | 'ewkb' => 'EWKB', |
||
| 49 | 'json' => 'GeoJSON', |
||
| 50 | 'geojson' => 'GeoJSON', |
||
| 51 | 'kml' => 'KML', |
||
| 52 | 'gpx' => 'GPX', |
||
| 53 | 'georss' => 'GeoRSS', |
||
| 54 | 'google_geocode' => 'GoogleGeocode', |
||
| 55 | 'geohash' => 'GeoHash', |
||
| 56 | 'twkb' => 'TWKB', |
||
| 57 | 'osm' => 'OSM', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | public static function getAdapterMap() |
||
| 63 | } |
||
| 64 | |||
| 65 | private static $geometryList = [ |
||
| 66 | 'point' => 'Point', |
||
| 67 | 'linestring' => 'LineString', |
||
| 68 | 'polygon' => 'Polygon', |
||
| 69 | 'multipoint' => 'MultiPoint', |
||
| 70 | 'multilinestring' => 'MultiLineString', |
||
| 71 | 'multipolygon' => 'MultiPolygon', |
||
| 72 | 'geometrycollection' => 'GeometryCollection', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | public static function getGeometryList() |
||
| 76 | { |
||
| 77 | return self::$geometryList; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Converts data to Geometry using geo adapters |
||
| 82 | * |
||
| 83 | * If $data is an array, all passed in values will be combined into a single geometry |
||
| 84 | * |
||
| 85 | * @param mixed $data The data in any supported format, including geoPHP Geometry |
||
| 86 | * @var null|string $type Data type. Tries to detect if omitted |
||
| 87 | * @var mixed|null $otherArgs Arguments will be passed to the geo adapter |
||
| 88 | * |
||
| 89 | * @return Collection|Geometry |
||
| 90 | * @throws \Exception |
||
| 91 | */ |
||
| 92 | public static function load($data) |
||
| 93 | { |
||
| 94 | $args = func_get_args(); |
||
| 95 | |||
| 96 | $data = array_shift($args); |
||
| 97 | $type = count($args) && @array_key_exists($args[0], self::$adapterMap) ? strtolower(array_shift($args)) : null; |
||
| 98 | |||
| 99 | // Auto-detect type if needed |
||
| 100 | if (!$type) { |
||
| 101 | // If the user is trying to load a Geometry from a Geometry... Just pass it back |
||
| 102 | if (is_object($data)) { |
||
| 103 | if ($data instanceof Geometry) { |
||
| 104 | return $data; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $detected = geoPHP::detectFormat($data); |
||
| 109 | if (!$detected) { |
||
| 110 | throw new \Exception("Can not detect format"); |
||
| 111 | } |
||
| 112 | $format = explode(':', $detected); |
||
| 113 | $type = array_shift($format); |
||
| 114 | $args = $format ?: $args; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (!array_key_exists($type, self::$adapterMap)) { |
||
| 118 | throw new \Exception('geoPHP could not find an adapter of type ' . htmlentities($type)); |
||
| 119 | } |
||
| 120 | $adapterType = self::CLASS_NAMESPACE . 'Adapter\\' . self::$adapterMap[$type]; |
||
| 121 | |||
| 122 | $adapter = new $adapterType(); |
||
| 123 | |||
| 124 | // Data is not an array, just pass it normally |
||
| 125 | if (!is_array($data)) { |
||
| 126 | $result = call_user_func_array([$adapter, "read"], array_merge([$data], $args)); |
||
| 127 | } else { // Data is an array, combine all passed in items into a single geometry |
||
| 128 | $geometries = []; |
||
| 129 | foreach ($data as $item) { |
||
| 130 | $geometries[] = call_user_func_array([$adapter, "read"], array_merge($item, $args)); |
||
| 131 | } |
||
| 132 | $result = geoPHP::buildGeometry($geometries); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $result; |
||
| 136 | } |
||
| 137 | |||
| 138 | public static function geosInstalled($force = null) |
||
| 139 | { |
||
| 140 | static $geosInstalled = null; |
||
| 141 | if ($force !== null) { |
||
| 142 | $geosInstalled = $force; |
||
| 143 | } |
||
| 144 | if (getenv('GEOS_DISABLED') == 1) { |
||
| 145 | $geosInstalled = false; |
||
| 146 | } |
||
| 147 | if ($geosInstalled !== null) { |
||
| 148 | return $geosInstalled; |
||
| 149 | } |
||
| 150 | $geosInstalled = class_exists('GEOSGeometry', false); |
||
| 151 | |||
| 152 | return $geosInstalled; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param \GEOSGeometry $geos |
||
| 157 | * @return Geometry|null |
||
| 158 | * @throws \Exception |
||
| 159 | * @codeCoverageIgnore |
||
| 160 | */ |
||
| 161 | public static function geosToGeometry($geos) |
||
| 162 | { |
||
| 163 | if (!geoPHP::geosInstalled()) { |
||
| 164 | return null; |
||
| 165 | } |
||
| 166 | /** @noinspection PhpUndefinedClassInspection */ |
||
| 167 | $wkbWriter = new \GEOSWKBWriter(); |
||
| 168 | /** @noinspection PhpUndefinedMethodInspection */ |
||
| 169 | $wkb = $wkbWriter->writeHEX($geos); |
||
| 170 | $geometry = geoPHP::load($wkb, 'wkb', true); |
||
| 171 | if ($geometry) { |
||
| 172 | $geometry->setGeos($geos); |
||
| 173 | return $geometry; |
||
| 174 | } |
||
| 175 | |||
| 176 | return null; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Reduce a geometry, or an array of geometries, into their 'lowest' available common geometry. |
||
| 181 | * For example a GeometryCollection of only points will become a MultiPoint |
||
| 182 | * A multi-point containing a single point will return a point. |
||
| 183 | * An array of geometries can be passed and they will be compiled into a single geometry |
||
| 184 | * |
||
| 185 | * @param Geometry|Geometry[]|GeometryCollection|GeometryCollection[] $geometries |
||
| 186 | * @return Geometry|false |
||
| 187 | */ |
||
| 188 | public static function geometryReduce($geometries) |
||
| 189 | { |
||
| 190 | if (empty($geometries)) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | /* |
||
| 194 | * If it is a single geometry |
||
| 195 | */ |
||
| 196 | if ($geometries instanceof Geometry) { |
||
| 197 | // If the geometry cannot even theoretically be reduced more, then pass it back |
||
| 198 | $singleGeometries = ['Point', 'LineString', 'Polygon']; |
||
| 199 | if (in_array($geometries->geometryType(), $singleGeometries)) { |
||
| 200 | return $geometries; |
||
| 201 | } |
||
| 202 | |||
| 203 | // If it is a multi-geometry, check to see if it just has one member |
||
| 204 | // If it does, then pass the member, if not, then just pass back the geometry |
||
| 205 | if (strpos($geometries->geometryType(), 'Multi') === 0) { |
||
| 206 | $components = $geometries->getComponents(); |
||
| 207 | if (count($components) == 1) { |
||
| 208 | return $components[0]; |
||
| 209 | } else { |
||
| 210 | return $geometries; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } elseif (is_array($geometries) && count($geometries) == 1) { |
||
| 214 | // If it's an array of one, then just parse the one |
||
| 215 | return geoPHP::geometryReduce(array_shift($geometries)); |
||
| 216 | } |
||
| 217 | |||
| 218 | if (!is_array($geometries)) { |
||
| 219 | $geometries = [$geometries]; |
||
| 220 | } |
||
| 221 | /** |
||
| 222 | * So now we either have an array of geometries |
||
| 223 | * @var Geometry[]|GeometryCollection[] $geometries |
||
| 224 | */ |
||
| 225 | |||
| 226 | $reducedGeometries = []; |
||
| 227 | $geometryTypes = []; |
||
| 228 | self::explodeCollections($geometries, $reducedGeometries, $geometryTypes); |
||
| 229 | |||
| 230 | $geometryTypes = array_unique($geometryTypes); |
||
| 231 | if (empty($geometryTypes)) { |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | if (count($geometryTypes) == 1) { |
||
| 235 | if (count($reducedGeometries) == 1) { |
||
| 236 | return $reducedGeometries[0]; |
||
| 237 | } else { |
||
| 238 | $class = self::CLASS_NAMESPACE . |
||
| 239 | 'Geometry\\' . |
||
| 240 | (strstr($geometryTypes[0], 'Multi') ? '' : 'Multi') . |
||
| 241 | $geometryTypes[0]; |
||
| 242 | return new $class($reducedGeometries); |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | return new GeometryCollection($reducedGeometries); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param Geometry[]|GeometryCollection[] $unreduced |
||
| 251 | */ |
||
| 252 | private static function explodeCollections($unreduced, &$reduced, &$types) |
||
| 253 | { |
||
| 254 | foreach ($unreduced as $item) { |
||
| 255 | if ($item->geometryType() == 'GeometryCollection' || strpos($item->geometryType(), 'Multi') === 0) { |
||
| 256 | self::explodeCollections($item->getComponents(), $reduced, $types); |
||
| 257 | } else { |
||
| 258 | $reduced[] = $item; |
||
| 259 | $types[] = $item->geometryType(); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Build an appropriate Geometry, MultiGeometry, or GeometryCollection to contain the Geometries in it. |
||
| 266 | * |
||
| 267 | * @see geos::geom::GeometryFactory::buildGeometry |
||
| 268 | * |
||
| 269 | * @param Geometry|Geometry[]|GeometryCollection|GeometryCollection[] $geometries |
||
| 270 | * @return Geometry A Geometry of the "smallest", "most type-specific" class that can contain the elements. |
||
| 271 | * @throws \Exception |
||
| 272 | */ |
||
| 273 | public static function buildGeometry($geometries) |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Detect a format given a value. This function is meant to be SPEEDY. |
||
| 333 | * It could make a mistake in XML detection if you are mixing or using namespaces in weird ways |
||
| 334 | * (ie, KML inside an RSS feed) |
||
| 335 | * |
||
| 336 | * @param mixed $input |
||
| 337 | * |
||
| 338 | * @return string|false |
||
| 339 | */ |
||
| 340 | public static function detectFormat(&$input) |
||
| 441 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths