From 45b88f7cc35f7200e5eca1c7dc02790e31c6e40a Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 2 Jan 2024 09:06:52 -0800 Subject: [PATCH 1/4] Run new php-cs-fixer --- examples/benchmark.php | 3 +- src/Database/Reader.php | 68 ++++++------- src/Model/City.php | 25 +++-- src/Model/Country.php | 38 ++++--- src/ProviderInterface.php | 4 +- src/WebService/Client.php | 105 ++++++++++---------- tests/GeoIp2/Test/Database/ReaderTest.php | 3 +- tests/GeoIp2/Test/WebService/ClientTest.php | 41 ++++---- 8 files changed, 155 insertions(+), 132 deletions(-) diff --git a/examples/benchmark.php b/examples/benchmark.php index a735a78b..eb657d6f 100644 --- a/examples/benchmark.php +++ b/examples/benchmark.php @@ -3,6 +3,7 @@ require __DIR__ . '/../vendor/autoload.php'; use GeoIp2\Database\Reader; +use GeoIp2\Exception\AddressNotFoundException; srand(0); @@ -14,7 +15,7 @@ try { $t = $reader->city($ip); - } catch (\GeoIp2\Exception\AddressNotFoundException $e) { + } catch (AddressNotFoundException $e) { } if ($i % 10000 === 0) { echo $i . ' ' . $ip . "\n"; diff --git a/src/Database/Reader.php b/src/Database/Reader.php index 1570afb8..224d401a 100644 --- a/src/Database/Reader.php +++ b/src/Database/Reader.php @@ -59,8 +59,8 @@ class Reader implements ProviderInterface * @param array $locales list of locale codes to use in name property * from most preferred to least preferred * - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function __construct( string $filename, @@ -76,10 +76,10 @@ public function __construct( * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function city(string $ipAddress): City { @@ -91,10 +91,10 @@ public function city(string $ipAddress): City * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function country(string $ipAddress): Country { @@ -106,10 +106,10 @@ public function country(string $ipAddress): Country * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function anonymousIp(string $ipAddress): AnonymousIp { @@ -125,10 +125,10 @@ public function anonymousIp(string $ipAddress): AnonymousIp * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function asn(string $ipAddress): Asn { @@ -144,10 +144,10 @@ public function asn(string $ipAddress): Asn * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function connectionType(string $ipAddress): ConnectionType { @@ -163,10 +163,10 @@ public function connectionType(string $ipAddress): ConnectionType * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function domain(string $ipAddress): Domain { @@ -182,10 +182,10 @@ public function domain(string $ipAddress): Domain * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function enterprise(string $ipAddress): Enterprise { @@ -197,10 +197,10 @@ public function enterprise(string $ipAddress): Enterprise * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address is - * not in the database - * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is + * not in the database + * @throws InvalidDatabaseException if the database + * is corrupt or invalid */ public function isp(string $ipAddress): Isp { diff --git a/src/Model/City.php b/src/Model/City.php index f89ccfe4..dbd8751f 100644 --- a/src/Model/City.php +++ b/src/Model/City.php @@ -4,6 +4,11 @@ namespace GeoIp2\Model; +use GeoIp2\Record\City; +use GeoIp2\Record\Location; +use GeoIp2\Record\Postal; +use GeoIp2\Record\Subdivision; + /** * Model class for the data returned by City Plus web service and City * database. @@ -14,28 +19,28 @@ class City extends Country { /** - * @var \GeoIp2\Record\City city data for the requested IP - * address + * @var City city data for the requested IP + * address */ public readonly \GeoIp2\Record\City $city; /** - * @var \GeoIp2\Record\Location location data for the - * requested IP address + * @var Location location data for the + * requested IP address */ public readonly \GeoIp2\Record\Location $location; /** - * @var \GeoIp2\Record\Subdivision An object - * representing the most specific subdivision returned. If the response - * did not contain any subdivisions, this method returns an empty - * \GeoIp2\Record\Subdivision object. + * @var Subdivision An object + * representing the most specific subdivision returned. If the response + * did not contain any subdivisions, this method returns an empty + * \GeoIp2\Record\Subdivision object. */ public readonly \GeoIp2\Record\Subdivision $mostSpecificSubdivision; /** - * @var \GeoIp2\Record\Postal postal data for the - * requested IP address + * @var Postal postal data for the + * requested IP address */ public readonly \GeoIp2\Record\Postal $postal; diff --git a/src/Model/Country.php b/src/Model/Country.php index 0fae2db7..3bd56368 100644 --- a/src/Model/Country.php +++ b/src/Model/Country.php @@ -4,6 +4,12 @@ namespace GeoIp2\Model; +use GeoIp2\Record\Continent; +use GeoIp2\Record\Country; +use GeoIp2\Record\MaxMind; +use GeoIp2\Record\RepresentedCountry; +use GeoIp2\Record\Traits; + /** * Model class for the data returned by GeoIP2 Country web service and database. * @@ -12,42 +18,42 @@ class Country implements \JsonSerializable { /** - * @var \GeoIp2\Record\Continent continent data for the - * requested IP address + * @var Continent continent data for the + * requested IP address */ public readonly \GeoIp2\Record\Continent $continent; /** - * @var \GeoIp2\Record\Country Country data for the requested - * IP address. This object represents the country where MaxMind believes the - * end user is located. + * @var Country Country data for the requested + * IP address. This object represents the country where MaxMind believes the + * end user is located. */ public readonly \GeoIp2\Record\Country $country; /** - * @var \GeoIp2\Record\MaxMind data related to your MaxMind - * account + * @var MaxMind data related to your MaxMind + * account */ public readonly \GeoIp2\Record\MaxMind $maxmind; /** - * @var \GeoIp2\Record\Country Registered country - * data for the requested IP address. This record represents the country - * where the ISP has registered a given IP block and may differ from the - * user's country. + * @var Country Registered country + * data for the requested IP address. This record represents the country + * where the ISP has registered a given IP block and may differ from the + * user's country. */ public readonly \GeoIp2\Record\Country $registeredCountry; /** - * @var \GeoIp2\Record\RepresentedCountry * Represented country data for the requested IP address. The represented - * country is used for things like military bases. It is only present when - * the represented country differs from the country. + * @var RepresentedCountry * Represented country data for the requested IP address. The represented + * country is used for things like military bases. It is only present when + * the represented country differs from the country. */ public readonly \GeoIp2\Record\RepresentedCountry $representedCountry; /** - * @var \GeoIp2\Record\Traits data for the traits of the - * requested IP address + * @var Traits data for the traits of the + * requested IP address */ public readonly \GeoIp2\Record\Traits $traits; diff --git a/src/ProviderInterface.php b/src/ProviderInterface.php index 7d148912..8f40bee5 100644 --- a/src/ProviderInterface.php +++ b/src/ProviderInterface.php @@ -9,14 +9,14 @@ interface ProviderInterface /** * @param string $ipAddress an IPv4 or IPv6 address to lookup * - * @return \GeoIp2\Model\Country a Country model for the requested IP address + * @return Model\Country a Country model for the requested IP address */ public function country(string $ipAddress): Model\Country; /** * @param string $ipAddress an IPv4 or IPv6 address to lookup * - * @return \GeoIp2\Model\City a City model for the requested IP address + * @return Model\City a City model for the requested IP address */ public function city(string $ipAddress): Model\City; } diff --git a/src/WebService/Client.php b/src/WebService/Client.php index 1e095419..ccdc4049 100644 --- a/src/WebService/Client.php +++ b/src/WebService/Client.php @@ -14,6 +14,9 @@ use GeoIp2\Model\Country; use GeoIp2\Model\Insights; use GeoIp2\ProviderInterface; +use MaxMind\Exception\InsufficientFundsException; +use MaxMind\Exception\IpAddressNotFoundException; +use MaxMind\Exception\WebServiceException; use MaxMind\WebService\Client as WsClient; /** @@ -116,24 +119,24 @@ private function userAgent(): string * address is provided, the address that the web service is called * from will be used. * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address you - * provided is not in our database (e.g., a private address). - * @throws \GeoIp2\Exception\AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out - * of queries - * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is + * @throws AddressNotFoundException if the address you + * provided is not in our database (e.g., a private address). + * @throws AuthenticationException if there is a problem + * with the account ID or license key that you provided + * @throws OutOfQueriesException if your account is out + * of queries + * @throws InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an issue * with this API. Please report the error to MaxMind. - * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned. - * This could indicate a problem with the connection between - * your server and the web service or that the web service - * returned an invalid document or 500 error code - * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent - * class to the above exceptions. It will be thrown directly - * if a 200 status code is returned but the body is invalid. - * @throws \InvalidArgumentException if something other than a single IP address or "me" is - * passed to the method + * @throws HttpException if an unexpected HTTP error code or message was returned. + * This could indicate a problem with the connection between + * your server and the web service or that the web service + * returned an invalid document or 500 error code + * @throws GeoIp2Exception This serves as the parent + * class to the above exceptions. It will be thrown directly + * if a 200 status code is returned but the body is invalid. + * @throws \InvalidArgumentException if something other than a single IP address or "me" is + * passed to the method */ public function city(string $ipAddress = 'me'): City { @@ -148,24 +151,24 @@ public function city(string $ipAddress = 'me'): City * address is provided, the address that the web service is called * from will be used. * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address you provided is not in our database (e.g., - * a private address). - * @throws \GeoIp2\Exception\AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out of queries - * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is + * @throws AddressNotFoundException if the address you provided is not in our database (e.g., + * a private address). + * @throws AuthenticationException if there is a problem + * with the account ID or license key that you provided + * @throws OutOfQueriesException if your account is out of queries + * @throws InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an * issue with this API. Please report the error to MaxMind. - * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error - * code or message was returned. This could indicate a problem - * with the connection between your server and the web service - * or that the web service returned an invalid document or 500 - * error code. - * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent class to the above exceptions. It - * will be thrown directly if a 200 status code is returned but - * the body is invalid. - * @throws \InvalidArgumentException if something other than a single IP address or "me" is - * passed to the method + * @throws HttpException if an unexpected HTTP error + * code or message was returned. This could indicate a problem + * with the connection between your server and the web service + * or that the web service returned an invalid document or 500 + * error code. + * @throws GeoIp2Exception This serves as the parent class to the above exceptions. It + * will be thrown directly if a 200 status code is returned but + * the body is invalid. + * @throws \InvalidArgumentException if something other than a single IP address or "me" is + * passed to the method */ public function country(string $ipAddress = 'me'): Country { @@ -180,24 +183,24 @@ public function country(string $ipAddress = 'me'): Country * address is provided, the address that the web service is called * from will be used. * - * @throws \GeoIp2\Exception\AddressNotFoundException if the address you - * provided is not in our database (e.g., a private address). - * @throws \GeoIp2\Exception\AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out - * of queries - * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is + * @throws AddressNotFoundException if the address you + * provided is not in our database (e.g., a private address). + * @throws AuthenticationException if there is a problem + * with the account ID or license key that you provided + * @throws OutOfQueriesException if your account is out + * of queries + * @throws InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an * issue with this API. Please report the error to MaxMind. - * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned. - * This could indicate a problem with the connection between - * your server and the web service or that the web service - * returned an invalid document or 500 error code - * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent - * class to the above exceptions. It will be thrown directly - * if a 200 status code is returned but the body is invalid. - * @throws \InvalidArgumentException if something other than a single IP address or "me" is - * passed to the method + * @throws HttpException if an unexpected HTTP error code or message was returned. + * This could indicate a problem with the connection between + * your server and the web service or that the web service + * returned an invalid document or 500 error code + * @throws GeoIp2Exception This serves as the parent + * class to the above exceptions. It will be thrown directly + * if a 200 status code is returned but the body is invalid. + * @throws \InvalidArgumentException if something other than a single IP address or "me" is + * passed to the method */ public function insights(string $ipAddress = 'me'): Insights { @@ -217,7 +220,7 @@ private function responseFor(string $endpoint, string $class, string $ipAddress) try { $service = (new \ReflectionClass($class))->getShortName(); $body = $this->client->get('GeoIP2 ' . $service, $path); - } catch (\MaxMind\Exception\IpAddressNotFoundException $ex) { + } catch (IpAddressNotFoundException $ex) { throw new AddressNotFoundException( $ex->getMessage(), $ex->getStatusCode(), @@ -229,7 +232,7 @@ private function responseFor(string $endpoint, string $class, string $ipAddress) $ex->getStatusCode(), $ex ); - } catch (\MaxMind\Exception\InsufficientFundsException $ex) { + } catch (InsufficientFundsException $ex) { throw new OutOfQueriesException( $ex->getMessage(), $ex->getStatusCode(), @@ -250,7 +253,7 @@ private function responseFor(string $endpoint, string $class, string $ipAddress) $ex->getUri(), $ex ); - } catch (\MaxMind\Exception\WebServiceException $ex) { + } catch (WebServiceException $ex) { throw new GeoIp2Exception( $ex->getMessage(), $ex->getCode(), diff --git a/tests/GeoIp2/Test/Database/ReaderTest.php b/tests/GeoIp2/Test/Database/ReaderTest.php index 9ab8b880..eea3a849 100644 --- a/tests/GeoIp2/Test/Database/ReaderTest.php +++ b/tests/GeoIp2/Test/Database/ReaderTest.php @@ -5,6 +5,7 @@ namespace GeoIp2\Test\Database; use GeoIp2\Database\Reader; +use GeoIp2\Exception\AddressNotFoundException; use PHPUnit\Framework\TestCase; /** @@ -76,7 +77,7 @@ public function testIsInEuropeanUnion(string $type, string $method): void public function testUnknownAddress(): void { - $this->expectException(\GeoIp2\Exception\AddressNotFoundException::class); + $this->expectException(AddressNotFoundException::class); $this->expectExceptionMessage('The address 10.10.10.10 is not in the database.'); $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb'); diff --git a/tests/GeoIp2/Test/WebService/ClientTest.php b/tests/GeoIp2/Test/WebService/ClientTest.php index 30225dec..f994d302 100644 --- a/tests/GeoIp2/Test/WebService/ClientTest.php +++ b/tests/GeoIp2/Test/WebService/ClientTest.php @@ -5,7 +5,14 @@ namespace GeoIp2\Test\WebService; use Composer\CaBundle\CaBundle; +use GeoIp2\Exception\AddressNotFoundException; +use GeoIp2\Exception\AuthenticationException; +use GeoIp2\Exception\GeoIp2Exception; +use GeoIp2\Exception\HttpException; +use GeoIp2\Exception\OutOfQueriesException; +use GeoIp2\WebService\Client; use MaxMind\WebService\Client as WsClient; +use MaxMind\WebService\Http\Request; use PHPUnit\Framework\TestCase; /** @@ -311,7 +318,7 @@ public function testMe(): void public function testNoBodyException(): void { - $this->expectException(\GeoIp2\Exception\GeoIp2Exception::class); + $this->expectException(GeoIp2Exception::class); $this->expectExceptionMessage('Received a 200 response for GeoIP2 Country but did not receive a HTTP body.'); $this->makeRequest('Country', '1.2.3.5'); @@ -319,7 +326,7 @@ public function testNoBodyException(): void public function testBadBodyException(): void { - $this->expectException(\GeoIp2\Exception\GeoIp2Exception::class); + $this->expectException(GeoIp2Exception::class); $this->expectExceptionMessage('Received a 200 response for GeoIP2 Country but could not decode the response as JSON:'); $this->makeRequest('Country', '2.2.3.5'); @@ -335,7 +342,7 @@ public function testInvalidIPException(): void public function testNoErrorBodyIPException(): void { - $this->expectException(\GeoIp2\Exception\HttpException::class); + $this->expectException(HttpException::class); $this->expectExceptionCode(400); $this->expectExceptionMessage('with no body'); @@ -344,7 +351,7 @@ public function testNoErrorBodyIPException(): void public function testWeirdErrorBodyIPException(): void { - $this->expectException(\GeoIp2\Exception\GeoIp2Exception::class); + $this->expectException(GeoIp2Exception::class); $this->expectExceptionMessage('Error response contains JSON but it does not specify code or error keys: {"weird":42}'); $this->makeRequest('Country', '1.2.3.8'); @@ -352,7 +359,7 @@ public function testWeirdErrorBodyIPException(): void public function testInvalidErrorBodyIPException(): void { - $this->expectException(\GeoIp2\Exception\HttpException::class); + $this->expectException(HttpException::class); $this->expectExceptionCode(400); $this->expectExceptionMessage('Received a 400 error for GeoIP2 Country but could not decode the response as JSON: Syntax error. Body: { invalid: }'); @@ -361,7 +368,7 @@ public function testInvalidErrorBodyIPException(): void public function test500PException(): void { - $this->expectException(\GeoIp2\Exception\HttpException::class); + $this->expectException(HttpException::class); $this->expectExceptionCode(500); $this->expectExceptionMessage('Received a server error (500)'); @@ -370,7 +377,7 @@ public function test500PException(): void public function test3xxException(): void { - $this->expectException(\GeoIp2\Exception\HttpException::class); + $this->expectException(HttpException::class); $this->expectExceptionCode(300); $this->expectExceptionMessage('Received an unexpected HTTP status (300) for GeoIP2 Country'); @@ -379,7 +386,7 @@ public function test3xxException(): void public function test406Exception(): void { - $this->expectException(\GeoIp2\Exception\HttpException::class); + $this->expectException(HttpException::class); $this->expectExceptionCode(406); $this->expectExceptionMessage('Received a 406 error for GeoIP2 Country with the following body: Cannot satisfy your Accept-Charset requirements'); @@ -388,7 +395,7 @@ public function test406Exception(): void public function testAddressNotFoundException(): void { - $this->expectException(\GeoIp2\Exception\AddressNotFoundException::class); + $this->expectException(AddressNotFoundException::class); $this->expectExceptionMessage('The address "1.2.3.13" is not in our database.'); $this->makeRequest('Country', '1.2.3.13'); @@ -396,7 +403,7 @@ public function testAddressNotFoundException(): void public function testAddressReservedException(): void { - $this->expectException(\GeoIp2\Exception\AddressNotFoundException::class); + $this->expectException(AddressNotFoundException::class); $this->expectExceptionMessage('The address "1.2.3.14" is a private address.'); $this->makeRequest('Country', '1.2.3.14'); @@ -404,7 +411,7 @@ public function testAddressReservedException(): void public function testAuthorizationException(): void { - $this->expectException(\GeoIp2\Exception\AuthenticationException::class); + $this->expectException(AuthenticationException::class); $this->expectExceptionMessage('A user ID and license key are required to use this service'); $this->makeRequest('Country', '1.2.3.15'); @@ -412,7 +419,7 @@ public function testAuthorizationException(): void public function testMissingLicenseKeyException(): void { - $this->expectException(\GeoIp2\Exception\AuthenticationException::class); + $this->expectException(AuthenticationException::class); $this->expectExceptionMessage('A license key is required to use this service'); $this->makeRequest('Country', '1.2.3.16'); @@ -420,7 +427,7 @@ public function testMissingLicenseKeyException(): void public function testMissingUserIdException(): void { - $this->expectException(\GeoIp2\Exception\AuthenticationException::class); + $this->expectException(AuthenticationException::class); $this->expectExceptionMessage('A user ID is required to use this service'); $this->makeRequest('Country', '1.2.3.17'); @@ -428,7 +435,7 @@ public function testMissingUserIdException(): void public function testMissingAccountIdException(): void { - $this->expectException(\GeoIp2\Exception\AuthenticationException::class); + $this->expectException(AuthenticationException::class); $this->expectExceptionMessage('A account ID is required to use this service'); $this->makeRequest('Country', '1.2.3.19'); @@ -436,7 +443,7 @@ public function testMissingAccountIdException(): void public function testOutOfQueriesException(): void { - $this->expectException(\GeoIp2\Exception\OutOfQueriesException::class); + $this->expectException(OutOfQueriesException::class); $this->expectExceptionMessage('The license key you have provided is out of queries.'); $this->makeRequest('Country', '1.2.3.18'); @@ -499,7 +506,7 @@ private function makeRequest( = $this->getResponse($service, $ipAddress); $stub = $this->createMock( - \MaxMind\WebService\Http\Request::class + Request::class ); $contentType = isset($headers['Content-Type']) ? $headers['Content-Type'] @@ -534,7 +541,7 @@ private function makeRequest( $this->equalTo( [ 'headers' => $headers, - 'userAgent' => 'GeoIP2-API/' . \GeoIp2\WebService\Client::VERSION + 'userAgent' => 'GeoIP2-API/' . Client::VERSION . ' MaxMind-WS-API/' . WsClient::VERSION . ' PHP/' . \PHP_VERSION . ' curl/' . $curlVersion['version'], From ffe95d46ed9e0521cc7fde447d631015ac5661e5 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 2 Jan 2024 09:17:39 -0800 Subject: [PATCH 2/4] Reflow some text --- src/Database/Reader.php | 51 +++++++++++++-------------------------- src/Model/City.php | 29 ++++++++++++---------- src/Model/Country.php | 30 +++++++++++------------ src/WebService/Client.php | 49 ++++++++++++++++++------------------- 4 files changed, 70 insertions(+), 89 deletions(-) diff --git a/src/Database/Reader.php b/src/Database/Reader.php index 224d401a..3ee8dc2c 100644 --- a/src/Database/Reader.php +++ b/src/Database/Reader.php @@ -59,8 +59,7 @@ class Reader implements ProviderInterface * @param array $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 + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function __construct( string $filename, @@ -76,10 +75,8 @@ public function __construct( * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function city(string $ipAddress): City { @@ -91,10 +88,8 @@ public function city(string $ipAddress): City * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function country(string $ipAddress): Country { @@ -106,10 +101,8 @@ public function country(string $ipAddress): Country * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function anonymousIp(string $ipAddress): AnonymousIp { @@ -125,10 +118,8 @@ public function anonymousIp(string $ipAddress): AnonymousIp * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function asn(string $ipAddress): Asn { @@ -144,10 +135,8 @@ public function asn(string $ipAddress): Asn * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function connectionType(string $ipAddress): ConnectionType { @@ -163,10 +152,8 @@ public function connectionType(string $ipAddress): ConnectionType * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function domain(string $ipAddress): Domain { @@ -182,10 +169,8 @@ public function domain(string $ipAddress): Domain * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function enterprise(string $ipAddress): Enterprise { @@ -197,10 +182,8 @@ public function enterprise(string $ipAddress): Enterprise * * @param string $ipAddress an IPv4 or IPv6 address as a string * - * @throws AddressNotFoundException if the address is - * not in the database - * @throws InvalidDatabaseException if the database - * is corrupt or invalid + * @throws AddressNotFoundException if the address is not in the database + * @throws InvalidDatabaseException if the database is corrupt or invalid */ public function isp(string $ipAddress): Isp { diff --git a/src/Model/City.php b/src/Model/City.php index dbd8751f..1906a2f9 100644 --- a/src/Model/City.php +++ b/src/Model/City.php @@ -19,21 +19,19 @@ class City extends Country { /** - * @var City city data for the requested IP - * address + * @var City city data for the requested IP address */ public readonly \GeoIp2\Record\City $city; /** - * @var Location location data for the - * requested IP address + * @var Location location data for the requested IP address */ public readonly \GeoIp2\Record\Location $location; /** - * @var Subdivision An object - * representing the most specific subdivision returned. If the response - * did not contain any subdivisions, this method returns an empty + * @var Subdivision An object representing the most specific subdivision + * returned. If the response did not contain any + * subdivisions, this method returns an empty * \GeoIp2\Record\Subdivision object. */ public readonly \GeoIp2\Record\Subdivision $mostSpecificSubdivision; @@ -46,12 +44,17 @@ class City extends Country /** * @var array<\GeoIp2\Record\Subdivision> An array of \GeoIp2\Record\Subdivision - * objects representing the country subdivisions for the requested IP - * address. The number and type of subdivisions varies by country, but a - * subdivision is typically a state, province, county, etc. Subdivisions - * are ordered from most general (largest) to most specific (smallest). - * If the response did not contain any subdivisions, this method returns - * an empty array. + * objects representing the country + * subdivisions for the requested IP + * address. The number and type of + * subdivisions varies by country, + * but a subdivision is typically a + * state, province, county, etc. + * Subdivisions are ordered from most + * general (largest) to most specific + * (smallest). If the response did + * not contain any subdivisions, this + * method returns an empty array. */ public readonly array $subdivisions; diff --git a/src/Model/Country.php b/src/Model/Country.php index 3bd56368..f17bfd66 100644 --- a/src/Model/Country.php +++ b/src/Model/Country.php @@ -18,42 +18,40 @@ class Country implements \JsonSerializable { /** - * @var Continent continent data for the - * requested IP address + * @var Continent continent data for the requested IP address */ public readonly \GeoIp2\Record\Continent $continent; /** - * @var Country Country data for the requested - * IP address. This object represents the country where MaxMind believes the - * end user is located. + * @var Country Country data for the requested IP address. This object + * represents the country where MaxMind believes the end user + * is located. */ public readonly \GeoIp2\Record\Country $country; /** - * @var MaxMind data related to your MaxMind - * account + * @var MaxMind data related to your MaxMind account */ public readonly \GeoIp2\Record\MaxMind $maxmind; /** - * @var Country Registered country - * data for the requested IP address. This record represents the country - * where the ISP has registered a given IP block and may differ from the - * user's country. + * @var Country Registered country data for the requested IP address. This + * record represents the country where the ISP has registered + * a given IP block and may differ from the user's country. */ public readonly \GeoIp2\Record\Country $registeredCountry; /** - * @var RepresentedCountry * Represented country data for the requested IP address. The represented - * country is used for things like military bases. It is only present when - * the represented country differs from the country. + * @var RepresentedCountry Represented country data for the requested IP + * address. The represented country is used for + * things like military bases. It is only present + * when the represented country differs from the + * country. */ public readonly \GeoIp2\Record\RepresentedCountry $representedCountry; /** - * @var Traits data for the traits of the - * requested IP address + * @var Traits data for the traits of the requested IP address */ public readonly \GeoIp2\Record\Traits $traits; diff --git a/src/WebService/Client.php b/src/WebService/Client.php index ccdc4049..f74bb513 100644 --- a/src/WebService/Client.php +++ b/src/WebService/Client.php @@ -119,15 +119,14 @@ private function userAgent(): string * address is provided, the address that the web service is called * from will be used. * - * @throws AddressNotFoundException if the address you - * provided is not in our database (e.g., a private address). - * @throws AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws OutOfQueriesException if your account is out - * of queries - * @throws InvalidRequestException} if your request was received by the web service but is - * invalid for some other reason. This may indicate an issue - * with this API. Please report the error to MaxMind. + * @throws AddressNotFoundException if the address you provided is not in our database (e.g., + * a private address). + * @throws AuthenticationException if there is a problem with the account ID or license key + * that you provided + * @throws OutOfQueriesException if your account is out of queries + * @throws InvalidRequestException if your request was received by the web service but is + * invalid for some other reason. This may indicate an issue + * with this API. Please report the error to MaxMind. * @throws HttpException if an unexpected HTTP error code or message was returned. * This could indicate a problem with the connection between * your server and the web service or that the web service @@ -151,14 +150,13 @@ public function city(string $ipAddress = 'me'): City * address is provided, the address that the web service is called * from will be used. * - * @throws AddressNotFoundException if the address you provided is not in our database (e.g., - * a private address). - * @throws AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws OutOfQueriesException if your account is out of queries - * @throws InvalidRequestException} if your request was received by the web service but is - * invalid for some other reason. This may indicate an - * issue with this API. Please report the error to MaxMind. + * @throws AddressNotFoundException if the address you provided is not in our database (e.g., + * a private address). + * @throws AuthenticationException if there is a problem with the account ID or license key that you provided + * @throws OutOfQueriesException if your account is out of queries + * @throws InvalidRequestException if your request was received by the web service but is + * invalid for some other reason. This may indicate an + * issue with this API. Please report the error to MaxMind. * @throws HttpException if an unexpected HTTP error * code or message was returned. This could indicate a problem * with the connection between your server and the web service @@ -183,15 +181,14 @@ public function country(string $ipAddress = 'me'): Country * address is provided, the address that the web service is called * from will be used. * - * @throws AddressNotFoundException if the address you - * provided is not in our database (e.g., a private address). - * @throws AuthenticationException if there is a problem - * with the account ID or license key that you provided - * @throws OutOfQueriesException if your account is out - * of queries - * @throws InvalidRequestException} if your request was received by the web service but is - * invalid for some other reason. This may indicate an - * issue with this API. Please report the error to MaxMind. + * @throws AddressNotFoundException if the address you provided is not in our database (e.g., + * a private address). + * @throws AuthenticationException if there is a problem with the account ID or license key + * that you provided + * @throws OutOfQueriesException if your account is out of queries + * @throws InvalidRequestException if your request was received by the web service but is + * invalid for some other reason. This may indicate an + * issue with this API. Please report the error to MaxMind. * @throws HttpException if an unexpected HTTP error code or message was returned. * This could indicate a problem with the connection between * your server and the web service or that the web service From f545fe9d8131a6c54e4d7bad99c7fb31b2c5de52 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 5 Jan 2024 07:35:13 -0800 Subject: [PATCH 3/4] Work around php-cs-fixer breakage https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7642 --- src/Model/City.php | 8 ++++---- src/Model/Country.php | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Model/City.php b/src/Model/City.php index 1906a2f9..8c3ee176 100644 --- a/src/Model/City.php +++ b/src/Model/City.php @@ -4,7 +4,7 @@ namespace GeoIp2\Model; -use GeoIp2\Record\City; +use GeoIp2\Record\City as CityRecord; use GeoIp2\Record\Location; use GeoIp2\Record\Postal; use GeoIp2\Record\Subdivision; @@ -19,9 +19,9 @@ class City extends Country { /** - * @var City city data for the requested IP address + * @var CityRecord city data for the requested IP address */ - public readonly \GeoIp2\Record\City $city; + public readonly CityRecord $city; /** * @var Location location data for the requested IP address @@ -65,7 +65,7 @@ public function __construct(array $raw, array $locales = ['en']) { parent::__construct($raw, $locales); - $this->city = new \GeoIp2\Record\City($raw['city'] ?? [], $locales); + $this->city = new CityRecord($raw['city'] ?? [], $locales); $this->location = new \GeoIp2\Record\Location($raw['location'] ?? []); $this->postal = new \GeoIp2\Record\Postal($raw['postal'] ?? []); diff --git a/src/Model/Country.php b/src/Model/Country.php index f17bfd66..38dc0c86 100644 --- a/src/Model/Country.php +++ b/src/Model/Country.php @@ -5,7 +5,7 @@ namespace GeoIp2\Model; use GeoIp2\Record\Continent; -use GeoIp2\Record\Country; +use GeoIp2\Record\Country as CountryRecord; use GeoIp2\Record\MaxMind; use GeoIp2\Record\RepresentedCountry; use GeoIp2\Record\Traits; @@ -23,11 +23,11 @@ class Country implements \JsonSerializable public readonly \GeoIp2\Record\Continent $continent; /** - * @var Country Country data for the requested IP address. This object - * represents the country where MaxMind believes the end user - * is located. + * @var CountryRecord Country data for the requested IP address. This + * object represents the country where MaxMind believes + * the end user is located. */ - public readonly \GeoIp2\Record\Country $country; + public readonly CountryRecord $country; /** * @var MaxMind data related to your MaxMind account @@ -35,11 +35,12 @@ class Country implements \JsonSerializable public readonly \GeoIp2\Record\MaxMind $maxmind; /** - * @var Country Registered country data for the requested IP address. This - * record represents the country where the ISP has registered - * a given IP block and may differ from the user's country. + * @var CountryRecord Registered country data for the requested IP address. + * This record represents the country where the ISP has + * registered a given IP block and may differ from the + * user's country. */ - public readonly \GeoIp2\Record\Country $registeredCountry; + public readonly CountryRecord $registeredCountry; /** * @var RepresentedCountry Represented country data for the requested IP @@ -64,12 +65,12 @@ public function __construct(array $raw, array $locales = ['en']) $raw['continent'] ?? [], $locales ); - $this->country = new \GeoIp2\Record\Country( + $this->country = new CountryRecord( $raw['country'] ?? [], $locales ); $this->maxmind = new \GeoIp2\Record\MaxMind($raw['maxmind'] ?? []); - $this->registeredCountry = new \GeoIp2\Record\Country( + $this->registeredCountry = new CountryRecord( $raw['registered_country'] ?? [], $locales ); From 0db2cc8dc2b8d33b2cd03f670aa8afce0feaa523 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 5 Jan 2024 07:36:09 -0800 Subject: [PATCH 4/4] Run latest php-cs-fixer --- src/Database/Reader.php | 3 ++- src/Model/City.php | 8 ++++---- src/Model/Country.php | 8 ++++---- tests/GeoIp2/Test/WebService/ClientTest.php | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Database/Reader.php b/src/Database/Reader.php index 3ee8dc2c..36da6583 100644 --- a/src/Database/Reader.php +++ b/src/Database/Reader.php @@ -16,6 +16,7 @@ use GeoIp2\ProviderInterface; use MaxMind\Db\Reader as DbReader; use MaxMind\Db\Reader\InvalidDatabaseException; +use MaxMind\Db\Reader\Metadata; /** * Instances of this class provide a reader for the GeoIP2 database format. @@ -250,7 +251,7 @@ private function getRecord(string $class, string $type, string $ipAddress): arra * @throws \InvalidArgumentException if arguments are passed to the method * @throws \BadMethodCallException if the database has been closed * - * @return \MaxMind\Db\Reader\Metadata object for the database + * @return Metadata object for the database */ public function metadata(): DbReader\Metadata { diff --git a/src/Model/City.php b/src/Model/City.php index 8c3ee176..6f766adb 100644 --- a/src/Model/City.php +++ b/src/Model/City.php @@ -66,13 +66,13 @@ public function __construct(array $raw, array $locales = ['en']) parent::__construct($raw, $locales); $this->city = new CityRecord($raw['city'] ?? [], $locales); - $this->location = new \GeoIp2\Record\Location($raw['location'] ?? []); - $this->postal = new \GeoIp2\Record\Postal($raw['postal'] ?? []); + $this->location = new Location($raw['location'] ?? []); + $this->postal = new Postal($raw['postal'] ?? []); if (!isset($raw['subdivisions'])) { $this->subdivisions = []; $this->mostSpecificSubdivision = - new \GeoIp2\Record\Subdivision([], $locales); + new Subdivision([], $locales); return; } @@ -80,7 +80,7 @@ public function __construct(array $raw, array $locales = ['en']) $subdivisions = []; foreach ($raw['subdivisions'] as $sub) { $subdivisions[] = - new \GeoIp2\Record\Subdivision($sub, $locales) + new Subdivision($sub, $locales) ; } diff --git a/src/Model/Country.php b/src/Model/Country.php index 38dc0c86..11a876cc 100644 --- a/src/Model/Country.php +++ b/src/Model/Country.php @@ -61,7 +61,7 @@ class Country implements \JsonSerializable */ public function __construct(array $raw, array $locales = ['en']) { - $this->continent = new \GeoIp2\Record\Continent( + $this->continent = new Continent( $raw['continent'] ?? [], $locales ); @@ -69,16 +69,16 @@ public function __construct(array $raw, array $locales = ['en']) $raw['country'] ?? [], $locales ); - $this->maxmind = new \GeoIp2\Record\MaxMind($raw['maxmind'] ?? []); + $this->maxmind = new MaxMind($raw['maxmind'] ?? []); $this->registeredCountry = new CountryRecord( $raw['registered_country'] ?? [], $locales ); - $this->representedCountry = new \GeoIp2\Record\RepresentedCountry( + $this->representedCountry = new RepresentedCountry( $raw['represented_country'] ?? [], $locales ); - $this->traits = new \GeoIp2\Record\Traits($raw['traits'] ?? []); + $this->traits = new Traits($raw['traits'] ?? []); } public function jsonSerialize(): ?array diff --git a/tests/GeoIp2/Test/WebService/ClientTest.php b/tests/GeoIp2/Test/WebService/ClientTest.php index f994d302..5c291da8 100644 --- a/tests/GeoIp2/Test/WebService/ClientTest.php +++ b/tests/GeoIp2/Test/WebService/ClientTest.php @@ -559,7 +559,7 @@ private function makeRequest( $method = strtolower($service); - $client = new \GeoIp2\WebService\Client( + $client = new Client( $accountId, $licenseKey, $locales,