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