Skip to content

Commit

Permalink
Add PHPStan in CI (#1193)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jbelien and norkunas committed Jul 16, 2023
1 parent f1a5a45 commit c83b9da
Show file tree
Hide file tree
Showing 96 changed files with 491 additions and 424 deletions.
23 changes: 15 additions & 8 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,22 @@ on:

jobs:
test:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
deps: ['low', 'high']

name: PHP ${{ matrix.php-version }} (${{ matrix.deps }})

steps:
- uses: actions/checkout@v3

- name: Use PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: curl

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
if: ${{ matrix.deps == null }}
run: composer update --no-progress --prefer-dist
Expand All @@ -40,6 +33,20 @@ jobs:
- name: Install dependencies (high)
if: ${{ matrix.deps == 'high' }}
run: composer update --no-progress

- name: Run test suite
run: composer run-script test

phpstan:
runs-on: ubuntu-latest
name: PHPStan
steps:
- uses: actions/checkout@v3
- name: Use PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: curl
- name: Install dependencies
run: composer install --no-progress
- name: Run PHPStan
run: composer run-script analyse
43 changes: 27 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"name": "geocoder-php/geocoder",
"type": "library",
"description": "A development package for all providers",
"license": "MIT",
"type": "library",
"keywords": [
"geocoder",
"geocoding",
"abstraction",
"geoip"
],
"homepage": "http://geocoder-php.org",
"license": "MIT",
"authors": [
{
"name": "William Durand",
"email": "[email protected]"
}
],
"homepage": "http://geocoder-php.org",
"require": {
"php": "^7.4 || ^8.0",
"igorw/get-in": "^1.0",
Expand All @@ -25,36 +25,36 @@
"psr/http-client": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-message-implementation": "^1.0",
"psr/log": "^1.0|^2.0|^3.0",
"psr/simple-cache": "^1.0|^2.0|^3.0"
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"cache/array-adapter": "^1.0",
"cache/simple-cache-bridge": "^1.0",
"cache/void-adapter": "^1.0",
"geocoder-php/provider-integration-tests": "^1.0",
"geocoder-php/provider-integration-tests": "^1.6.2",
"geoip2/geoip2": "~2.0",
"nyholm/nsa": "^1.1",
"nyholm/psr7": "^1.0",
"php-http/curl-client": "^2.2",
"php-http/message": "^1.0",
"php-http/mock-client": "^1.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5",
"symfony/stopwatch": "~2.5 || ~5.0"
},
"suggest": {
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
"ext-json": "If you want to use HostIp",
"ext-simplexml": "If you want to use HostIpXml",
"geoip/geoip": "If you are going to use the MaxMindBinaryProvider (conflict with geoip extension).",
"geoip2/geoip2": "If you are going to use the GeoIP2DatabaseProvider.",
"symfony/stopwatch": "If you want to use the TimedGeocoder",
"ext-simplexml": "If you want to use HostIpXml",
"ext-json": "If you want to use HostIp"
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
"symfony/stopwatch": "If you want to use the TimedGeocoder"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Geocoder\\": [
Expand All @@ -68,9 +68,20 @@
"Geocoder\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"phpstan/extension-installer": true,
"php-http/discovery": true
},
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/phpunit"
}
}
61 changes: 61 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
parameters:
ignoreErrors:
-
message: "#^Static method Geocoder\\\\ProviderAggregator\\:\\:getProvider\\(\\) is unused\\.$#"
count: 1
path: src/Common/ProviderAggregator.php

-
message: "#^Parameter \\#1 \\$locations of class Geocoder\\\\Model\\\\AddressCollection constructor expects array\\<Geocoder\\\\Location\\>, array\\<string, string\\> given\\.$#"
count: 2
path: src/Provider/Chain/Tests/ChainTest.php

-
message: "#^Parameter \\#1 \\$geoIpProvider of class Geocoder\\\\Provider\\\\GeoIP2\\\\GeoIP2Adapter constructor expects GeoIp2\\\\ProviderInterface, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
count: 4
path: src/Provider/GeoIP2/Tests/GeoIP2AdapterTest.php

-
message: "#^Access to constant ALL on an unknown class IP2Location\\\\Database\\.$#"
count: 1
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php

-
message: "#^Call to method lookup\\(\\) on an unknown class IP2Location\\\\Database\\.$#"
count: 1
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php

-
message: "#^Instantiated class IP2Location\\\\Database not found\\.$#"
count: 1
path: src/Provider/IP2LocationBinary/IP2LocationBinary.php

-
message: "#^Access to constant FILE_IO on an unknown class IP2Location\\\\Database\\.$#"
count: 1
path: src/Provider/IP2LocationBinary/Tests/IntegrationTest.php

-
message: "#^Access to property \\$[a-zA-Z_]+ on an unknown class GeoIpRecord\\.$#"
count: 10
path: src/Provider/MaxMindBinary/MaxMindBinary.php

-
message: "#^Class GeoIpRecord not found\\.$#"
count: 1
path: src/Provider/MaxMindBinary/MaxMindBinary.php

-
message: "#^Constant GEOIP_STANDARD not found\\.$#"
count: 1
path: src/Provider/MaxMindBinary/MaxMindBinary.php

