Skip to content

Commit e3b43b5

Browse files
authored
Merge pull request #24 from InteractionDesignFoundation/array-shape
Describe Location array shape
2 parents a575f16 + 6af21a8 commit e3b43b5

File tree

7 files changed

+85
-19
lines changed

7 files changed

+85
-19
lines changed

Diff for: psalm-baseline.xml

+47-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
33
<file src="src/Cache.php">
4-
<MixedAssignment>
5-
<code><![CDATA[$value]]></code>
6-
</MixedAssignment>
74
<MixedInferredReturnType>
85
<code><![CDATA[bool]]></code>
96
</MixedInferredReturnType>
@@ -64,6 +61,12 @@
6461
<code><![CDATA[Update]]></code>
6562
</PropertyNotSetInConstructor>
6663
</file>
64+
<file src="src/Contracts/ServiceInterface.php">
65+
<InvalidParamDefault>
66+
<code><![CDATA[LocationArray]]></code>
67+
<code><![CDATA[LocationArray]]></code>
68+
</InvalidParamDefault>
69+
</file>
6770
<file src="src/GeoIP.php">
6871
<DeprecatedMethod>
6972
<code><![CDATA[setPrefix]]></code>
@@ -94,6 +97,9 @@
9497
<code><![CDATA[$this->config('cache_tags')]]></code>
9598
<code><![CDATA[$this->config('default_location', [])]]></code>
9699
</MixedArgument>
100+
<MixedArgumentTypeCoercion>
101+
<code><![CDATA[$this->default_location]]></code>
102+
</MixedArgumentTypeCoercion>
97103
<MixedAssignment>
98104
<code><![CDATA[$class]]></code>
99105
<code><![CDATA[$config]]></code>
@@ -166,6 +172,9 @@
166172
</UndefinedFunction>
167173
</file>
168174
<file src="src/Location.php">
175+
<InvalidParamDefault>
176+
<code><![CDATA[LocationArray]]></code>
177+
</InvalidParamDefault>
169178
<MissingParamType>
170179
<code><![CDATA[$key]]></code>
171180
<code><![CDATA[$value]]></code>
@@ -196,6 +205,21 @@
196205
<code><![CDATA[$this->config('continent_path')]]></code>
197206
<code><![CDATA[$this->config('continent_path')]]></code>
198207
</MixedArgument>
208+
<MixedArgumentTypeCoercion>
209+
<code><![CDATA[[
210+
'ip' => $ip,
211+
'iso_code' => $json->countryCode,
212+
'country' => $json->country,
213+
'city' => $json->city,
214+
'state' => $json->region,
215+
'state_name' => $json->regionName,
216+
'postal_code' => $json->zip,
217+
'lat' => $json->lat,
218+
'lon' => $json->lon,
219+
'timezone' => $json->timezone,
220+
'continent' => $this->getContinent($json->countryCode),
221+
]]]></code>
222+
</MixedArgumentTypeCoercion>
199223
<MixedAssignment>
200224
<code><![CDATA[$base['query']['key']]]></code>
201225
<code><![CDATA[$json]]></code>
@@ -242,6 +266,22 @@
242266
<code><![CDATA[$json]]></code>
243267
<code><![CDATA[$json]]></code>
244268
</MixedArgument>
269+
<MixedArgumentTypeCoercion>
270+
<code><![CDATA[[
271+
'ip' => $ip,
272+
'iso_code' => $json['country_code'],
273+
'country' => $json['country_name'],
274+
'city' => $json['city'],
275+
'state' => $json['region_code'],
276+
'state_name' => $json['region'],
277+
'postal_code' => $json['postal'],
278+
'lat' => $json['latitude'],
279+
'lon' => $json['longitude'],
280+
'timezone' => Arr::get($json, 'time_zone.name'),
281+
'continent' => Arr::get($json, 'continent_code'),
282+
'currency' => Arr::get($json, 'currency.code'),
283+
]]]></code>
284+
</MixedArgumentTypeCoercion>
245285
<MixedArrayAccess>
246286
<code><![CDATA[$json['city']]]></code>
247287
<code><![CDATA[$json['country_code']]]></code>
@@ -287,30 +327,25 @@
287327
</RedundantConditionGivenDocblockType>
288328
</file>
289329
<file src="src/Services/MaxMindDatabase.php">
290-
<ImplicitToStringCast>
291-
<code><![CDATA[$file]]></code>
292-
</ImplicitToStringCast>
293330
<MissingClosureParamType>
294331
<code><![CDATA[$directory]]></code>
295332
</MissingClosureParamType>
296333
<MixedArgument>
297334
<code><![CDATA[$directory]]></code>
298335
<code><![CDATA[$directory]]></code>
299-
<code><![CDATA[$path]]></code>
300-
<code><![CDATA[$path]]></code>
301-
<code><![CDATA[$path]]></code>
302-
<code><![CDATA[$path]]></code>
303336
<code><![CDATA[$this->config('database_path')]]></code>
304337
<code><![CDATA[$this->config('locales', ['en'])]]></code>
305338
<code><![CDATA[$this->config('update_url')]]></code>
306339
</MixedArgument>
307340
<MixedAssignment>
308341
<code><![CDATA[$file]]></code>
309-
<code><![CDATA[$path]]></code>
310342
</MixedAssignment>
311343
<MixedMethodCall>
312344
<code><![CDATA[getFilename]]></code>
313345
</MixedMethodCall>
346+
<PossiblyInvalidArgument>
347+
<code><![CDATA[$file]]></code>
348+
</PossiblyInvalidArgument>
314349
<PossiblyInvalidMethodCall>
315350
<code><![CDATA[getPathName]]></code>
316351
<code><![CDATA[isDir]]></code>
@@ -327,12 +362,7 @@
327362
<DocblockTypeContradiction>
328363
<code><![CDATA[is_null($this->errors)]]></code>
329364
</DocblockTypeContradiction>
330-
<InvalidScalarArgument>
331-
<code><![CDATA[$response]]></code>
332-
<code><![CDATA[$response]]></code>
333-
</InvalidScalarArgument>
334365
<MixedArgument>
335-
<code><![CDATA[$header_size]]></code>
336366
<code><![CDATA[$header_size]]></code>
337367
<code><![CDATA[Arr::get($this->config, 'headers', [])]]></code>
338368
<code><![CDATA[Arr::get($this->config, 'query', [])]]></code>
@@ -355,7 +385,7 @@
355385
</file>
356386
<file src="src/helpers.php">
357387
<MixedInferredReturnType>
358-
<code><![CDATA[\InteractionDesignFoundation\GeoIP\GeoIP|\InteractionDesignFoundation\GeoIP\Location]]></code>
388+
<code><![CDATA[($ip is null ? \InteractionDesignFoundation\GeoIP\GeoIP : \InteractionDesignFoundation\GeoIP\Location)]]></code>
359389
</MixedInferredReturnType>
360390
<MixedMethodCall>
361391
<code><![CDATA[getLocation]]></code>

