Skip to content

Commit c83b9da

Browse files
jbeliennorkunas
andauthored
Add PHPStan in CI (#1193)
* Add PHPStan * Update php.yml * Update composer.json * Fix PHPStan level 0 * Fix PHPStan level 1 * Update phpstan.neon * Fix PHPStan level 2 * Update composer.json * Fix PHPStan level 3 * Fix tests * Fix PHPStan level 4 * Update src/Common/Tests/TimedGeocoderTest.php Co-authored-by: Tomas Norkūnas <[email protected]> * Update src/Provider/Cache/Tests/ProviderCacheTest.php Co-authored-by: Tomas Norkūnas <[email protected]> * Update src/Provider/Cache/Tests/ProviderCacheTest.php Co-authored-by: Tomas Norkūnas <[email protected]> * Update src/Provider/GeoIP2/Tests/GeoIP2Test.php Co-authored-by: Tomas Norkūnas <[email protected]> * Fix PHPStan level 5 * Normalize composer.json * Rename analyse script * Update composer.json * Update IntegrationTest * Update AlgoliaPlacesTest * Update composer.json * Update RequestInterface vs. getParsedResponse() * Update src/Plugin/PluginProvider.php Co-authored-by: Tomas Norkūnas <[email protected]> * Update src/Plugin/PluginProvider.php Co-authored-by: Tomas Norkūnas <[email protected]> * Use PHPStan baseline instead of ignore See https://phpstan.org/user-guide/baseline --------- Co-authored-by: Tomas Norkūnas <[email protected]>
1 parent f1a5a45 commit c83b9da

File tree

96 files changed

+491
-424
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+491
-424
lines changed

.github/workflows/php.yml

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@ on:
88

99
jobs:
1010
test:
11-
1211
runs-on: ubuntu-latest
13-
1412
strategy:
1513
fail-fast: false
1614
matrix:
1715
php-version: ['7.4', '8.0', '8.1', '8.2']
1816
deps: ['low', 'high']
19-
2017
name: PHP ${{ matrix.php-version }} (${{ matrix.deps }})
21-
2218
steps:
2319
- uses: actions/checkout@v3
24-
2520
- name: Use PHP ${{ matrix.php-version }}
2621
uses: shivammathur/setup-php@v2
2722
with:
2823
php-version: ${{ matrix.php-version }}
2924
extensions: curl
30-
3125
- name: Validate composer.json and composer.lock
3226
run: composer validate
33-
3427
- name: Install dependencies
3528
if: ${{ matrix.deps == null }}
3629
run: composer update --no-progress --prefer-dist
@@ -40,6 +33,20 @@ jobs:
4033
- name: Install dependencies (high)
4134
if: ${{ matrix.deps == 'high' }}
4235
run: composer update --no-progress
43-
4436
- name: Run test suite
4537
run: composer run-script test
38+
39+
phpstan:
40+
runs-on: ubuntu-latest
41+
name: PHPStan
42+
steps:
43+
- uses: actions/checkout@v3
44+
- name: Use PHP 8.2
45+
uses: shivammathur/setup-php@v2
46+
with:
47+
php-version: 8.2
48+
extensions: curl
49+
- name: Install dependencies
50+
run: composer install --no-progress
51+
- name: Run PHPStan
52+
run: composer run-script analyse

composer.json

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"name": "geocoder-php/geocoder",
3-
"type": "library",
43
"description": "A development package for all providers",
4+
"license": "MIT",
5+
"type": "library",
56
"keywords": [
67
"geocoder",
78
"geocoding",
89
"abstraction",
910
"geoip"
1011
],
11-
"homepage": "http://geocoder-php.org",
12-
"license": "MIT",
1312
"authors": [
1413
{
1514
"name": "William Durand",
1615
"email": "[email protected]"
1716
}
1817
],
18+
"homepage": "http://geocoder-php.org",
1919
"require": {
2020
"php": "^7.4 || ^8.0",
2121
"igorw/get-in": "^1.0",
@@ -25,36 +25,36 @@
2525
"psr/http-client": "^1.0",
2626
"psr/http-client-implementation": "^1.0",
2727
"psr/http-message-implementation": "^1.0",
28-
"psr/log": "^1.0|^2.0|^3.0",
29-
"psr/simple-cache": "^1.0|^2.0|^3.0"
28+
"psr/log": "^1.0 || ^2.0 || ^3.0",
29+
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
3030
},
3131
"require-dev": {
3232
"cache/array-adapter": "^1.0",
3333
"cache/simple-cache-bridge": "^1.0",
3434
"cache/void-adapter": "^1.0",
35-
"geocoder-php/provider-integration-tests": "^1.0",
35+
"geocoder-php/provider-integration-tests": "^1.6.2",
3636
"geoip2/geoip2": "~2.0",
3737
"nyholm/nsa": "^1.1",
3838
"nyholm/psr7": "^1.0",
3939
"php-http/curl-client": "^2.2",
4040
"php-http/message": "^1.0",
4141
"php-http/mock-client": "^1.0",
42+
"phpstan/extension-installer": "^1.3",
43+
"phpstan/phpstan": "^1.10",
44+
"phpstan/phpstan-phpunit": "^1.3",
4245
"phpunit/phpunit": "^9.5",
4346
"symfony/stopwatch": "~2.5 || ~5.0"
4447
},
4548
"suggest": {
4649
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
50+
"ext-json": "If you want to use HostIp",
51+
"ext-simplexml": "If you want to use HostIpXml",
4752
"geoip/geoip": "If you are going to use the MaxMindBinaryProvider (conflict with geoip extension).",
4853
"geoip2/geoip2": "If you are going to use the GeoIP2DatabaseProvider.",
49-
"symfony/stopwatch": "If you want to use the TimedGeocoder",
50-
"ext-simplexml": "If you want to use HostIpXml",
51-
"ext-json": "If you want to use HostIp"
52-
},
53-
"extra": {
54-
"branch-alias": {
55-
"dev-master": "4.0-dev"
56-
}
54+
"symfony/stopwatch": "If you want to use the TimedGeocoder"
5755
},
56+
"minimum-stability": "dev",
57+
"prefer-stable": true,
5858
"autoload": {
5959
"psr-4": {
6060
"Geocoder\\": [
@@ -68,9 +68,20 @@
6868
"Geocoder\\Tests\\": "tests/"
6969
}
7070
},
71-
"minimum-stability": "dev",
72-
"prefer-stable": true,
71+
"config": {
72+
"allow-plugins": {
73+
"phpstan/extension-installer": true,
74+
"php-http/discovery": true
75+
},
76+
"sort-packages": true
77+
},
78+
"extra": {
79+
"branch-alias": {
80+
"dev-master": "4.0-dev"
81+
}
82+
},
7383
"scripts": {
84+
"analyse": "vendor/bin/phpstan analyse",
7485
"test": "vendor/bin/phpunit"
7586
}
7687
}

phpstan-baseline.neon

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Static method Geocoder\\\\ProviderAggregator\\:\\:getProvider\\(\\) is unused\\.$#"
5+
count: 1
6+
path: src/Common/ProviderAggregator.php
7+
8+
-
9+
message: "#^Parameter \\#1 \\$locations of class Geocoder\\\\Model\\\\AddressCollection constructor expects array\\<Geocoder\\\\Location\\>, array\\<string, string\\> given\\.$#"
10+
count: 2
11+
path: src/Provider/Chain/Tests/ChainTest.php
12+
13+
-
14+
message: "#^Parameter \\#1 \\$geoIpProvider of class Geocoder\\\\Provider\\\\GeoIP2\\\\GeoIP2Adapter constructor expects GeoIp2\\\\ProviderInterface, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
15+
count: 4
16+
path: src/Provider/GeoIP2/Tests/GeoIP2AdapterTest.php
17+
18+
-
19+
message: "#^Access to constant ALL on an unknown class IP2Location\\\\Database\\.$#"
20+
count: 1
21+
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php
22+
23+
-
24+
message: "#^Call to method lookup\\(\\) on an unknown class IP2Location\\\\Database\\.$#"
25+
count: 1
26+
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php
27+
28+
-
29+
message: "#^Instantiated class IP2Location\\\\Database not found\\.$#"
30+
count: 1
31+
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php
32+
33+
-
34+
message: "#^Access to constant FILE_IO on an unknown class IP2Location\\\\Database\\.$#"
35+
count: 1
36+
path: src/Provider/IP2LocationBinary/Tests/IntegrationTest.php
37+
38+
-
39+
message: "#^Access to property \\$[a-zA-Z_]+ on an unknown class GeoIpRecord\\.$#"
40+
count: 10
41+
path: src/Provider/MaxMindBinary/MaxMindBinary.php
42+
43+
-
44+
message: "#^Class GeoIpRecord not found\\.$#"
45+
count: 1
46+
path: src/Provider/MaxMindBinary/MaxMindBinary.php
47+
48+
-
49+
message: "#^Constant GEOIP_STANDARD not found\\.$#"
50+
count: 1
51+
path: src/Provider/MaxMindBinary/MaxMindBinary.php
52+
53+
-
54+
message: "#^Function GeoIP_record_by_addr not found\\.$#"
55+
count: 1
56+
path: src/Provider/MaxMindBinary/MaxMindBinary.php
57+
58+
-
59+
message: "#^Function geoip_(open|close) not found\\.$#"
60+
count: 2
61+
path: src/Provider/MaxMindBinary/MaxMindBinary.php

phpstan.neon

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
parameters:
4+
level: 5
5+
paths:
6+
- src
7+
excludePaths:
8+
- **/vendor/**
9+
treatPhpDocTypesAsCertain: false

src/Common/Model/Address.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Address implements Location
8787
* @param Country|null $country
8888
* @param string|null $timezone
8989
*/
90-
public function __construct(
90+
final public function __construct(
9191
string $providedBy,
9292
AdminLevelCollection $adminLevels,
9393
Coordinates $coordinates = null,

src/Common/ProviderAggregator.php

-8
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public function __construct(callable $decider = null, int $limit = Geocoder::DEF
6060
*/
6161
public function geocodeQuery(GeocodeQuery $query): Collection
6262
{
63-
if (null === $query->getLimit()) {
64-
$query = $query->withLimit($this->limit);
65-
}
66-
6763
return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query);
6864
}
6965

@@ -72,10 +68,6 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7268
*/
7369
public function reverseQuery(ReverseQuery $query): Collection
7470
{
75-
if (null === $query->getLimit()) {
76-
$query = $query->withLimit($this->limit);
77-
}
78-
7971
return call_user_func($this->decider, $query, $this->providers, $this->provider)->reverseQuery($query);
8072
}
8173

src/Common/StatefulGeocoder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function geocode(string $value): Collection
6565
$query = $query->withLocale($this->locale);
6666
}
6767

68-
if (!empty($this->bounds)) {
68+
if (null !== $this->bounds) {
6969
$query = $query->withBounds($this->bounds);
7070
}
7171

src/Common/Tests/TimedGeocoderTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Geocoder\Model\AddressCollection;
1616
use Geocoder\Provider\Provider;
1717
use Geocoder\TimedGeocoder;
18+
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
1920
use Symfony\Component\Stopwatch\Stopwatch;
2021

@@ -26,7 +27,7 @@ class TimedGeocoderTest extends TestCase
2627
private $stopwatch;
2728

2829
/**
29-
* @var Provider|\PHPUnit_Framework_MockObject_MockObject
30+
* @var Provider&MockObject
3031
*/
3132
private $delegate;
3233

src/Plugin/Plugin/CachePlugin.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ private function getCacheKey(Query $query): string
7979
{
8080
if (null !== $this->precision && $query instanceof ReverseQuery) {
8181
$query = $query->withCoordinates(new Coordinates(
82-
number_format($query->getCoordinates()->getLatitude(), $this->precision),
83-
number_format($query->getCoordinates()->getLongitude(), $this->precision)
82+
(float) number_format($query->getCoordinates()->getLatitude(), $this->precision),
83+
(float) number_format($query->getCoordinates()->getLongitude(), $this->precision)
8484
));
8585
}
8686

src/Plugin/Plugin/LoggerPlugin.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Geocoder\Collection;
1616
use Geocoder\Exception\Exception;
17+
use Geocoder\Plugin\Plugin;
1718
use Geocoder\Query\Query;
1819
use Psr\Log\LoggerInterface;
1920

@@ -22,7 +23,7 @@
2223
*
2324
* @author Tobias Nyholm <[email protected]>
2425
*/
25-
class LoggerPlugin
26+
class LoggerPlugin implements Plugin
2627
{
2728
/**
2829
* @var LoggerInterface

src/Plugin/PluginProvider.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ class PluginProvider implements Provider
4949
/**
5050
* @param Provider $provider
5151
* @param Plugin[] $plugins
52-
* @param array $options {
53-
*
54-
* @var int $max_restarts
55-
* }
52+
* @param array{max_restarts?: int<0, max>} $options
5653
*/
5754
public function __construct(Provider $provider, array $plugins = [], array $options = [])
5855
{

src/Provider/AlgoliaPlaces/AlgoliaPlaces.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,11 @@ class AlgoliaPlaces extends AbstractHttpProvider implements Provider
5353
/** @var GeocodeQuery */
5454
private $query;
5555

56-
/** @var ClientInterface */
57-
private $client;
58-
5956
public function __construct(ClientInterface $client, string $apiKey = null, string $appId = null)
6057
{
6158
parent::__construct($client);
6259

6360
$this->apiKey = $apiKey;
64-
$this->client = $client;
6561
$this->appId = $appId;
6662
}
6763

@@ -79,7 +75,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7975
$this->query = $query;
8076

8177
$request = $this->getRequest(self::ENDPOINT_URL_SSL);
82-
$jsonParsed = AbstractHttpProvider::getParsedResponse($request);
78+
$jsonParsed = $this->getParsedResponse($request);
8379
$jsonResponse = json_decode($jsonParsed, true);
8480

8581
if (is_null($jsonResponse)) {

src/Provider/AlgoliaPlaces/Tests/AlgoliaPlacesTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Geocoder\Query\GeocodeQuery;
1919
use Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces;
2020
use Http\Client\Curl\Client as HttplugClient;
21+
use Psr\Http\Client\ClientInterface;
2122

2223
/**
2324
* @author Sébastien Barré <[email protected]>
@@ -31,10 +32,8 @@ protected function getCacheDir()
3132

3233
/**
3334
* Get a real HTTP client. If a cache dir is set to a path it will use cached responses.
34-
*
35-
* @return HttpClient
3635
*/
37-
protected function getHttpClient($apiKey = null, $appCode = null)
36+
protected function getHttpClient($apiKey = null, $appCode = null): ClientInterface
3837
{
3938
if (null !== $cacheDir = $this->getCacheDir()) {
4039
return new CachedResponseClient(new HttplugClient(), $cacheDir, $apiKey, $appCode);

0 commit comments

Comments
 (0)