GoogleGeocode::getBottomLeft()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * (c) Camptocamp <[email protected]>
4
 * (c) Patrick Hayes
5
 *
6
 * This code is open-source and licenced under the Modified BSD License.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * PHP Google Geocoder Adapter
13
 *
14
 *
15
 * @package    geoPHP
16
 * @author     Patrick Hayes <[email protected]>
17
 */
18
class GoogleGeocode extends GeoAdapter
19
{
20
21
  /**
22
   * Read an address string or array geometry objects
23
   *
24
   * @param string - Address to geocode
25
   * @param string - Type of Geometry to return. Can either be 'points' or 'bounds' (polygon)
26
   * @param Geometry|bounds-array - Limit the search area to within this region. For example
27
   *                                by default geocoding "Cairo" will return the location of Cairo Egypt.
28
   *                                If you pass a polygon of illinois, it will return Cairo IL.
29
   * @param return_multiple - Return all results in a multipoint or multipolygon
30
   * @return Geometry|GeometryCollection
31
   */
32
  public function read($address, $return_type = 'point', $bounds = FALSE, $return_multiple = FALSE) {
33
    if (is_array($address)) $address = join(',', $address);
34
    
35
    if (gettype($bounds) == 'object') {
36
      $bounds = $bounds->getBBox();
0 ignored issues
show
Bug introduced by
The method getBBox cannot be called on $bounds (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
    }
38
    if (gettype($bounds) == 'array') {
39
      $bounds_string = '&bounds='.$bounds['miny'].','.$bounds['minx'].'|'.$bounds['maxy'].','.$bounds['maxx'];
40
    }
41
    else {
42
      $bounds_string = '';
43
    }
44
    
45
    $url = "http://maps.googleapis.com/maps/api/geocode/json";
46
    $url .= '?address='. urlencode($address);
47
    $url .= $bounds_string;
48
    $url .= '&sensor=false';
49
    $this->result = json_decode(@file_get_contents($url));
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
    
51
    if ($this->result->status == 'OK') {
52
      if ($return_multiple == FALSE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
53
        if ($return_type == 'point') {
54
          return $this->getPoint();
55
        }
56
        if ($return_type == 'bounds' || $return_type == 'polygon') {
57
          return $this->getPolygon();
58
        }
59
      }
60
      if ($return_multiple == TRUE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
61
        if ($return_type == 'point') {
62
          $points = array();
63
          foreach ($this->result->results as $delta => $item) {
64
            $points[] = $this->getPoint($delta);
65
          }
66
          return new MultiPoint($points);
67
        }
68
        if ($return_type == 'bounds' || $return_type == 'polygon') {
69
          $polygons = array();
70
          foreach ($this->result->results as $delta => $item) {
71
            $polygons[] = $this->getPolygon($delta);
72
          }
73
          return new MultiPolygon($polygons);
74
        }
75
      }
76
    }
77
    else {
78
      if ($this->result->status) throw new Exception('Error in Google Geocoder: '.$this->result->status);
79
      else throw new Exception('Unknown error in Google Geocoder');
80
      return FALSE;
0 ignored issues
show
Unused Code introduced by
return FALSE; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
    }
82
  }
83
84
  /**
85
   * Serialize geometries into a WKT string.
86
   *
87
   * @param Geometry $geometry
88
   * @param string $return_type Should be either 'string' or 'array'
89
   *
90
   * @return string Does a reverse geocode of the geometry
91
   */
92
  public function write(Geometry $geometry, $return_type = 'string') {
93
    $centroid = $geometry->getCentroid();
94
    $lat = $centroid->getY();
95
    $lon = $centroid->getX();
96
    
97
    $url = "http://maps.googleapis.com/maps/api/geocode/json";
98
    $url .= '?latlng='.$lat.','.$lon;
99
    $url .= '&sensor=false';
100
    $this->result = json_decode(@file_get_contents($url));
101
    
102
    if ($this->result->status == 'OK') {
103
      if ($return_type == 'string') {
104
        return $this->result->results[0]->formatted_address;
105
      }
106
      if ($return_type == 'array') {
107
        return $this->result->results[0]->address_components;
108
      }
109
    }
110
    else {
111
      if ($this->result->status) throw new Exception('Error in Google Reverse Geocoder: '.$this->result->status);
112
      else throw new Exception('Unknown error in Google Reverse Geocoder');
113
      return FALSE;
0 ignored issues
show
Unused Code introduced by
return FALSE; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
114
    }
115
  }
116
  
117
  private function getPoint($delta = 0) {
118
    $lat = $this->result->results[$delta]->geometry->location->lat;
119
    $lon = $this->result->results[$delta]->geometry->location->lng;
120
    return new Point($lon, $lat);
121
  }
122
123
  private function getPolygon($delta = 0) {
124
    $points = array (
125
      $this->getTopLeft($delta),
126
      $this->getTopRight($delta),
127
      $this->getBottomRight($delta),
128
      $this->getBottomLeft($delta),
129
      $this->getTopLeft($delta),
130
    );
131
    $outer_ring = new LineString($points);
132
    return new Polygon(array($outer_ring));
133
  }
134
135
  private function getTopLeft($delta = 0) {
136
    $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat;
137
    $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng;
138
    return new Point($lon, $lat);
139
  }
140
141
  private function getTopRight($delta = 0) {
142
    $lat = $this->result->results[$delta]->geometry->bounds->northeast->lat;
143
    $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng;
144
    return new Point($lon, $lat);
145
  }
146
  
147
  private function getBottomLeft($delta = 0) {
148
    $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat;
149
    $lon = $this->result->results[$delta]->geometry->bounds->southwest->lng;
150
     return new Point($lon, $lat);
151
  }
152
153
  private function getBottomRight($delta = 0) {
154
    $lat = $this->result->results[$delta]->geometry->bounds->southwest->lat;
155
    $lon = $this->result->results[$delta]->geometry->bounds->northeast->lng;
156
    return new Point($lon, $lat);
157
  }
158
}
159