From bf8c4bcddab60a7a1f5b0d7cbc881872e9d36e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Gonz=C3=A1lez=20Majoral?= Date: Tue, 26 May 2026 12:05:57 +0200 Subject: [PATCH] Fix LocationManager::get() return hint and simplify it. --- src/Facades/Location.php | 2 +- src/LocationFake.php | 2 +- src/LocationManager.php | 8 ++------ tests/LocationTest.php | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Facades/Location.php b/src/Facades/Location.php index ce7f29c..057a4eb 100644 --- a/src/Facades/Location.php +++ b/src/Facades/Location.php @@ -8,7 +8,7 @@ /** * @method static \Stevebauman\Location\Drivers\Driver[] drivers() - * @method static \Stevebauman\Location\Position|bool get(string $ip = null) + * @method static \Stevebauman\Location\Position|false get(string $ip = null) * @method static void resolveRequestUsing(callable $callback) * @method static void setDriver(\Stevebauman\Location\Drivers\Driver $driver) */ diff --git a/src/LocationFake.php b/src/LocationFake.php index 7dbdcef..fa6758f 100644 --- a/src/LocationFake.php +++ b/src/LocationFake.php @@ -28,7 +28,7 @@ public function __call(string $method, array $parameters): mixed /** * Get a fake location instance. */ - public function get(?string $ip = null): Position|bool + public function get(?string $ip = null): Position|false { $ip ??= '127.0.0.1'; diff --git a/src/LocationManager.php b/src/LocationManager.php index 7b84ad8..848026c 100644 --- a/src/LocationManager.php +++ b/src/LocationManager.php @@ -70,13 +70,9 @@ public function setDefaultDriver(): static /** * Attempt to retrieve the location of the user. */ - public function get(?string $ip = null): Position|bool + public function get(?string $ip = null): Position|false { - if ($location = $this->driver->get($this->request()->setIp($ip))) { - return $location; - } - - return false; + return $this->driver->get($this->request()->setIp($ip)); } /** diff --git a/tests/LocationTest.php b/tests/LocationTest.php index be5a9d0..28a1c3b 100644 --- a/tests/LocationTest.php +++ b/tests/LocationTest.php @@ -31,6 +31,20 @@ $this->assertInstanceOf(Position::class, Location::get()); }); +it('returns false when no driver can resolve a location', function () { + $driver = m::mock(Driver::class) + ->makePartial() + ->shouldAllowMockingProtectedMethods(); + + $driver->shouldReceive('process') + ->once() + ->andReturn(false); + + Location::setDriver($driver); + + expect(Location::get())->toBeFalse(); +}); + it('throws an exception when the driver does not exist', function () { config(['location.driver' => 'Test']);