Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add PHPStan in CI #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ade0019
Add PHPStan
jbelien Jul 9, 2023
1fc4c1a
Update php.yml
jbelien Jul 9, 2023
142df1f
Update composer.json
jbelien Jul 9, 2023
b0c1823
Fix PHPStan level 0
jbelien Jul 9, 2023
7c63968
Fix PHPStan level 1
jbelien Jul 9, 2023
ac4b89e
Update phpstan.neon
jbelien Jul 9, 2023
47611f0
Fix PHPStan level 2
jbelien Jul 9, 2023
abacbb7
Update composer.json
jbelien Jul 9, 2023
5b86282
Fix PHPStan level 3
jbelien Jul 9, 2023
44e4ff3
Fix tests
jbelien Jul 9, 2023
29c0f58
Fix PHPStan level 4
jbelien Jul 9, 2023
b37b17a
Update src/Common/Tests/TimedGeocoderTest.php
jbelien Jul 9, 2023
943181f
Update src/Provider/Cache/Tests/ProviderCacheTest.php
jbelien Jul 9, 2023
63e3a39
Update src/Provider/Cache/Tests/ProviderCacheTest.php
jbelien Jul 9, 2023
43d9145
Update src/Provider/GeoIP2/Tests/GeoIP2Test.php
jbelien Jul 9, 2023
8fb105a
Fix PHPStan level 5
jbelien Jul 9, 2023
2ac650a
Merge branch 'master' into phpstan
jbelien Jul 9, 2023
0e789fb
Normalize composer.json
jbelien Jul 9, 2023
8f45744
Rename analyse script
jbelien Jul 9, 2023
7301d3f
Update composer.json
jbelien Jul 9, 2023
ce4171c
Update IntegrationTest
jbelien Jul 9, 2023
6a19f91
Update AlgoliaPlacesTest
jbelien Jul 9, 2023
807bf4c
Update composer.json
jbelien Jul 9, 2023
0bf5e0f
Update RequestInterface vs. getParsedResponse()
jbelien Jul 9, 2023
3ad7f18
Update src/Plugin/PluginProvider.php
jbelien Jul 10, 2023
0bcbc76
Update src/Plugin/PluginProvider.php
jbelien Jul 10, 2023
b3ce75f
Use PHPStan baseline instead of ignore
jbelien Jul 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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