From cdedaebb1c3b9a6d1c2bf1a985b530f5b53b1d8b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 11 Nov 2024 10:11:13 +0100 Subject: [PATCH 1/2] Add default values to Location constructor params --- CHANGELOG.md | 1 + src/Model/Location.php | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff8bb3b..fc71b7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Changed * Update shlinkio coding standard to v2.4 +* Add default values to `Location` constructor params, making it easier to handle with named parameters. ### Deprecated * *Nothing* diff --git a/src/Model/Location.php b/src/Model/Location.php index 3d23499..86a8f0b 100644 --- a/src/Model/Location.php +++ b/src/Model/Location.php @@ -4,21 +4,27 @@ namespace Shlinkio\Shlink\IpGeolocation\Model; -final class Location +final readonly class Location { public function __construct( - public readonly string $countryCode, - public readonly string $countryName, - public readonly string $regionName, - public readonly string $city, - public readonly float $latitude, - public readonly float $longitude, - public readonly string $timeZone, + public string $countryCode = '', + public string $countryName = '', + public string $regionName = '', + public string $city = '', + public float $latitude = 0.0, + public float $longitude = 0.0, + public string $timeZone = '', ) { } + /** @deprecated Use self:.empty() instead */ public static function emptyInstance(): self { - return new self('', '', '', '', 0.0, 0.0, ''); + return self::empty(); + } + + public static function empty(): self + { + return new self(); } } From e864db7bfe1cfcafd63a0a9c17bee8abb776c1a8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 11 Nov 2024 10:13:27 +0100 Subject: [PATCH 2/2] Replace usages of Location::emptyInstance() with Location::empty() --- src/Resolver/EmptyIpLocationResolver.php | 2 +- test/Resolver/ChainIpLocationResolverTest.php | 4 ++-- test/Resolver/EmptyIpLocationResolverTest.php | 2 +- test/Resolver/GeoLite2LocationResolverTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Resolver/EmptyIpLocationResolver.php b/src/Resolver/EmptyIpLocationResolver.php index 5bcce13..e8e5745 100644 --- a/src/Resolver/EmptyIpLocationResolver.php +++ b/src/Resolver/EmptyIpLocationResolver.php @@ -14,6 +14,6 @@ class EmptyIpLocationResolver implements IpLocationResolverInterface */ public function resolveIpLocation(string $ipAddress): Model\Location { - return Model\Location::emptyInstance(); + return Model\Location::empty(); } } diff --git a/test/Resolver/ChainIpLocationResolverTest.php b/test/Resolver/ChainIpLocationResolverTest.php index 4bc9398..f36848b 100644 --- a/test/Resolver/ChainIpLocationResolverTest.php +++ b/test/Resolver/ChainIpLocationResolverTest.php @@ -56,7 +56,7 @@ public function returnsResultOfFirstInnerResolver(): void ->expects($this->once()) ->method('resolveIpLocation') ->with($this->equalTo($ipAddress)) - ->willReturn(Location::emptyInstance()); + ->willReturn(Location::empty()); $this->secondInnerResolver->expects($this->never())->method('resolveIpLocation'); $this->resolver->resolveIpLocation($ipAddress); @@ -76,7 +76,7 @@ public function returnsResultOfSecondInnerResolver(): void ->expects($this->once()) ->method('resolveIpLocation') ->with($this->equalTo($ipAddress)) - ->willReturn(Location::emptyInstance()); + ->willReturn(Location::empty()); $this->resolver->resolveIpLocation($ipAddress); } diff --git a/test/Resolver/EmptyIpLocationResolverTest.php b/test/Resolver/EmptyIpLocationResolverTest.php index 3297015..8f52c8d 100644 --- a/test/Resolver/EmptyIpLocationResolverTest.php +++ b/test/Resolver/EmptyIpLocationResolverTest.php @@ -25,7 +25,7 @@ public function setUp(): void #[Test, DataProvider('provideEmptyResponses')] public function alwaysReturnsAnEmptyLocation(string $ipAddress): void { - self::assertEquals(Location::emptyInstance(), $this->resolver->resolveIpLocation($ipAddress)); + self::assertEquals(Location::empty(), $this->resolver->resolveIpLocation($ipAddress)); } public static function provideEmptyResponses(): array diff --git a/test/Resolver/GeoLite2LocationResolverTest.php b/test/Resolver/GeoLite2LocationResolverTest.php index 0e7bff0..191e407 100644 --- a/test/Resolver/GeoLite2LocationResolverTest.php +++ b/test/Resolver/GeoLite2LocationResolverTest.php @@ -62,6 +62,6 @@ public function resolvedCityIsProperlyMapped(): void $result = $this->resolver->resolveIpLocation($ipAddress); - self::assertEquals(Location::emptyInstance(), $result); + self::assertEquals(Location::empty(), $result); } }