Skip to content

Commit 7d90db8

Browse files
committedMar 11, 2025··
Use PHP param, propery and return types
1 parent 03286d3 commit 7d90db8

14 files changed

+92
-210
lines changed
 

‎psalm-baseline.xml

+15-70
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@
6767
<code><![CDATA[MissingConfigurationException]]></code>
6868
</ClassMustBeFinal>
6969
</file>
70-
<file src="src/Facades/GeoIP.php">
71-
<ClassMustBeFinal>
72-
<code><![CDATA[GeoIP]]></code>
73-
</ClassMustBeFinal>
74-
<MissingOverrideAttribute>
75-
<code><![CDATA[protected static function getFacadeAccessor()]]></code>
76-
</MissingOverrideAttribute>
77-
</file>
7870
<file src="src/GeoIP.php">
7971
<ClassMustBeFinal>
8072
<code><![CDATA[GeoIP]]></code>
@@ -85,10 +77,6 @@
8577
<DeprecatedMethod>
8678
<code><![CDATA[setPrefix]]></code>
8779
</DeprecatedMethod>
88-
<DeprecatedProperty>
89-
<code><![CDATA[$this->remote_ip]]></code>
90-
<code><![CDATA[$this->remote_ip]]></code>
91-
</DeprecatedProperty>
9280
<DocblockTypeContradiction>
9381
<code><![CDATA[$this->service === null]]></code>
9482
</DocblockTypeContradiction>
@@ -140,9 +128,6 @@
140128
<ClassMustBeFinal>
141129
<code><![CDATA[GeoIPServiceProvider]]></code>
142130
</ClassMustBeFinal>
143-
<MissingOverrideAttribute>
144-
<code><![CDATA[public function register(): void]]></code>
145-
</MissingOverrideAttribute>
146131
<MixedArgument>
147132
<code><![CDATA[$app['cache']]]></code>
148133
<code><![CDATA[$app['config']->get('geoip', [])]]></code>
@@ -162,12 +147,6 @@
162147
<InvalidParamDefault>
163148
<code><![CDATA[LocationArray]]></code>
164149
</InvalidParamDefault>
165-
<MissingOverrideAttribute>
166-
<code><![CDATA[public function offsetExists(mixed $offset): bool]]></code>
167-
<code><![CDATA[public function offsetGet(mixed $offset): mixed]]></code>
168-
<code><![CDATA[public function offsetSet(mixed $offset, mixed $value): void]]></code>
169-
<code><![CDATA[public function offsetUnset(mixed $offset): void]]></code>
170-
</MissingOverrideAttribute>
171150
<MissingParamType>
172151
<code><![CDATA[$key]]></code>
173152
<code><![CDATA[$value]]></code>
@@ -191,11 +170,6 @@
191170
</PropertyTypeCoercion>
192171
</file>
193172
<file src="src/Services/AbstractService.php">
194-
<MissingOverrideAttribute>
195-
<code><![CDATA[public function boot()]]></code>
196-
<code><![CDATA[public function config($key, $default = null)]]></code>
197-
<code><![CDATA[public function hydrate(array $attributes = [])]]></code>
198-
</MissingOverrideAttribute>
199173
<MixedAssignment>
200174
<code><![CDATA[$configValue]]></code>
201175
</MixedAssignment>
@@ -204,10 +178,6 @@
204178
<ClassMustBeFinal>
205179
<code><![CDATA[IPApi]]></code>
206180
</ClassMustBeFinal>
207-
<MissingOverrideAttribute>
208-
<code><![CDATA[public function boot(): void]]></code>
209-
<code><![CDATA[public function locate($ip)]]></code>
210-
</MissingOverrideAttribute>
211181
<MixedArgument>
212182
<code><![CDATA[$json->countryCode]]></code>
213183
<code><![CDATA[$path]]></code>
@@ -249,28 +219,26 @@
249219
<code><![CDATA[file_get_contents($this->config('continent_path'))]]></code>
250220
<code><![CDATA[json_encode($output)]]></code>
251221
</PossiblyFalseArgument>
222+
<PossiblyNullArgument>
223+
<code><![CDATA[$this->client->getErrors()]]></code>
224+
</PossiblyNullArgument>
252225
<PossiblyNullArrayOffset>
253226
<code><![CDATA[$output]]></code>
254227
</PossiblyNullArrayOffset>
228+
<PossiblyNullOperand>
229+
<code><![CDATA[$this->client->getErrors()]]></code>
230+
</PossiblyNullOperand>
255231
<PropertyNotSetInConstructor>
256232
<code><![CDATA[$continents]]></code>
257233
</PropertyNotSetInConstructor>
258234
<RedundantCastGivenDocblockType>
259235
<code><![CDATA[(string) $line]]></code>
260236
</RedundantCastGivenDocblockType>
261-
<RedundantConditionGivenDocblockType>
262-
<code><![CDATA[$this->client->getErrors() !== null]]></code>
263-
<code><![CDATA[$this->client->getErrors() !== null]]></code>
264-
</RedundantConditionGivenDocblockType>
265237
</file>
266238
<file src="src/Services/IPData.php">
267239
<ClassMustBeFinal>
268240
<code><![CDATA[IPData]]></code>
269241
</ClassMustBeFinal>
270-
<MissingOverrideAttribute>
271-
<code><![CDATA[public function boot(): void]]></code>
272-
<code><![CDATA[public function locate($ip)]]></code>
273-
</MissingOverrideAttribute>
274242
<MixedArgument>
275243
<code><![CDATA[$json]]></code>
276244
<code><![CDATA[$json]]></code>
@@ -305,36 +273,28 @@
305273
<MixedAssignment>
306274
<code><![CDATA[$json]]></code>
307275
</MixedAssignment>
308-
<RedundantConditionGivenDocblockType>
309-
<code><![CDATA[$this->client->getErrors() !== null]]></code>
310-
</RedundantConditionGivenDocblockType>
276+
<PossiblyNullOperand>
277+
<code><![CDATA[$this->client->getErrors()]]></code>
278+
</PossiblyNullOperand>
311279
</file>
312280
<file src="src/Services/IPFinder.php">
313281
<ClassMustBeFinal>
314282
<code><![CDATA[IPFinder]]></code>
315283
</ClassMustBeFinal>
316-
<MissingOverrideAttribute>
317-
<code><![CDATA[public function boot(): void]]></code>
318-
<code><![CDATA[public function locate($ip)]]></code>
319-
</MissingOverrideAttribute>
320284
<MixedArgument>
321285
<code><![CDATA[$json]]></code>
322286
</MixedArgument>
323287
<MixedAssignment>
324288
<code><![CDATA[$json]]></code>
325289
</MixedAssignment>
326-
<RedundantConditionGivenDocblockType>
327-
<code><![CDATA[$this->client->getErrors() !== null]]></code>
328-
</RedundantConditionGivenDocblockType>
290+
<PossiblyNullOperand>
291+
<code><![CDATA[$this->client->getErrors()]]></code>
292+
</PossiblyNullOperand>
329293
</file>
330294
<file src="src/Services/IPGeoLocation.php">
331295
<ClassMustBeFinal>
332296
<code><![CDATA[IPGeoLocation]]></code>
333297
</ClassMustBeFinal>
334-
<MissingOverrideAttribute>
335-
<code><![CDATA[public function boot(): void]]></code>
336-
<code><![CDATA[public function locate($ip)]]></code>
337-
</MissingOverrideAttribute>
338298
<MixedArgument>
339299
<code><![CDATA[$json]]></code>
340300
</MixedArgument>
@@ -344,9 +304,9 @@
344304
<MixedOperand>
345305
<code><![CDATA[$this->config('key')]]></code>
346306
</MixedOperand>
347-
<RedundantConditionGivenDocblockType>
348-
<code><![CDATA[$this->client->getErrors() !== null]]></code>
349-
</RedundantConditionGivenDocblockType>
307+
<PossiblyNullOperand>
308+
<code><![CDATA[$this->client->getErrors()]]></code>
309+
</PossiblyNullOperand>
350310
</file>
351311
<file src="src/Services/MaxMindDatabase.php">
352312
<ClassMustBeFinal>
@@ -358,10 +318,6 @@
358318
<InvalidReturnType>
359319
<code><![CDATA[array<string, string|null>]]></code>
360320
</InvalidReturnType>
361-
<MissingOverrideAttribute>
362-
<code><![CDATA[public function boot(): void]]></code>
363-
<code><![CDATA[public function locate($ip)]]></code>
364-
</MissingOverrideAttribute>
365321
<MixedArgument>
366322
<code><![CDATA[$lang]]></code>
367323
<code><![CDATA[$lang]]></code>
@@ -416,10 +372,6 @@
416372
<InvalidReturnType>
417373
<code><![CDATA[array<string, string|null>]]></code>
418374
</InvalidReturnType>
419-
<MissingOverrideAttribute>
420-
<code><![CDATA[public function boot(): void]]></code>
421-
<code><![CDATA[public function locate($ip)]]></code>
422-
</MissingOverrideAttribute>
423375
<MixedArgument>
424376
<code><![CDATA[$lang]]></code>
425377
<code><![CDATA[$lang]]></code>
@@ -443,9 +395,6 @@
443395
<ClassMustBeFinal>
444396
<code><![CDATA[HttpClient]]></code>
445397
</ClassMustBeFinal>
446-
<DocblockTypeContradiction>
447-
<code><![CDATA[is_null($this->errors)]]></code>
448-
</DocblockTypeContradiction>
449398
<MixedArgument>
450399
<code><![CDATA[$header_size]]></code>
451400
<code><![CDATA[$row]]></code>
@@ -478,10 +427,6 @@
478427
<PossiblyFalseIterator>
479428
<code><![CDATA[preg_split("/\\r\\n|\\r|\\n/", $headers)]]></code>
480429
</PossiblyFalseIterator>
481-
<PossiblyNullPropertyAssignmentValue>
482-
<code><![CDATA[null]]></code>
483-
<code><![CDATA[null]]></code>
484-
</PossiblyNullPropertyAssignmentValue>
485430
<RiskyTruthyFalsyComparison>
486431
<code><![CDATA[strpos($url, '?')]]></code>
487432
</RiskyTruthyFalsyComparison>