-
message: "#^Function GeoIP_record_by_addr not found\\.$#"
count: 1
path: src/Provider/MaxMindBinary/MaxMindBinary.php

-
message: "#^Function geoip_(open|close) not found\\.$#"
count: 2
path: src/Provider/MaxMindBinary/MaxMindBinary.php
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- phpstan-baseline.neon
parameters:
level: 5
paths:
- src
excludePaths:
- **/vendor/**
treatPhpDocTypesAsCertain: false
2 changes: 1 addition & 1 deletion src/Common/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Address implements Location
* @param Country|null $country
* @param string|null $timezone
*/
public function __construct(
final public function __construct(
string $providedBy,
AdminLevelCollection $adminLevels,
Coordinates $coordinates = null,
Expand Down
8 changes: 0 additions & 8 deletions src/Common/ProviderAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public function __construct(callable $decider = null, int $limit = Geocoder::DEF
*/
public function geocodeQuery(GeocodeQuery $query): Collection
{
if (null === $query->getLimit()) {
$query = $query->withLimit($this->limit);
}

return call_user_func($this->decider, $query, $this->providers, $this->provider)->geocodeQuery($query);
}

Expand All @@ -72,10 +68,6 @@ public function geocodeQuery(GeocodeQuery $query): Collection
*/
public function reverseQuery(ReverseQuery $query): Collection
{
if (null === $query->getLimit()) {
$query = $query->withLimit($this->limit);
}

return call_user_func($this->decider, $query, $this->providers, $this->provider)->reverseQuery($query);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/StatefulGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function geocode(string $value): Collection
$query = $query->withLocale($this->locale);
}

if (!empty($this->bounds)) {
if (null !== $this->bounds) {
$query = $query->withBounds($this->bounds);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Common/Tests/TimedGeocoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Provider;
use Geocoder\TimedGeocoder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Stopwatch\Stopwatch;

Expand All @@ -26,7 +27,7 @@ class TimedGeocoderTest extends TestCase
private $stopwatch;

/**
* @var Provider|\PHPUnit_Framework_MockObject_MockObject
* @var Provider&MockObject
*/
private $delegate;

Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Plugin/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ private function getCacheKey(Query $query): string
{
if (null !== $this->precision && $query instanceof ReverseQuery) {
$query = $query->withCoordinates(new Coordinates(
number_format($query->getCoordinates()->getLatitude(), $this->precision),
number_format($query->getCoordinates()->getLongitude(), $this->precision)
(float) number_format($query->getCoordinates()->getLatitude(), $this->precision),
(float) number_format($query->getCoordinates()->getLongitude(), $this->precision)
));
}

Expand Down
3 changes: 2 additions & 1 deletion src/Plugin/Plugin/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Geocoder\Collection;
use Geocoder\Exception\Exception;
use Geocoder\Plugin\Plugin;
use Geocoder\Query\Query;
use Psr\Log\LoggerInterface;

Expand All @@ -22,7 +23,7 @@
*
* @author Tobias Nyholm <[email protected]>
*/
class LoggerPlugin
class LoggerPlugin implements Plugin
{
/**
* @var LoggerInterface
Expand Down
5 changes: 1 addition & 4 deletions src/Plugin/PluginProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ class PluginProvider implements Provider
/**
* @param Provider $provider
* @param Plugin[] $plugins
* @param array $options {
*
* @var int $max_restarts
* }
* @param array{max_restarts?: int<0, max>} $options
*/
public function __construct(Provider $provider, array $plugins = [], array $options = [])
{
Expand Down
6 changes: 1 addition & 5 deletions src/Provider/AlgoliaPlaces/AlgoliaPlaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ class AlgoliaPlaces extends AbstractHttpProvider implements Provider
/** @var GeocodeQuery */
private $query;

/** @var ClientInterface */
private $client;

public function __construct(ClientInterface $client, string $apiKey = null, string $appId = null)
{
parent::__construct($client);

$this->apiKey = $apiKey;
$this->client = $client;
$this->appId = $appId;
}

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

$request = $this->getRequest(self::ENDPOINT_URL_SSL);
$jsonParsed = AbstractHttpProvider::getParsedResponse($request);
$jsonParsed = $this->getParsedResponse($request);
$jsonResponse = json_decode($jsonParsed, true);

if (is_null($jsonResponse)) {
Expand Down
5 changes: 2 additions & 3 deletions src/Provider/AlgoliaPlaces/Tests/AlgoliaPlacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Geocoder\Query\GeocodeQuery;
use Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces;
use Http\Client\Curl\Client as HttplugClient;
use Psr\Http\Client\ClientInterface;

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

/**
* Get a real HTTP client. If a cache dir is set to a path it will use cached responses.
*
* @return HttpClient
*/
protected function getHttpClient($apiKey = null, $appCode = null)
protected function getHttpClient($apiKey = null, $appCode = null): ClientInterface
{
if (null !== $cacheDir = $this->getCacheDir()) {
return new CachedResponseClient(new HttplugClient(), $cacheDir, $apiKey, $appCode);
Expand Down
Loading

0 comments on commit c83b9da

Please sign in to comment.