Skip to content

Commit

Permalink
Enable PHPStan Level 6 (#1194)
Browse files Browse the repository at this point in the history
* Update phpstan.neon

* Update YandexTest.php

* Update TomTomTest.php

* Update PickPointTest.php

* Update PickPoint.php

* Update PhotonTest.php

* Update PeliasTest.php

* Update OpenRouteServiceTest.php

* Update OpenCageTest.php

* Update OpenCage.php

* Update NominatimTest.php

* Update MaxMindBinaryTest.php

* Update MaxMindTest.php

* [WIP] Apply PHPStan fixes

* Apply PHPCSFixer fixes

* [WIP] Apply PHPStan fixes

* [WIP] Apply PHPStan fixes

* Revert "[WIP] Apply PHPStan fixes"

This reverts commit 734c5c5.

* [WIP] Apply PHPStan fixes

* [WIP] Apply PHPStan fixes

* Update phpstan-baseline.neon
  • Loading branch information
jbelien authored Jul 31, 2023
1 parent a7d67b7 commit 722fc19
Show file tree
Hide file tree
Showing 111 changed files with 975 additions and 779 deletions.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ parameters:
count: 1
path: src/Common/ProviderAggregator.php

-
message: "#^Method Geocoder\\\\TimedGeocoder\\:\\:__call\\(\\) has parameter \\$args with no value type specified in iterable type array\\.$#"
count: 1
path: src/Common/TimedGeocoder.php

-
message: "#^Method Geocoder\\\\Provider\\\\Cache\\\\ProviderCache\\:\\:__call\\(\\) has parameter \\$args with no value type specified in iterable type array\\.$#"
count: 1
path: src/Provider/Cache/ProviderCache.php

-
message: "#^Parameter \\#1 \\$locations of class Geocoder\\\\Model\\\\AddressCollection constructor expects array\\<Geocoder\\\\Location\\>, array\\<string, string\\> given\\.$#"
count: 2
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
includes:
- phpstan-baseline.neon
parameters:
level: 5
level: 6
paths:
- src
excludePaths:
Expand Down
16 changes: 5 additions & 11 deletions src/Common/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,35 @@

class Assert
{
/**
* @param float $value
*/
public static function latitude($value, string $message = '')
public static function latitude(mixed $value, string $message = ''): void
{
self::float($value, $message);
if ($value < -90 || $value > 90) {
throw new InvalidArgument(sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value));
}
}

/**
* @param float $value
*/
public static function longitude($value, string $message = '')
public static function longitude(mixed $value, string $message = ''): void
{
self::float($value, $message);
if ($value < -180 || $value > 180) {
throw new InvalidArgument(sprintf($message ?: 'Longitude should be between -180 and 180. Got: %s', $value));
}
}

public static function notNull($value, string $message = '')
public static function notNull(mixed $value, string $message = ''): void
{
if (null === $value) {
throw new InvalidArgument(sprintf($message ?: 'Value cannot be null'));
}
}

private static function typeToString($value): string
private static function typeToString(mixed $value): string
{
return is_object($value) ? get_class($value) : gettype($value);
}

private static function float($value, string $message)
private static function float(mixed $value, string $message): void
{
if (!is_float($value)) {
throw new InvalidArgument(sprintf($message ?: 'Expected a float. Got: %s', self::typeToString($value)));
Expand Down
3 changes: 3 additions & 0 deletions src/Common/Dumper/AbstractArrayDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
abstract class AbstractArrayDumper
{
/**
* @return array{type: 'Feature', geometry: array{type: 'Point', coordinates: array{0: float, 1: float}}, properties: array<string, mixed>, bounds?: array{south: float, west: float, north: float, east: float}}
*/
protected function getArray(Location $location): array
{
$properties = array_filter($location->toArray(), function ($value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ interface Dumper
* Dumps an `Location` object as a string representation of
* the implemented format.
*/
public function dump(Location $location);
public function dump(Location $location): mixed;
}
3 changes: 3 additions & 0 deletions src/Common/Dumper/GeoArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
final class GeoArray extends AbstractArrayDumper implements Dumper
{
/**
* @return array{type: 'Feature', geometry: array{type: 'Point', coordinates: array{0: float, 1: float}}, properties: array<string, mixed>, bounds?: array{south: float, west: float, north: float, east: float}}
*/
public function dump(Location $location): array
{
return $this->getArray($location);
Expand Down
7 changes: 5 additions & 2 deletions src/Common/Exception/ProviderNotRegistered.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
final class ProviderNotRegistered extends \RuntimeException implements Exception
{
public static function create(string $providerName, array $registeredProviders = [])
/**
* @param string[] $registeredProviders
*/
public static function create(string $providerName, array $registeredProviders = []): self
{
return new self(sprintf(
'Provider "%s" is not registered, so you cannot use it. Did you forget to register it or made a typo?%s',
Expand All @@ -26,7 +29,7 @@ public static function create(string $providerName, array $registeredProviders =
));
}

public static function noProviderRegistered()
public static function noProviderRegistered(): self
{
return new self('No provider registered.');
}
Expand Down
34 changes: 10 additions & 24 deletions src/Common/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ interface Location
{
/**
* Will always return the coordinates value object.
*
* @return Coordinates|null
*/
public function getCoordinates();
public function getCoordinates(): ?Coordinates;

/**
* Returns the bounds value object.
*
* @return Bounds|null
*/
public function getBounds();
public function getBounds(): ?Bounds;

/**
* Returns the street number value.
Expand All @@ -48,32 +44,24 @@ public function getStreetNumber();

/**
* Returns the street name value.
*
* @return string|null
*/
public function getStreetName();
public function getStreetName(): ?string;

/**
* Returns the city or locality value.
*
* @return string|null
*/
public function getLocality();
public function getLocality(): ?string;

/**
* Returns the postal code or zipcode value.
*
* @return string|null
*/
public function getPostalCode();
public function getPostalCode(): ?string;

/**
* Returns the locality district, or
* sublocality, or neighborhood.
*
* @return string|null
*/
public function getSubLocality();
public function getSubLocality(): ?string;

/**
* Returns the administrative levels.
Expand All @@ -84,22 +72,20 @@ public function getAdminLevels(): AdminLevelCollection;

/**
* Returns the country value object.
*
* @return Country|null
*/
public function getCountry();
public function getCountry(): ?Country;

/**
* Returns the timezone for the Location. The timezone MUST be in the list of supported timezones.
*
* {@link http://php.net/manual/en/timezones.php}
*
* @return string|null
*/
public function getTimezone();
public function getTimezone(): ?string;

/**
* Returns an array with data indexed by name.
*
* @return array<string, mixed>
*/
public function toArray(): array;

Expand Down
22 changes: 12 additions & 10 deletions src/Common/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,37 @@ public function getProvidedBy(): string
return $this->providedBy;
}

public function getCoordinates()
public function getCoordinates(): ?Coordinates
{
return $this->coordinates;
}

public function getBounds()
public function getBounds(): ?Bounds
{
return $this->bounds;
}

public function getStreetNumber()
public function getStreetNumber(): ?string
{
return $this->streetNumber;
}

public function getStreetName()
public function getStreetName(): ?string
{
return $this->streetName;
}

public function getLocality()
public function getLocality(): ?string
{
return $this->locality;
}

public function getPostalCode()
public function getPostalCode(): ?string
{
return $this->postalCode;
}

public function getSubLocality()
public function getSubLocality(): ?string
{
return $this->subLocality;
}
Expand All @@ -145,19 +145,21 @@ public function getAdminLevels(): AdminLevelCollection
return $this->adminLevels;
}

public function getCountry()
public function getCountry(): ?Country
{
return $this->country;
}

public function getTimezone()
public function getTimezone(): ?string
{
return $this->timezone;
}

/**
* Create an Address with an array. Useful for testing.
*
* @param array<string, mixed> $data
*
* @return static
*/
public static function createFromArray(array $data)
Expand Down Expand Up @@ -259,7 +261,7 @@ private static function createCountry($name, $code)
*
* @return Bounds|null
*/
private static function createBounds($south, $west, $north, $east)
private static function createBounds(?float $south, ?float $west, ?float $north, ?float $east)
{
if (null === $south || null === $west || null === $north || null === $east) {
return null;
Expand Down
13 changes: 5 additions & 8 deletions src/Common/Model/AddressBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class AddressBuilder
private $subLocality;

/**
* @var array
* @var AdminLevel[]
*/
private $adminLevels = [];

Expand All @@ -85,7 +85,7 @@ final class AddressBuilder
/**
* A storage for extra parameters.
*
* @var array
* @var array<string, mixed>
*/
private $data = [];

Expand Down Expand Up @@ -210,7 +210,7 @@ public function setSubLocality($subLocality): self
}

/**
* @param array $adminLevels
* @param AdminLevel[] $adminLevels
*/
public function setAdminLevels($adminLevels): self
{
Expand Down Expand Up @@ -249,17 +249,14 @@ public function setTimezone($timezone): self
return $this;
}

public function setValue(string $name, $value): self
public function setValue(string $name, mixed $value): self
{
$this->data[$name] = $value;

return $this;
}

/**
* @param mixed|null $default
*/
public function getValue(string $name, $default = null)
public function getValue(string $name, mixed $default = null): mixed
{
if ($this->hasValue($name)) {
return $this->data[$name];
Expand Down
4 changes: 3 additions & 1 deletion src/Common/Model/AdminLevelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* @author Giorgio Premi <[email protected]>
*
* @phpstan-implements \IteratorAggregate<int, AdminLevel>
*/
final class AdminLevelCollection implements \IteratorAggregate, \Countable
{
Expand Down Expand Up @@ -111,7 +113,7 @@ public function all(): array
/**
* @throws \OutOfBoundsException
*/
private function checkLevel(int $level)
private function checkLevel(int $level): void
{
if ($level <= 0 || $level > self::MAX_LEVEL_DEPTH) {
throw new OutOfBounds(sprintf('Administrative level should be an integer in [1,%d], %d given', self::MAX_LEVEL_DEPTH, $level));
Expand Down
2 changes: 2 additions & 0 deletions src/Common/Model/Bounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public function getEast(): float

/**
* Returns an array with bounds.
*
* @return array{south: float, west: float, north: float, east: float}
*/
public function toArray(): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Common/Model/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function getLongitude(): float

/**
* Returns the coordinates as a tuple.
*
* @return array{float, float}
*/
public function toArray(): array
{
Expand Down
Loading

0 comments on commit 722fc19

Please sign in to comment.