‎src/Contracts/ServiceInterface.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
namespace InteractionDesignFoundation\GeoIP\Contracts;
66

7+
use InteractionDesignFoundation\GeoIP\Location;
8+
79
/**
810
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
911
*/
1012
interface ServiceInterface
1113
{
1214
/**
1315
* The "booting" method of the service.
14-
*
1516
* @return void
1617
*/
17-
public function boot();
18+
public function boot(): void;
1819

1920
/**
2021
* Determine a location based off of
@@ -26,24 +27,19 @@ public function boot();
2627
*
2728
* @throws \InvalidArgumentException if an invalid IP address is passed
2829
*/
29-
public function locate($ip);
30+
public function locate($ip): Location;
3031

3132
/**
3233
* Create a location instance from the provided attributes.
3334
*
3435
* @param array $attributes
3536
* @psalm-param LocationArray $attributes
36-
*
37-
* @return \InteractionDesignFoundation\GeoIP\Location
3837
*/
39-
public function hydrate(array $attributes = []);
38+
public function hydrate(array $attributes = []): Location;
4039

4140
/**
4241
* Get configuration value.
43-
*
44-
* @param string $key
45-
*
4642
* @return mixed
4743
*/
48-
public function config($key, mixed $default = null);
44+
public function config(string $key, mixed $default = null);
4945
}

