Skip to content

Commit

Permalink
Merge pull request #156 from maxmind/greg/readonly-properties
Browse files Browse the repository at this point in the history
Use readonly properties in model classes
  • Loading branch information
faktas2 authored Nov 6, 2023
2 parents 192f5ab + 005cf2d commit c3294af
Show file tree
Hide file tree
Showing 41 changed files with 1,070 additions and 924 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2
tools: composer

- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2']
php-versions: ['8.1', '8.2']
name: "PHP ${{ matrix.php-versions }} test on ${{ matrix.operating-system }}"
steps:
- name: Setup PHP
Expand All @@ -31,4 +31,4 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Test with phpunit
run: vendor/bin/phpunit --coverage-text
run: vendor/bin/phpunit
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
CHANGELOG
=========

2.0.0
-------------------

* IMPORTANT: PHP 8.1 or greater is now required.
* BREAKING: Read-only properties are now used for the model class rather than
magic methods.
* BREAKING: The `rawResponse` property on model classess has been removed. Use
the `jsonSerialize` method instead.
* BREAKING: The inheritance hierarchy on model classes has changed.

1.23.0 (2023-03-06)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ to the client API, please see

## Requirements ##

This code requires PHP 7.3 or greater. Older versions of PHP are not
This code requires PHP 8.1 or greater. Older versions of PHP are not
supported.

There are several other dependencies as defined in the `composer.json` file.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
}
],
"require": {
"php": ">=7.3",
"php": ">=8.1",
"ext-json": "*",
"geoip2/geoip2": "^2.13",
"geoip2/geoip2": "^v3.0.0-alpha3",
"maxmind/web-service-common": "^0.9.0",
"respect/validation": "~2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.*",
"phpunit/phpunit": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.0",
"squizlabs/php_codesniffer": "*",
"phpstan/phpstan": "*"
},
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ parameters:
checkMissingIterableValueType: false
ignoreErrors:
- '/Parameter \#\d+ \.\.\.\$rule of static method Respect\\Validation\\StaticValidator::keySet\(\) expects Respect\\Validation\\Rules\\Key, Respect\\Validation\\ChainedValidator given\./'
- '/PHPDoc type MaxMind\\MinFraud\\Model\\IpAddress of property MaxMind\\MinFraud\\Model\\Insights::\$ipAddress is not covariant with PHPDoc type MaxMind\\MinFraud\\Model\\ScoreIpAddress of overridden property MaxMind\\MinFraud\\Model\\Score::\$ipAddress\./'
25 changes: 6 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="minFraud Test Suite">
<directory suffix="Test.php">./tests/MaxMind/Test/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/logs/html"/>
</logging>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="minFraud Test Suite">
<directory suffix="Test.php">./tests/MaxMind/Test/</directory>
</testsuite>
</testsuites>
</phpunit>
83 changes: 0 additions & 83 deletions src/MinFraud/Model/AbstractModel.php

This file was deleted.

86 changes: 45 additions & 41 deletions src/MinFraud/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,69 @@

/**
* Abstract model for a postal address.
*
* @property-read int|null $distanceToIpLocation The distance in kilometers from
* the address to the IP location.
* @property-read bool|null $isInIpCountry This property is true if the address
* is in the IP country. The property is false when the address is not in the
* IP country. If the address could not be parsed or was not provided or if
* the IP address could not be geolocated, the property will be null.
* @property-read bool|null $isPostalInCity This property is true if the postal
* code provided with the address is in the city for the address. The property
* is false when the postal code is not in the city. If the address was not
* provided or could not be parsed, the property will be null.
* @property-read float|null $latitude The latitude associated with the address.
* @property-read float|null $longitude The longitude associated with the address.
*/
abstract class Address extends AbstractModel
abstract class Address implements \JsonSerializable
{
/**
* @internal
*
* @var bool|null
* @var int|null the distance in kilometers from
* the address to the IP location
*/
protected $isPostalInCity;
public readonly ?int $distanceToIpLocation;

/**
* @internal
*
* @var float|null
* @var bool|null This property is true if the address is in the IP
* country. The property is false when the address is not in the IP
* country. If the address could not be parsed or was not provided or if
* the IP address could not be geolocated, the property will be null.
*/
protected $latitude;
public readonly ?bool $isInIpCountry;

/**
* @internal
*
* @var float|null
* @var bool|null This property is true if the postal code provided with
* the address is in the city for the address. The property is false when
* the postal code is not in the city. If the address was not provided or
* could not be parsed, the property will be null.
*/
protected $longitude;
public readonly ?bool $isPostalInCity;

/**
* @internal
*
* @var int|null
* @var float|null the latitude associated with the address
*/
protected $distanceToIpLocation;
public readonly ?float $latitude;

/**
* @internal
*
* @var bool|null
* @var float|null the longitude associated with the address
*/
protected $isInIpCountry;
public readonly ?float $longitude;

public function __construct(?array $response, array $locales = ['en'])
public function __construct(?array $response)
{
parent::__construct($response, $locales);
$this->distanceToIpLocation = $response['distance_to_ip_location'] ?? null;
$this->isInIpCountry = $response['is_in_ip_country'] ?? null;
$this->isPostalInCity = $response['is_postal_in_city'] ?? null;
$this->latitude = $response['latitude'] ?? null;
$this->longitude = $response['longitude'] ?? null;
}

public function jsonSerialize(): array
{
$js = [];
if ($this->distanceToIpLocation !== null) {
$js['distance_to_ip_location'] = $this->distanceToIpLocation;
}
if ($this->isInIpCountry !== null) {
$js['is_in_ip_country'] = $this->isInIpCountry;
}
if ($this->isPostalInCity !== null) {
$js['is_postal_in_city'] = $this->isPostalInCity;
}
if ($this->latitude !== null) {
$js['latitude'] = $this->latitude;
}
if ($this->longitude !== null) {
$js['longitude'] = $this->longitude;
}

$this->isPostalInCity = $this->safeArrayLookup($response['is_postal_in_city']);
$this->latitude = $this->safeArrayLookup($response['latitude']);
$this->longitude = $this->safeArrayLookup($response['longitude']);
$this->distanceToIpLocation = $this->safeArrayLookup($response['distance_to_ip_location']);
$this->isInIpCountry = $this->safeArrayLookup($response['is_in_ip_country']);
return $js;
}
}
Loading

0 comments on commit c3294af

Please sign in to comment.