Skip to content

Commit

Permalink
Improve PHPStan type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Nov 14, 2024
1 parent 440822e commit 780a572
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.1.0
------------------

* The PHPDoc type hints have been improved for use with PHPStan.

3.0.0 (2023-12-04)
------------------

Expand Down
9 changes: 6 additions & 3 deletions src/Database/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class Reader implements ProviderInterface
/**
* Constructor.
*
* @param string $filename the path to the GeoIP2 database file
* @param array $locales list of locale codes to use in name property
* from most preferred to least preferred
* @param string $filename the path to the GeoIP2 database file
* @param array<string> $locales list of locale codes to use in name property
* from most preferred to least preferred
*
* @throws InvalidDatabaseException if the database is corrupt or invalid
*/
Expand Down Expand Up @@ -215,6 +215,9 @@ private function flatModelFor(string $class, string $type, string $ipAddress): o
return new $class($record);
}

/**
* @return array{0:array<string, mixed>, 1:int}
*/
private function getRecord(string $class, string $type, string $ipAddress): array
{
if (!str_contains($this->dbType, $type)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Model/AnonymousIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class AnonymousIp implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
*/
public function __construct(array $raw)
{
Expand All @@ -78,6 +80,9 @@ public function __construct(array $raw)
$this->network = Util::cidr($ipAddress, $raw['prefix_len']);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Asn.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Asn implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
*/
public function __construct(array $raw)
{
Expand All @@ -50,6 +52,9 @@ public function __construct(array $raw)
$this->network = Util::cidr($ipAddress, $raw['prefix_len']);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Model/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class City extends Country

/**
* @ignore
*
* @param array<string, mixed> $raw
* @param list<string> $locales
*/
public function __construct(array $raw, array $locales = ['en'])
{
Expand Down Expand Up @@ -90,6 +93,9 @@ public function __construct(array $raw, array $locales = ['en'])
$this->subdivisions = $subdivisions;
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = parent::jsonSerialize();
Expand Down
5 changes: 5 additions & 0 deletions src/Model/ConnectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ConnectionType implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
*/
public function __construct(array $raw)
{
Expand All @@ -42,6 +44,9 @@ public function __construct(array $raw)
$this->network = Util::cidr($ipAddress, $raw['prefix_len']);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Country implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
* @param list<string> $locales
*/
public function __construct(array $raw, array $locales = ['en'])
{
Expand All @@ -81,6 +84,9 @@ public function __construct(array $raw, array $locales = ['en'])
$this->traits = new Traits($raw['traits'] ?? []);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Domain implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
*/
public function __construct(array $raw)
{
Expand All @@ -42,6 +44,9 @@ public function __construct(array $raw)
$this->network = Util::cidr($ipAddress, $raw['prefix_len']);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Isp.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Isp implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $raw
*/
public function __construct(array $raw)
{
Expand All @@ -81,6 +83,9 @@ public function __construct(array $raw)
$this->network = Util::cidr($ipAddress, $raw['prefix_len']);
}

/**
* @return array<string, mixed>|null
*/
public function jsonSerialize(): ?array
{
$js = [];
Expand Down
12 changes: 9 additions & 3 deletions src/Record/AbstractNamedRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ abstract class AbstractNamedRecord implements \JsonSerializable
public readonly ?string $name;

/**
* @var array An array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* services and databases.
* @var array<string, string> An array map where the keys are locale codes
* and the values are names. This attribute is returned by all location
* services and databases.
*/
public readonly array $names;

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -37,6 +40,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->name = null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Record/AbstractPlaceRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class AbstractPlaceRecord extends AbstractNamedRecord

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -30,6 +33,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->geonameId = $record['geoname_id'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
Expand Down
6 changes: 6 additions & 0 deletions src/Record/Continent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Continent extends AbstractNamedRecord

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -35,6 +38,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->geonameId = $record['geoname_id'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
Expand Down
6 changes: 6 additions & 0 deletions src/Record/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Country extends AbstractPlaceRecord

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -36,6 +39,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->isoCode = $record['iso_code'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
Expand Down
8 changes: 8 additions & 0 deletions src/Record/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Location implements \JsonSerializable
*/
public readonly ?string $timeZone;

/**
* @ignore
*
* @param array<string, mixed> $record
*/
public function __construct(array $record)
{
$this->averageIncome = $record['average_income'] ?? null;
Expand All @@ -75,6 +80,9 @@ public function __construct(array $record)
$this->timeZone = $record['time_zone'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = [];
Expand Down
8 changes: 8 additions & 0 deletions src/Record/MaxMind.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ class MaxMind implements \JsonSerializable
*/
public readonly ?int $queriesRemaining;

/**
* @ignore
*
* @param array<string, mixed> $record
*/
public function __construct(array $record)
{
$this->queriesRemaining = $record['queries_remaining'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Record/Postal.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ class Postal implements \JsonSerializable

/**
* @ignore
*
* @param array<string, mixed> $record
*/
public function __construct(array $record)
{
$this->code = $record['code'] ?? null;
$this->confidence = $record['confidence'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = [];
Expand Down
6 changes: 6 additions & 0 deletions src/Record/RepresentedCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class RepresentedCountry extends Country

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -30,6 +33,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->type = $record['type'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
Expand Down
6 changes: 6 additions & 0 deletions src/Record/Subdivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Subdivision extends AbstractPlaceRecord

/**
* @ignore
*
* @param array<string, mixed> $record
* @param list<string> $locales
*/
public function __construct(array $record, array $locales = ['en'])
{
Expand All @@ -30,6 +33,9 @@ public function __construct(array $record, array $locales = ['en'])
$this->isoCode = $record['iso_code'] ?? null;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
Expand Down
8 changes: 8 additions & 0 deletions src/Record/Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ class Traits implements \JsonSerializable
*/
public readonly ?string $userType;

/**
* @ignore
*
* @param array<string, mixed> $record
*/
public function __construct(array $record)
{
$this->autonomousSystemNumber = $record['autonomous_system_number'] ?? null;
Expand Down Expand Up @@ -226,6 +231,9 @@ public function __construct(array $record)
}
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$js = [];
Expand Down
Loading

0 comments on commit 780a572

Please sign in to comment.