‎src/Facades/GeoIP.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* @method static \InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface getService()
1414
* @see \InteractionDesignFoundation\GeoIP\GeoIP
1515
*/
16-
class GeoIP extends Facade
16+
final class GeoIP extends Facade
1717
{
1818
/**
1919
* Get the registered name of the component.
20-
*
2120
* @return string
2221
*/
23-
protected static function getFacadeAccessor()
22+
#[\Override]
23+
protected static function getFacadeAccessor(): string
2424
{
2525
return 'geoip';
2626
}

‎src/GeoIP.php

+6-16
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
*/
1313
class GeoIP
1414
{
15-
/**
16-
* Remote Machine IP address.
17-
* @deprecated Use {@see self::getClientIP()} instead.
18-
*/
19-
protected string $remote_ip;
20-
2115
/**
2216
* Current location instance.
2317
*
@@ -39,9 +33,7 @@ class GeoIP
3933
*/
4034
protected $service;
4135

42-
/**
43-
* Cache manager instance.
44-
*/
36+
/** Cache manager instance. */
4537
protected \InteractionDesignFoundation\GeoIP\Cache $cache;
4638

4739
/** Default Location data. */
@@ -85,7 +77,7 @@ public function __construct(protected array $config, CacheManager $cache)
8577
);
8678

8779
// Set IP
88-
$this->remote_ip = $this->default_location['ip'] = $this->getClientIP();
80+
$this->default_location['ip'] = $this->getClientIP();
8981
}
9082

9183
/**
@@ -111,16 +103,13 @@ public function getLocation($ip = null)
111103

112104
/**
113105
* Find location from IP.
114-
*
115-
* @param string $ip
116-
*
117106
* @return \InteractionDesignFoundation\GeoIP\Location
118107
* @throws \Exception
119108
*/
120-
private function find($ip = null): Location
109+
private function find(?string $ip = null): Location
121110
{
122111
// If IP not set, user remote IP
123-
$ip = $ip ?: $this->remote_ip;
112+
$ip = $ip ?: $this->getClientIP();
124113

125114
// Check cache for location
126115
if ($this->config('cache', 'none') !== 'none' && $location = $this->getCache()->get($ip)) {
@@ -258,7 +247,7 @@ public function getClientIP(): string
258247
*
259248
* @return bool
260249
*/
261-
private function isValid($ip): bool
250+
private function isValid(string $ip): bool
262251
{
263252
return !(! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
264253
&& ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE));
@@ -293,6 +282,7 @@ private function shouldCache(Location $location, ?string $ip = null): bool
293282
* Get configuration value.
294283
*
295284
* @param string $key
285+
* @param array|bool|int|null|string $default
296286
*
297287
* @return mixed
298288
*/

‎src/GeoIPServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class GeoIPServiceProvider extends ServiceProvider
1111
{
1212
/**
1313
* Register the service provider.
14-
*
1514
* @return void
1615
*/
16+
#[\Override]
1717
public function register(): void
1818
{
1919
$this->registerGeoIpService();

0 commit comments

Comments
 (0)
Please sign in to comment.