Diff for: src/Cache.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Illuminate\Cache\CacheManager;
88

9+
/**
10+
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
11+
*/
912
class Cache
1013
{
1114
/**
@@ -56,6 +59,7 @@ public function setPrefix(?string $prefix = null): void
5659
*/
5760
public function get($name)
5861
{
62+
/** @psalm-var LocationArray|null $value */
5963
$value = $this->cache->get($this->prefix . $name);
6064

6165
return is_array($value)

Diff for: src/Contracts/ServiceInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace InteractionDesignFoundation\GeoIP\Contracts;
66

7+
/**
8+
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
9+
*/
710
interface ServiceInterface
811
{
912
/**
@@ -27,6 +30,7 @@ public function locate($ip);
2730
* Create a location instance from the provided attributes.
2831
*
2932
* @param array $attributes
33+
* @psalm-param LocationArray $attributes
3034
*
3135
* @return \InteractionDesignFoundation\GeoIP\Location
3236
*/

Diff for: src/GeoIP.php

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Illuminate\Cache\CacheManager;
1111
use Monolog\Handler\StreamHandler;
1212

13+
/**
14+
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
15+
*/
1316
class GeoIP
1417
{
1518
/**

Diff for: src/Location.php

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@
2626
* @property bool $default
2727
* @property bool $cached
2828
* @property-read string $displayName {@see static::getDisplayNameAttribute()}
29+
*
30+
* @psalm-type LocationArray = array{
31+
* ip: string,
32+
* iso_code: string|null,
33+
* country: string|null,
34+
* city: string|null,
35+
* state: string|null,
36+
* state_name: string|null,
37+
* postal_code: string|null,
38+
* lat: float|null,
39+
* lon: float|null,
40+
* timezone: string|null,
41+
* continent: string|null,
42+
* currency?: string|null,
43+
* default?: bool,
44+
* cached?: bool,
45+
* }
46+
* How to use it: @@psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
2947
*/
3048
class Location implements ArrayAccess
3149
{
@@ -40,6 +58,7 @@ class Location implements ArrayAccess
4058
* Create a new location instance.
4159
*
4260
* @param array $attributes
61+
* @psalm-param LocationArray $attributes
4362
*/
4463
public function __construct(array $attributes = [])
4564
{

Diff for: src/Services/IPApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function boot()
3434
$base = [
3535
'base_uri' => 'http://ip-api.com/',
3636
'headers' => [
37-
'User-Agent' => 'Laravel-GeoIP',
37+
'User-Agent' => 'Laravel-GeoIP-InteractionDesignFoundation',
3838
],
3939
'query' => [
4040
'fields' => 49663,

Diff for: src/Support/HttpClient.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public function get($url, array $query = [], array $headers = [])
7272
* @param array $query
7373
* @param array $headers
7474
*
75-
* @return array
75+
* @return array{string, array}
76+
*
77+
* @throws \RuntimeException
7678
*/
7779
public function execute($method, $url, array $query = [], array $headers = [])
7880
{
@@ -119,6 +121,10 @@ public function execute($method, $url, array $query = [], array $headers = [])
119121
// Make request
120122
curl_setopt($curl, CURLOPT_HEADER, true);
121123
$response = curl_exec($curl);
124+
if (! is_string($response)) {
125+
$curlError = curl_error($curl);
126+
throw new \RuntimeException("Failed to make {$method} HTTP request: {$curlError}");
127+
}
122128

123129
// Set HTTP response code
124130
$this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

0 commit comments

Comments
 (0)