Complex classes like Api 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 Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Api |
||
| 13 | { |
||
| 14 | const SOURCE_DEFAULT = 'default'; |
||
| 15 | |||
| 16 | const SOURCE_OUTDOOR = 'outdoor'; |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | private $endpointImage = 'https://maps.googleapis.com/maps/api/streetview'; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | private $endpointMetadata = 'https://maps.googleapis.com/maps/api/streetview/metadata'; |
||
| 23 | |||
| 24 | /** @var \GuzzleHttp\Client */ |
||
| 25 | private $client; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | private $apiKey; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | private $signature; |
||
| 32 | |||
| 33 | /** @var int */ |
||
| 34 | private $imageWidth = 600; |
||
| 35 | |||
| 36 | /** @var int */ |
||
| 37 | private $imageHeight = 600; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | private $heading; |
||
| 41 | |||
| 42 | /** @var int */ |
||
| 43 | private $cameraFov = 90; |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | private $cameraPitch = 0; |
||
| 47 | |||
| 48 | /** @var int */ |
||
| 49 | private $radius = 50; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $source = 'default'; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Api constructor. |
||
| 57 | * |
||
| 58 | * @param Client $client |
||
| 59 | */ |
||
| 60 | 21 | public function __construct(Client $client) |
|
| 61 | { |
||
| 62 | 21 | $this->client = $client; |
|
| 63 | 21 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * API key from your Google console |
||
| 67 | * |
||
| 68 | * @param string $apiKey |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | public function setApiKey(string $apiKey): self |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Digital signature used to verify that any site generating requests. |
||
| 80 | * |
||
| 81 | * @param string $signature |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function setSignature(string $signature): self |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Determines the horizontal field of view of the image. |
||
| 93 | * The field of view is expressed in degrees, with a maximum allowed value of 120. |
||
| 94 | * When dealing with a fixed-size viewport, as with a Street View image of a set size, |
||
| 95 | * field of view in essence represents zoom, with smaller numbers indicating a higher level of zoom. |
||
| 96 | * |
||
| 97 | * @param int $cameraFov |
||
| 98 | * @return $this |
||
| 99 | * @throws UnexpectedValueException |
||
| 100 | */ |
||
| 101 | 2 | public function setCameraFov(int $cameraFov): self |
|
| 102 | { |
||
| 103 | 2 | if ($cameraFov > 120) { |
|
| 104 | 1 | throw new UnexpectedValueException( |
|
| 105 | 1 | 'Camera FOV value cannot exceed 120 degrees.' |
|
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | 1 | $this->cameraFov = $cameraFov; |
|
| 110 | |||
| 111 | 1 | return $this; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Specifies the up or down angle of the camera relative to the Street View vehicle. |
||
| 116 | * This is often, but not always, flat horizontal. |
||
| 117 | * Positive values angle the camera up (with 90 degrees indicating straight up); |
||
| 118 | * negative values angle the camera down (with -90 indicating straight down). |
||
| 119 | * |
||
| 120 | * @param int $cameraPitch |
||
| 121 | * @return $this |
||
| 122 | * @throws UnexpectedValueException |
||
| 123 | */ |
||
| 124 | 3 | public function setCameraPitch(int $cameraPitch):self |
|
| 125 | { |
||
| 126 | 3 | if ($cameraPitch > 90) { |
|
| 127 | 1 | throw new UnexpectedValueException( |
|
| 128 | 1 | 'Camera pitch value for Google Street View cannot exceed 90 degrees.' |
|
| 129 | ); |
||
| 130 | } |
||
| 131 | 2 | if ($cameraPitch < -90) { |
|
| 132 | 1 | throw new UnexpectedValueException( |
|
| 133 | 1 | 'Camera pitch value for Google Street View cannot be inferior of -90 degrees.' |
|
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | 1 | $this->cameraPitch = $cameraPitch; |
|
| 138 | |||
| 139 | 1 | return $this; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Sets a radius, specified in meters, |
||
| 144 | * in which to search for a panorama, centered on the given latitude and longitude. |
||
| 145 | * Valid values are non-negative integers. |
||
| 146 | * |
||
| 147 | * @param int $radius |
||
| 148 | * @return $this |
||
| 149 | * @throws UnexpectedValueException |
||
| 150 | */ |
||
| 151 | 2 | public function setRadius(int $radius): self |
|
| 152 | { |
||
| 153 | 2 | if ($radius < 0) { |
|
| 154 | 1 | throw new UnexpectedValueException( |
|
| 155 | 1 | 'Radius value cannot be negative.' |
|
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | 1 | $this->radius = $radius; |
|
| 160 | |||
| 161 | 1 | return $this; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Indicates the compass heading of the camera. |
||
| 166 | * Accepted values are from 0 to 360 (both values indicating North, with 90 indicating East, and 180 South). |
||
| 167 | * |
||
| 168 | * @param int $heading |
||
| 169 | * @return $this |
||
| 170 | * @throws UnexpectedValueException |
||
| 171 | */ |
||
| 172 | 3 | public function setHeading(int $heading): self |
|
| 173 | { |
||
| 174 | 3 | if ($heading < 0) { |
|
| 175 | 1 | throw new UnexpectedValueException( |
|
| 176 | 1 | 'Heading value cannot be inferior to zero degree.' |
|
| 177 | ); |
||
| 178 | } |
||
| 179 | 2 | if ($heading > 360) { |
|
| 180 | 1 | throw new UnexpectedValueException( |
|
| 181 | 1 | 'Heading value cannot exceed 360 degrees.' |
|
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | 1 | $this->heading = $heading; |
|
| 186 | |||
| 187 | 1 | return $this; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Limits Street View searches to selected sources. Valid values are: |
||
| 192 | * - default uses the default sources for Street View; searches are not limited to specific sources. |
||
| 193 | * - outdoor limits searches to outdoor collections. |
||
| 194 | * |
||
| 195 | * @param string $source |
||
| 196 | * @return $this |
||
| 197 | * @throws UnexpectedValueException |
||
| 198 | */ |
||
| 199 | 2 | public function setSource(string $source): self |
|
| 200 | { |
||
| 201 | 2 | if (!in_array($source, [self::SOURCE_DEFAULT, self::SOURCE_OUTDOOR], true)) { |
|
| 202 | 1 | throw new UnexpectedValueException(sprintf( |
|
| 203 | 1 | 'Source value "%s" is unknown, only "%s" or "%s" values expected.', |
|
| 204 | 1 | $source, self::SOURCE_DEFAULT, self::SOURCE_OUTDOOR |
|
| 205 | )); |
||
| 206 | } |
||
| 207 | |||
| 208 | 1 | $this->source = $source; |
|
| 209 | |||
| 210 | 1 | return $this; |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param int $height |
||
| 215 | * @return $this |
||
| 216 | * @throws UnexpectedValueException |
||
| 217 | */ |
||
| 218 | 2 | public function setImageHeight(int $height): self |
|
| 219 | { |
||
| 220 | 2 | if ($height < 1) { |
|
| 221 | 1 | throw new UnexpectedValueException( |
|
| 222 | 1 | 'Image height value cannot be negative or equal to zero.' |
|
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | 1 | $this->imageHeight = $height; |
|
| 227 | |||
| 228 | 1 | return $this; |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param int $width |
||
| 233 | * @return $this |
||
| 234 | * @throws UnexpectedValueException |
||
| 235 | */ |
||
| 236 | 2 | public function setImageWidth(int $width): self |
|
| 237 | { |
||
| 238 | 2 | if ($width < 1) { |
|
| 239 | 1 | throw new UnexpectedValueException( |
|
| 240 | 1 | 'Image height value cannot be negative or equal to zero.' |
|
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | 1 | $this->imageWidth = $width; |
|
| 245 | |||
| 246 | 1 | return $this; |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns URL to a static (non-interactive) Street View panorama or thumbnail. |
||
| 251 | * |
||
| 252 | * @param string $location |
||
| 253 | * @return string |
||
| 254 | * @throws BadStatusCodeException |
||
| 255 | */ |
||
| 256 | public function getImageUrlByLocation(string $location): string |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns URL to a static (non-interactive) Street View panorama or thumbnail |
||
| 267 | * |
||
| 268 | * @param float $latitude |
||
| 269 | * @param float $longitude |
||
| 270 | * @return string |
||
| 271 | * @throws BadStatusCodeException |
||
| 272 | */ |
||
| 273 | public function getImageUrlByLatitudeAndLongitude(float $latitude, float $longitude): string |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns URL to a static (non-interactive) Street View panorama or thumbnail |
||
| 284 | * |
||
| 285 | * @param string $panoramaId |
||
| 286 | * @return string |
||
| 287 | * @throws BadStatusCodeException |
||
| 288 | */ |
||
| 289 | public function getImageUrlByPanoramaId(string $panoramaId): string |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns URL to a static (non-interactive) Street View panorama or thumbnail |
||
| 300 | * The viewport is defined with URL parameters sent through a standard HTTP request, and is returned as a static image. |
||
| 301 | * |
||
| 302 | * @param array $parameters |
||
| 303 | * @return string |
||
| 304 | * @throws BadStatusCodeException |
||
| 305 | */ |
||
| 306 | private function getImageUrl(array $parameters): string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Requests provide data about Street View panoramas. |
||
| 324 | * Using the metadata, you can find out if a Street View image is available at a given location, |
||
| 325 | * as well as getting programmatic access to the latitude and longitude, |
||
| 326 | * the panorama ID, the date the photo was taken, and the copyright information for the image. |
||
| 327 | * Accessing this metadata allows you to customize error behavior in your application. |
||
| 328 | * Street View API metadata requests are free to use. No quota is consumed when you request metadata. |
||
| 329 | * |
||
| 330 | * @param string $location |
||
| 331 | * @return array |
||
| 332 | * @throws UnexpectedValueException |
||
| 333 | * @throws RequestException |
||
| 334 | * @throws BadStatusCodeException |
||
| 335 | * @throws UnexpectedStatusException |
||
| 336 | */ |
||
| 337 | 5 | public function getMetadata(string $location): array |
|
| 405 | |||
| 406 | 1 | protected function formatMetadataResponse($response): array |
|
| 416 | |||
| 417 | 4 | private function getRequestPayload(array $parameters): array |
|
| 421 | |||
| 422 | 4 | private function getRequestParameters(array $parameters): array |
|
| 443 | |||
| 444 | } |
||
| 445 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: