Skip to content

Commit 49e90ab

Browse files
committed
check that fetched JSON is array with certain required fields
1 parent eca51a0 commit 49e90ab

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 8
2+
level: 10
33
paths:
44
- src
55
- tests

src/Geocoder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function setHost(string $host): void
6767

6868
/**
6969
* @param array<string, string> $optParams
70-
* @return ?array<string, mixed>
70+
* @return array{status: array{code: int, message: string}, results: list<mixed>, total_results: int, ...}
7171
*/
72-
public function geocode(string $query, array $optParams = []): ?array
72+
public function geocode(string $query, array $optParams = []): array
7373
{
7474
$url = $this->url . 'q=' . urlencode($query);
7575

@@ -85,6 +85,10 @@ public function geocode(string $query, array $optParams = []): ?array
8585
$url .= '&key=' . urlencode($this->key);
8686

8787
$ret = json_decode($this->getJSON($url), true);
88+
if (!is_array($ret)) {
89+
throw new \Exception('Failed to decode API response');
90+
}
91+
/** @var array{status: array{code: int, message: string}, results: list<mixed>, total_results: int, ...} */
8892
return $ret;
8993
}
9094

tests/GeocoderTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function testInvalidKey(): void
2323
$geocoder = new Geocoder('invalid-APIKEY');
2424
$result = $geocoder->geocode('Johannesburg');
2525
// print_r($result);
26-
$this->assertNotNull($result);
2726
$this->assertEquals(401, $result['status']['code']);
2827
$this->assertEquals('invalid API key', $result['status']['message']);
2928
}
@@ -49,7 +48,6 @@ public function testNetworkRequestError(): void
4948
$result = $geocoder->geocode('London');
5049
// print_r($result);
5150

52-
$this->assertNotNull($result);
5351
$this->assertEquals(498, $result['status']['code']);
5452
$this->assertStringContainsString('network issue', $result['status']['message']);
5553
$this->assertStringContainsString('doesnotexist.opencagedata.com', $result['status']['message']);
@@ -60,7 +58,6 @@ public function testOverQuota(): void
6058
// https://opencagedata.com/api#testingkeys
6159
$geocoder = new Geocoder('4372eff77b8343cebfc843eb4da4ddc4');
6260
$result = $geocoder->geocode('Johannesburg');
63-
$this->assertNotNull($result);
6461
$this->assertEquals(402, $result['status']['code']);
6562
$this->assertEquals('quota exceeded', $result['status']['message']);
6663
}
@@ -74,7 +71,6 @@ public function testLondon(): void
7471

7572
// print_r($result);
7673

77-
$this->assertNotNull($result);
7874
$this->assertEquals(200, $result['status']['code']);
7975
$this->assertEquals('OK', $result['status']['message']);
8076
}
@@ -94,7 +90,6 @@ public function testProxy(): void
9490
}
9591

9692
$result = $geocoder->geocode("82 Clerkenwell Road, London");
97-
$this->assertNotNull($result);
9893
$this->assertEquals(200, $result['status']['code']);
9994
$this->assertEquals('OK', $result['status']['message']);
10095
}

0 commit comments

Comments
 (0)