From d3685e384ff2ca0cb698e19b746132aca021a739 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 13:26:26 +0000 Subject: [PATCH 01/11] Update minimum required PHP version, PHPUnit, and tests. --- .gitignore | 5 ++- composer.json | 6 +-- phpunit.xml | 31 ++++++++------ src/Distance.php | 6 ++- tests/DistanceTest.php | 95 +++++++++++++++++++----------------------- 5 files changed, 72 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index 9e270ad..0f0f75e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.idea composer.phar composer.lock -vendor/ \ No newline at end of file +vendor/ +.phpunit.cache +.phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 02b010b..e5a2909 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,10 @@ } ], "require": { - "php": ">=5.5" + "php": ">=8.4" }, "require-dev": { - "phpunit/phpunit": "4.6.*" + "phpunit/phpunit": "^13.0" }, "extra": { "branch-alias": { @@ -35,4 +35,4 @@ "autoload": { "psr-4": {"LucDeBrouwer\\Distance\\": "src/"} } -} \ No newline at end of file +} diff --git a/phpunit.xml b/phpunit.xml index 6df70d2..a3722f2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,23 @@ - + cacheDirectory=".phpunit.cache" + executionOrder="depends,defects" + beStrictAboutOutputDuringTests="true" + displayDetailsOnPhpunitDeprecations="true" + failOnPhpunitDeprecation="true" + failOnRisky="true" + failOnWarning="true"> - - ./tests/ + + tests - \ No newline at end of file + + + + src + + + diff --git a/src/Distance.php b/src/Distance.php index d0eb5c2..d226073 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -1,4 +1,8 @@ -setUnit('mi'); - $this->assertEquals('mi', $distance->getUnit()); + $this->assertSame('mi', $distance->getUnit()); } - /** - * Test the throwing of an exception in case we try to set an invalid unit. - * - * @expectedException Exception - */ - public function testInvalidUnit() + #[Test] + public function willThrowExceptionInCaseWeSetAnInvalidUnit(): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('You have tried to set an invalid distance unit.'); + $distance = new Distance(); $distance->setUnit('mm'); } - /** - * Test the setting and getting of the distance formula. - */ - public function testFormula() + #[Test] + public function canSetFormula(): void { $distance = new Distance(); $distance->setFormula('vincenty'); - $this->assertEquals('vincenty', $distance->getFormula()); + $this->assertSame('vincenty', $distance->getFormula()); } - /** - * Test the throwing of an exception in case we try to set an invalid formula. - * - * @expectedException Exception - */ - public function testInvalidFormula() + #[Test] + public function willThrowExceptionInCaseWeSetAnInvalidFormula(): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('You have tried to set an invalid distance formula.'); + $distance = new Distance(); $distance->setFormula('Leonardo'); } - /** - * Test the throwing of an exception in case an invalid parameter is being passed. - * - * @expectedException Exception - */ - public function testInvalidParams() + #[Test] + public function willThrowAnExceptionInCaseWeProvideInvalidParameters(): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('One or more of the parsed variables are not valid floats.'); + $distance = new Distance(); $distance->between('1', 1, 'not a float', 'invalid'); } - /** - * Test the retrieval of the distance between the Apple and Google campuses using the Vincenty formula. - */ - public function testDistanceBetweenAppleAndGoogleUsingVincenty() + #[Test] + public function canConvertDistanceUsingVincentyFormula(): void { $distance = new Distance(); $distance->setUnit('m'); $distance->setFormula('vincenty'); - $this->assertEquals(11164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('km'); - $this->assertEquals(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('mi'); - $this->assertEquals(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('cm'); - $this->assertEquals(1116400, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('ft'); - $this->assertEquals(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('in'); - $this->assertEquals(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); } - /** - * Test the retrieval of the distance between the Apple and Google campuses using the Haversine formula. - */ - public function testDistanceBetweenAppleAndGoogleUsingHaversine() + #[Test] + public function canConvertDistanceUsingHaversineFormula(): void { $distance = new Distance(); $distance->setUnit('m'); $distance->setFormula('haversine'); - $this->assertEquals(11164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('km'); - $this->assertEquals(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('mi'); - $this->assertEquals(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('cm'); - $this->assertEquals(1116400, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('ft'); - $this->assertEquals(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); $distance->setUnit('in'); - $this->assertEquals(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); } } \ No newline at end of file From eeef1129ef7bbf0ac1b7b8780e43363b40490336 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 15:27:17 +0000 Subject: [PATCH 02/11] Introduce PHPStan, PHPCS, clean up code. --- .gitignore | 1 + composer.json | 22 ++++++- phpcs.xml | 132 +++++++++++++++++++++++++++++++++++++++++ phpstan.neon | 12 ++++ src/Distance.php | 124 +++++++++++--------------------------- tests/DistanceTest.php | 20 ++----- 6 files changed, 205 insertions(+), 106 deletions(-) create mode 100644 phpcs.xml create mode 100644 phpstan.neon diff --git a/.gitignore b/.gitignore index 0f0f75e..edede23 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ composer.phar composer.lock vendor/ +storage/ .phpunit.cache .phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index e5a2909..bbe0326 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,13 @@ "php": ">=8.4" }, "require-dev": { - "phpunit/phpunit": "^13.0" + "phpunit/phpunit": "^13.0", + "phpstan/phpstan": "^2.1", + "phpstan/phpstan-phpunit": "^2.0", + "roave/security-advisories": "dev-latest", + "phpstan/phpstan-strict-rules": "^2.0", + "squizlabs/php_codesniffer": "^4.0", + "slevomat/coding-standard": "^8.27" }, "extra": { "branch-alias": { @@ -33,6 +39,18 @@ } }, "autoload": { - "psr-4": {"LucDeBrouwer\\Distance\\": "src/"} + "psr-4": { + "LucDeBrouwer\\Distance\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\LucDeBrouwer\\Distance\\": "tests/" + } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..d2e5a58 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,132 @@ + + + The coding standard for ldebrouwer/distance. + + + + src/ + tests + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..a29f3a0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,12 @@ +parameters: + level: max + paths: + - src + - tests + tmpDir: /var/www/html/storage/phpstan-cache + reportUnmatchedIgnoredErrors: true + +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon + - vendor/phpstan/phpstan-strict-rules/rules.neon + - vendor/phpstan/phpstan/conf/bleedingEdge.neon diff --git a/src/Distance.php b/src/Distance.php index d226073..3fe030a 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -4,88 +4,62 @@ namespace LucDeBrouwer\Distance; -use \Exception; +use RuntimeException; /** * Distance helps you calculate the distance between GPS coordinates, in vanilla PHP. Pure and simple. - * - * @package LucDeBrouwer\Distance */ class Distance { - /** * The formula used for distance calculation. - * - * @var string */ - private $formula = 'vincenty'; + private string $formula = 'vincenty'; /** * The unit used to return the distance in. The supported units are shown in the conversion table below. - * - * @var string */ - private $unit = 'km'; + private string $unit = 'km'; /** * An array holding the conversion table from meters to several other units. * - * @var array + * @var array */ - private $conversion = [ - 'cm' => '100', // centimeters - 'in' => '39.3700787', // inches - 'ft' => '3.2808399', // feet - 'm' => '1', // meters - 'km' => '0.001', // kilometers - 'mi' => '0.000621371192', // miles + private array $conversion = [ + 'cm' => 100, // centimeters + 'in' => 39.3700787, // inches + 'ft' => 3.2808399, // feet + 'm' => 1, // meters + 'km' => 0.001, // kilometers + 'mi' => 0.000621371192, // miles ]; /** * Method that returns the distance between two GPS locations in the preferred unit according to the set formula. - * - * @param float $latitudeA The latitude for point A. - * @param float $longitudeA The longitude for point A. - * @param float $latitudeB The latitude for point B. - * @param float $longitudeB The longitude for point B. - * - * @uses betweenVincenty - * @uses betweenHaversine - * @throws Exception - * @return float */ - public function between($latitudeA, $longitudeA, $latitudeB, $longitudeB) + public function between(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { - if (!floatval($latitudeA) || !floatval($longitudeA) || !floatval($latitudeB) || !floatval($longitudeB)) { - throw new Exception('One or more of the parsed variables are not valid floats.'); - } - - $distanceInMeters = call_user_func_array([$this, 'between' . ucfirst($this->getFormula())], - [$latitudeA, $longitudeA, $latitudeB, $longitudeB]); + $distanceInMeters = match ($this->getFormula()) { + 'vincenty' => $this->betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitudeB), + 'haversine' => $this->betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitudeB), + default => throw new RuntimeException('Invalid formula'), + }; return $this->convert($distanceInMeters); } /** * Method that returns the distance between two GPS locations in meters according to the Vincenty formula. - * - * @param float $latitudeA The latitude for point A. - * @param float $longitudeA The longitude for point A. - * @param float $latitudeB The latitude for point B. - * @param float $longitudeB The longitude for point B. - * - * @return float */ - private function betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitudeB) + private function betweenVincenty(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { $latitudeA = deg2rad($latitudeA); $longitudeA = deg2rad($longitudeA); $latitudeB = deg2rad($latitudeB); $longitudeB = deg2rad($longitudeB); $longDelta = $longitudeB - $longitudeA; - $a = pow(cos($latitudeB) * sin($longDelta), - 2) + pow(cos($latitudeA) * sin($latitudeB) - sin($latitudeA) * cos($latitudeB) * cos($longDelta), 2); + $a = ((cos($latitudeB) * sin($longDelta)) ** 2) + ((cos($latitudeA) * sin($latitudeB) - sin($latitudeA) * cos($latitudeB) * cos($longDelta)) ** 2); $b = sin($latitudeA) * sin($latitudeB) + cos($latitudeA) * cos($latitudeB) * cos($longDelta); $angle = atan2(sqrt($a), $b); @@ -94,15 +68,8 @@ private function betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitude /** * Method that returns the distance between two GPS locations in meters according to the Haversine formula. - * - * @param float $latitudeA The latitude for point A. - * @param float $longitudeA The longitude for point A. - * @param float $latitudeB The latitude for point B. - * @param float $longitudeB The longitude for point B. - * - * @return float */ - private function betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitudeB) + private function betweenHaversine(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { $latitudeA = deg2rad($latitudeA); $longitudeA = deg2rad($longitudeA); @@ -110,26 +77,20 @@ private function betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitud $longitudeB = deg2rad($longitudeB); $latDelta = $latitudeB - $latitudeA; $longDelta = $longitudeB - $longitudeA; - $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latitudeA) * cos($latitudeB) * pow(sin($longDelta / 2), 2))); + $angle = 2 * asin(sqrt((sin($latDelta / 2) ** 2) + cos($latitudeA) * cos($latitudeB) * (sin($longDelta / 2) ** 2))); return floor($angle * 6371000); } - /** - * @param $distance - * @return mixed - */ - private function convert($distance) + private function convert(float $distance): float { - return $distance * $this->getConversion()[$this->getUnit()]; + return $distance * $this->conversion[$this->getUnit()]; } /** * Returns the currently set formula used for distance calculation. - * - * @return string */ - public function getFormula() + public function getFormula(): string { return $this->formula; } @@ -137,14 +98,12 @@ public function getFormula() /** * Sets the formula to be used for distance calculation. * - * @param string $formula - * - * @throws Exception + * @throws RuntimeException */ - public function setFormula($formula) + public function setFormula(string $formula): void { - if (!in_array($formula, ['vincenty', 'haversine'])) { - throw new Exception('You have tried to set an invalid distance formula.'); + if (!in_array($formula, ['vincenty', 'haversine'], true)) { + throw new RuntimeException('You have tried to set an invalid distance formula.'); } $this->formula = $formula; @@ -152,10 +111,8 @@ public function setFormula($formula) /** * Returns the currently set unit used when returning the distance between two coordinates. - * - * @return string */ - public function getUnit() + public function getUnit(): string { return $this->unit; } @@ -163,27 +120,14 @@ public function getUnit() /** * Sets the unit to be used when returning the distance between two coordinates. * - * @param string $unit - * - * @throws Exception + * @throws RuntimeException */ - public function setUnit($unit) + public function setUnit(string $unit): void { - if (!in_array($unit, array_keys($this->getConversion()))) { - throw new Exception('You have tried to set an invalid distance unit.'); + if (!array_key_exists($unit, $this->conversion)) { + throw new RuntimeException('You have tried to set an invalid distance unit.'); } $this->unit = $unit; } - - /** - * Gets the conversion table between different units. - * - * @return array - */ - public function getConversion() - { - return $this->conversion; - } - -} \ No newline at end of file +} diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index 5ac711c..2b599ef 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -2,9 +2,12 @@ declare(strict_types=1); +namespace Tests\LucDeBrouwer\Distance; + use LucDeBrouwer\Distance\Distance; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use RuntimeException; class DistanceTest extends TestCase { @@ -20,7 +23,7 @@ public function canSetUnit(): void #[Test] public function willThrowExceptionInCaseWeSetAnInvalidUnit(): void { - $this->expectException(Exception::class); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('You have tried to set an invalid distance unit.'); $distance = new Distance(); @@ -40,7 +43,7 @@ public function canSetFormula(): void #[Test] public function willThrowExceptionInCaseWeSetAnInvalidFormula(): void { - $this->expectException(Exception::class); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('You have tried to set an invalid distance formula.'); $distance = new Distance(); @@ -48,17 +51,6 @@ public function willThrowExceptionInCaseWeSetAnInvalidFormula(): void $distance->setFormula('Leonardo'); } - #[Test] - public function willThrowAnExceptionInCaseWeProvideInvalidParameters(): void - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('One or more of the parsed variables are not valid floats.'); - - $distance = new Distance(); - - $distance->between('1', 1, 'not a float', 'invalid'); - } - #[Test] public function canConvertDistanceUsingVincentyFormula(): void { @@ -118,4 +110,4 @@ public function canConvertDistanceUsingHaversineFormula(): void $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); } -} \ No newline at end of file +} From 70e76011cb2eda70c21107200228891d0a721d0a Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 15:32:48 +0000 Subject: [PATCH 03/11] Use a string-backed enumeration class for formulas. --- src/Distance.php | 15 +++++---------- src/Formula.php | 11 +++++++++++ tests/DistanceTest.php | 20 +++++--------------- 3 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 src/Formula.php diff --git a/src/Distance.php b/src/Distance.php index 3fe030a..15f3e24 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -14,7 +14,7 @@ class Distance /** * The formula used for distance calculation. */ - private string $formula = 'vincenty'; + private Formula $formula = Formula::VINCENTY; /** * The unit used to return the distance in. The supported units are shown in the conversion table below. @@ -41,9 +41,8 @@ class Distance public function between(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { $distanceInMeters = match ($this->getFormula()) { - 'vincenty' => $this->betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitudeB), - 'haversine' => $this->betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitudeB), - default => throw new RuntimeException('Invalid formula'), + Formula::VINCENTY => $this->betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitudeB), + Formula::HAVERSINE => $this->betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitudeB), }; return $this->convert($distanceInMeters); @@ -90,7 +89,7 @@ private function convert(float $distance): float /** * Returns the currently set formula used for distance calculation. */ - public function getFormula(): string + public function getFormula(): Formula { return $this->formula; } @@ -100,12 +99,8 @@ public function getFormula(): string * * @throws RuntimeException */ - public function setFormula(string $formula): void + public function setFormula(Formula $formula): void { - if (!in_array($formula, ['vincenty', 'haversine'], true)) { - throw new RuntimeException('You have tried to set an invalid distance formula.'); - } - $this->formula = $formula; } diff --git a/src/Formula.php b/src/Formula.php new file mode 100644 index 0000000..62425b7 --- /dev/null +++ b/src/Formula.php @@ -0,0 +1,11 @@ +setFormula('vincenty'); + $distance->setFormula(Formula::VINCENTY); - $this->assertSame('vincenty', $distance->getFormula()); - } - - #[Test] - public function willThrowExceptionInCaseWeSetAnInvalidFormula(): void - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('You have tried to set an invalid distance formula.'); - - $distance = new Distance(); - - $distance->setFormula('Leonardo'); + $this->assertSame(Formula::VINCENTY, $distance->getFormula()); } #[Test] @@ -56,7 +46,7 @@ public function canConvertDistanceUsingVincentyFormula(): void { $distance = new Distance(); $distance->setUnit('m'); - $distance->setFormula('vincenty'); + $distance->setFormula(Formula::VINCENTY); $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); @@ -86,7 +76,7 @@ public function canConvertDistanceUsingHaversineFormula(): void { $distance = new Distance(); $distance->setUnit('m'); - $distance->setFormula('haversine'); + $distance->setFormula(Formula::HAVERSINE); $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); From 66fc006fe8bc026de78d7742c314fdda1ec3d1ce Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 15:49:02 +0000 Subject: [PATCH 04/11] Use a string-backed enumeration class for units. --- src/Distance.php | 32 ++++---------------------------- src/Unit.php | 27 +++++++++++++++++++++++++++ tests/DistanceTest.php | 41 +++++++++++++++-------------------------- 3 files changed, 46 insertions(+), 54 deletions(-) create mode 100644 src/Unit.php diff --git a/src/Distance.php b/src/Distance.php index 15f3e24..cfbc658 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -4,8 +4,6 @@ namespace LucDeBrouwer\Distance; -use RuntimeException; - /** * Distance helps you calculate the distance between GPS coordinates, in vanilla PHP. Pure and simple. */ @@ -19,21 +17,7 @@ class Distance /** * The unit used to return the distance in. The supported units are shown in the conversion table below. */ - private string $unit = 'km'; - - /** - * An array holding the conversion table from meters to several other units. - * - * @var array - */ - private array $conversion = [ - 'cm' => 100, // centimeters - 'in' => 39.3700787, // inches - 'ft' => 3.2808399, // feet - 'm' => 1, // meters - 'km' => 0.001, // kilometers - 'mi' => 0.000621371192, // miles - ]; + private Unit $unit = Unit::KILOMETRES; /** * Method that returns the distance between two GPS locations in the preferred unit according to the set formula. @@ -83,7 +67,7 @@ private function betweenHaversine(float $latitudeA, float $longitudeA, float $la private function convert(float $distance): float { - return $distance * $this->conversion[$this->getUnit()]; + return $distance * $this->unit->multiplierFromMetres(); } /** @@ -96,8 +80,6 @@ public function getFormula(): Formula /** * Sets the formula to be used for distance calculation. - * - * @throws RuntimeException */ public function setFormula(Formula $formula): void { @@ -107,22 +89,16 @@ public function setFormula(Formula $formula): void /** * Returns the currently set unit used when returning the distance between two coordinates. */ - public function getUnit(): string + public function getUnit(): Unit { return $this->unit; } /** * Sets the unit to be used when returning the distance between two coordinates. - * - * @throws RuntimeException */ - public function setUnit(string $unit): void + public function setUnit(Unit $unit): void { - if (!array_key_exists($unit, $this->conversion)) { - throw new RuntimeException('You have tried to set an invalid distance unit.'); - } - $this->unit = $unit; } } diff --git a/src/Unit.php b/src/Unit.php new file mode 100644 index 0000000..58effb7 --- /dev/null +++ b/src/Unit.php @@ -0,0 +1,27 @@ + 100, + self::INCHES => 39.3700787, + self::FEET => 3.2808399, + self::METRES => 1, + self::KILOMETRES => 0.001, + self::MILES => 0.000621371192, + }; + } +} diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index 661e110..7d99ab3 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -6,9 +6,9 @@ use LucDeBrouwer\Distance\Distance; use LucDeBrouwer\Distance\Formula; +use LucDeBrouwer\Distance\Unit; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; -use RuntimeException; class DistanceTest extends TestCase { @@ -16,20 +16,9 @@ class DistanceTest extends TestCase public function canSetUnit(): void { $distance = new Distance(); - $distance->setUnit('mi'); + $distance->setUnit(Unit::MILES); - $this->assertSame('mi', $distance->getUnit()); - } - - #[Test] - public function willThrowExceptionInCaseWeSetAnInvalidUnit(): void - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('You have tried to set an invalid distance unit.'); - - $distance = new Distance(); - - $distance->setUnit('mm'); + $this->assertSame(Unit::MILES, $distance->getUnit()); } #[Test] @@ -45,28 +34,28 @@ public function canSetFormula(): void public function canConvertDistanceUsingVincentyFormula(): void { $distance = new Distance(); - $distance->setUnit('m'); + $distance->setUnit(Unit::METRES); $distance->setFormula(Formula::VINCENTY); $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('km'); + $distance->setUnit(Unit::KILOMETRES); $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('mi'); + $distance->setUnit(Unit::MILES); $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('cm'); + $distance->setUnit(Unit::CENTIMETRES); $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('ft'); + $distance->setUnit(Unit::FEET); $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('in'); + $distance->setUnit(Unit::INCHES); $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); } @@ -75,28 +64,28 @@ public function canConvertDistanceUsingVincentyFormula(): void public function canConvertDistanceUsingHaversineFormula(): void { $distance = new Distance(); - $distance->setUnit('m'); + $distance->setUnit(Unit::METRES); $distance->setFormula(Formula::HAVERSINE); $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('km'); + $distance->setUnit(Unit::KILOMETRES); $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('mi'); + $distance->setUnit(Unit::MILES); $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('cm'); + $distance->setUnit(Unit::CENTIMETRES); $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('ft'); + $distance->setUnit(Unit::FEET); $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - $distance->setUnit('in'); + $distance->setUnit(Unit::INCHES); $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); } From d09bd89e2ab02ac6659c5183db70c4cb2bbcb049 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 16:08:22 +0000 Subject: [PATCH 05/11] Make setters fluent, clean up tests. --- src/Distance.php | 16 ++++++---- tests/DistanceTest.php | 72 ++++++++++++++---------------------------- 2 files changed, 34 insertions(+), 54 deletions(-) diff --git a/src/Distance.php b/src/Distance.php index cfbc658..80960fb 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -15,12 +15,12 @@ class Distance private Formula $formula = Formula::VINCENTY; /** - * The unit used to return the distance in. The supported units are shown in the conversion table below. + * The unit used to return the distance in. */ private Unit $unit = Unit::KILOMETRES; /** - * Method that returns the distance between two GPS locations in the preferred unit according to the set formula. + * Returns the distance between two GPS locations in the preferred unit according to the set formula. */ public function between(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { @@ -33,7 +33,7 @@ public function between(float $latitudeA, float $longitudeA, float $latitudeB, f } /** - * Method that returns the distance between two GPS locations in meters according to the Vincenty formula. + * Returns the distance between two GPS locations in meters according to the Vincenty formula. */ private function betweenVincenty(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { @@ -50,7 +50,7 @@ private function betweenVincenty(float $latitudeA, float $longitudeA, float $lat } /** - * Method that returns the distance between two GPS locations in meters according to the Haversine formula. + * Returns the distance between two GPS locations in meters according to the Haversine formula. */ private function betweenHaversine(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { @@ -81,9 +81,11 @@ public function getFormula(): Formula /** * Sets the formula to be used for distance calculation. */ - public function setFormula(Formula $formula): void + public function setFormula(Formula $formula): self { $this->formula = $formula; + + return $this; } /** @@ -97,8 +99,10 @@ public function getUnit(): Unit /** * Sets the unit to be used when returning the distance between two coordinates. */ - public function setUnit(Unit $unit): void + public function setUnit(Unit $unit): self { $this->unit = $unit; + + return $this; } } diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index 7d99ab3..5583012 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -8,17 +8,23 @@ use LucDeBrouwer\Distance\Formula; use LucDeBrouwer\Distance\Unit; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; class DistanceTest extends TestCase { + private const float LATITUDE_A = 37.331741; + private const float LONGITUDE_A = -122.030333; + private const float LATITUDE_B = 37.422546; + private const float LONGITUDE_B = -122.084250; + #[Test] public function canSetUnit(): void { $distance = new Distance(); $distance->setUnit(Unit::MILES); - $this->assertSame(Unit::MILES, $distance->getUnit()); + self::assertSame(Unit::MILES, $distance->getUnit()); } #[Test] @@ -27,66 +33,36 @@ public function canSetFormula(): void $distance = new Distance(); $distance->setFormula(Formula::VINCENTY); - $this->assertSame(Formula::VINCENTY, $distance->getFormula()); + self::assertSame(Formula::VINCENTY, $distance->getFormula()); } #[Test] - public function canConvertDistanceUsingVincentyFormula(): void + #[TestWith([Unit::METRES, 11164.0])] + #[TestWith([Unit::KILOMETRES, 11.164])] + #[TestWith([Unit::MILES, 6.936987987488])] + #[TestWith([Unit::CENTIMETRES, 1116400.0])] + #[TestWith([Unit::FEET, 36627.2966436])] + #[TestWith([Unit::INCHES, 439527.5586068])] + public function canConvertDistanceUsingVincentyFormula(Unit $unit, float $expected): void { $distance = new Distance(); - $distance->setUnit(Unit::METRES); $distance->setFormula(Formula::VINCENTY); - $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::KILOMETRES); - - $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::MILES); - - $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::CENTIMETRES); - - $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::FEET); - - $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::INCHES); - - $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + self::assertSame($expected, $distance->setUnit($unit)->between(self::LATITUDE_A, self::LONGITUDE_A, self::LATITUDE_B, self::LONGITUDE_B)); } #[Test] - public function canConvertDistanceUsingHaversineFormula(): void + #[TestWith([Unit::METRES, 11164.0])] + #[TestWith([Unit::KILOMETRES, 11.164])] + #[TestWith([Unit::MILES, 6.936987987488])] + #[TestWith([Unit::CENTIMETRES, 1116400.0])] + #[TestWith([Unit::FEET, 36627.2966436])] + #[TestWith([Unit::INCHES, 439527.5586068])] + public function canConvertDistanceUsingHaversineFormula(Unit $unit, float $expected): void { $distance = new Distance(); - $distance->setUnit(Unit::METRES); $distance->setFormula(Formula::HAVERSINE); - $this->assertSame(11164.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::KILOMETRES); - - $this->assertSame(11.164, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::MILES); - - $this->assertSame(6.936987987488, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::CENTIMETRES); - - $this->assertSame(1116400.0, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::FEET); - - $this->assertSame(36627.2966436, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); - - $distance->setUnit(Unit::INCHES); - - $this->assertSame(439527.5586068, $distance->between(37.331741, -122.030333, 37.422546, -122.084250)); + self::assertSame($expected, $distance->setUnit($unit)->between(self::LATITUDE_A, self::LONGITUDE_A, self::LATITUDE_B, self::LONGITUDE_B)); } } From 84873033d3f87a33c6c550f067e869f804b9f419 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Sun, 22 Feb 2026 18:44:45 +0000 Subject: [PATCH 06/11] GitHub workflows setup. --- .github/workflows/php_84.yml | 39 ++++++++++++++++++++++++++++++++++++ .github/workflows/php_85.yml | 39 ++++++++++++++++++++++++++++++++++++ .gitignore | 1 - phpstan.neon | 1 - 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/php_84.yml create mode 100644 .github/workflows/php_85.yml diff --git a/.github/workflows/php_84.yml b/.github/workflows/php_84.yml new file mode 100644 index 0000000..aa7d89b --- /dev/null +++ b/.github/workflows/php_84.yml @@ -0,0 +1,39 @@ +name: PHP 8.4 +on: push + +jobs: + build: + name: Static analysis, unit tests, and linting + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup PHP 8.4 + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + tools: composer + + - name: Cache Composer dependencies + uses: actions/cache@v5 + with: + path: ./vendor + key: composer-${{ runner.os }}-8.4 + + - name: Install dependencies + run: | + composer install + + - name: Run PHPStan + run: | + ./vendor/bin/phpstan + + - name: PHP Code Sniffer + run: | + ./vendor/bin/phpcs + + - name: Run PHPUnit + run: | + ./vendor/bin/phpunit \ No newline at end of file diff --git a/.github/workflows/php_85.yml b/.github/workflows/php_85.yml new file mode 100644 index 0000000..b44a2d1 --- /dev/null +++ b/.github/workflows/php_85.yml @@ -0,0 +1,39 @@ +name: PHP 8.5 +on: push + +jobs: + build: + name: Static analysis, unit tests, and linting + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup PHP 8.5 + uses: shivammathur/setup-php@v2 + with: + php-version: '8.5' + tools: composer + + - name: Cache Composer dependencies + uses: actions/cache@v5 + with: + path: ./vendor + key: composer-${{ runner.os }}-8.5 + + - name: Install dependencies + run: | + composer install + + - name: Run PHPStan + run: | + ./vendor/bin/phpstan + + - name: PHP Code Sniffer + run: | + ./vendor/bin/phpcs + + - name: Run PHPUnit + run: | + ./vendor/bin/phpunit \ No newline at end of file diff --git a/.gitignore b/.gitignore index edede23..0f0f75e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,5 @@ composer.phar composer.lock vendor/ -storage/ .phpunit.cache .phpunit.result.cache \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index a29f3a0..fa81b49 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,7 +3,6 @@ parameters: paths: - src - tests - tmpDir: /var/www/html/storage/phpstan-cache reportUnmatchedIgnoredErrors: true includes: From 689b8b674727735aa9794a3006cc171ebfd63c71 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Mon, 23 Feb 2026 19:21:51 +0000 Subject: [PATCH 07/11] Combine workflows, generate code coverage and upload it to Codecov. --- .github/workflows/php_84.yml | 39 ----------------- .github/workflows/php_85.yml | 39 ----------------- .github/workflows/tests.yml | 83 ++++++++++++++++++++++++++++++++++++ README.md | 2 +- 4 files changed, 84 insertions(+), 79 deletions(-) delete mode 100644 .github/workflows/php_84.yml delete mode 100644 .github/workflows/php_85.yml create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/php_84.yml b/.github/workflows/php_84.yml deleted file mode 100644 index aa7d89b..0000000 --- a/.github/workflows/php_84.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: PHP 8.4 -on: push - -jobs: - build: - name: Static analysis, unit tests, and linting - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v6 - - - name: Setup PHP 8.4 - uses: shivammathur/setup-php@v2 - with: - php-version: '8.4' - tools: composer - - - name: Cache Composer dependencies - uses: actions/cache@v5 - with: - path: ./vendor - key: composer-${{ runner.os }}-8.4 - - - name: Install dependencies - run: | - composer install - - - name: Run PHPStan - run: | - ./vendor/bin/phpstan - - - name: PHP Code Sniffer - run: | - ./vendor/bin/phpcs - - - name: Run PHPUnit - run: | - ./vendor/bin/phpunit \ No newline at end of file diff --git a/.github/workflows/php_85.yml b/.github/workflows/php_85.yml deleted file mode 100644 index b44a2d1..0000000 --- a/.github/workflows/php_85.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: PHP 8.5 -on: push - -jobs: - build: - name: Static analysis, unit tests, and linting - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v6 - - - name: Setup PHP 8.5 - uses: shivammathur/setup-php@v2 - with: - php-version: '8.5' - tools: composer - - - name: Cache Composer dependencies - uses: actions/cache@v5 - with: - path: ./vendor - key: composer-${{ runner.os }}-8.5 - - - name: Install dependencies - run: | - composer install - - - name: Run PHPStan - run: | - ./vendor/bin/phpstan - - - name: PHP Code Sniffer - run: | - ./vendor/bin/phpcs - - - name: Run PHPUnit - run: | - ./vendor/bin/phpunit \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..73faf98 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,83 @@ +name: Tests +on: push + +jobs: + build-84: + name: Static analysis, unit tests, and linting on PHP 8.4 + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup PHP 8.4 + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + tools: composer + + - name: Cache Composer dependencies + uses: actions/cache@v5 + with: + path: ./vendor + key: composer-${{ runner.os }}-8.4 + + - name: Install dependencies + run: | + composer install + + - name: Run PHPStan + run: | + ./vendor/bin/phpstan + + - name: PHP Code Sniffer + run: | + ./vendor/bin/phpcs + + - name: Run PHPUnit + run: | + ./vendor/bin/phpunit + + build-85: + name: Static analysis, unit tests, and linting on PHP 8.5 + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup PHP 8.5 + uses: shivammathur/setup-php@v2 + with: + php-version: '8.5' + tools: composer + coverage: pcov + + - name: Cache Composer dependencies + uses: actions/cache@v5 + with: + path: ./vendor + key: composer-${{ runner.os }}-8.5 + + - name: Install dependencies + run: | + composer install + + - name: Run PHPStan + run: | + ./vendor/bin/phpstan + + - name: PHP Code Sniffer + run: | + ./vendor/bin/phpcs + + - name: Run PHPUnit + run: | + ./vendor/bin/phpunit --coverage-clover ./coverage.xml + + - name: Upload to Codecov + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./coverage.xml + verbose: true \ No newline at end of file diff --git a/README.md b/README.md index a3877bf..3517722 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Distance -[![Build Status](https://travis-ci.org/ldebrouwer/distance.svg)](https://travis-ci.org/ldebrouwer/distance) +[![Tests](https://github.com/ldebrouwer/distance/actions/workflows/tests.yml/badge.svg)](https://github.com/ldebrouwer/distance/actions/workflows/tests.yml) [![Latest Stable Version](https://poser.pugx.org/ldebrouwer/distance/v/stable)](https://packagist.org/packages/ldebrouwer/distance) [![Total Downloads](https://poser.pugx.org/ldebrouwer/distance/downloads)](https://packagist.org/packages/ldebrouwer/distance) [![Latest Unstable Version](https://poser.pugx.org/ldebrouwer/distance/v/unstable)](https://packagist.org/packages/ldebrouwer/distance) From 3007f1cf5c1c4b77ca6424c901f87093df591eb0 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Mon, 23 Feb 2026 19:48:24 +0000 Subject: [PATCH 08/11] Update namespace for consistent naming. --- composer.json | 7 +++---- src/Distance.php | 2 +- src/Formula.php | 2 +- src/Unit.php | 2 +- tests/DistanceTest.php | 8 ++++---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index bbe0326..13af2b5 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,7 @@ { "name": "Luc De Brouwer", "email": "info@lucdebrouwer.nl", - "homepage": "http://www.lucdebrouwer.nl", - "role": "Developer" + "homepage": "http://www.lucdebrouwer.nl" } ], "require": { @@ -40,12 +39,12 @@ }, "autoload": { "psr-4": { - "LucDeBrouwer\\Distance\\": "src/" + "ldebrouwer\\Distance\\": "src/" } }, "autoload-dev": { "psr-4": { - "Tests\\LucDeBrouwer\\Distance\\": "tests/" + "Tests\\ldebrouwer\\Distance\\": "tests/" } }, "config": { diff --git a/src/Distance.php b/src/Distance.php index 80960fb..b12b10a 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace LucDeBrouwer\Distance; +namespace ldebrouwer\Distance; /** * Distance helps you calculate the distance between GPS coordinates, in vanilla PHP. Pure and simple. diff --git a/src/Formula.php b/src/Formula.php index 62425b7..156b079 100644 --- a/src/Formula.php +++ b/src/Formula.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace LucDeBrouwer\Distance; +namespace ldebrouwer\Distance; enum Formula: string { diff --git a/src/Unit.php b/src/Unit.php index 58effb7..45884d2 100644 --- a/src/Unit.php +++ b/src/Unit.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace LucDeBrouwer\Distance; +namespace ldebrouwer\Distance; enum Unit: string { diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index 5583012..f4ffc5c 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Tests\LucDeBrouwer\Distance; +namespace Tests\ldebrouwer\Distance; -use LucDeBrouwer\Distance\Distance; -use LucDeBrouwer\Distance\Formula; -use LucDeBrouwer\Distance\Unit; +use ldebrouwer\Distance\Distance; +use ldebrouwer\Distance\Formula; +use ldebrouwer\Distance\Unit; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; From 009e0d9129adc62a7f13abf7311112b727df5fc3 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Mon, 23 Feb 2026 19:56:12 +0000 Subject: [PATCH 09/11] Remove .travis.yml. --- .travis.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index deb51d8..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: php - -php: - - 5.5 - - 5.6 - - 7.0 - - hhvm - -matrix: - allow_failures: - - php: 7.0 - -sudo: false - -install: travis_retry composer install --no-interaction --prefer-source - -script: vendor/bin/phpunit \ No newline at end of file From 570692382bda72e60dedb9a3d879cbe318d03124 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Mon, 23 Feb 2026 20:08:31 +0000 Subject: [PATCH 10/11] Remove obsolete getters. --- src/Distance.php | 18 +----------------- tests/DistanceTest.php | 18 ------------------ 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/src/Distance.php b/src/Distance.php index b12b10a..0212b4c 100644 --- a/src/Distance.php +++ b/src/Distance.php @@ -24,7 +24,7 @@ class Distance */ public function between(float $latitudeA, float $longitudeA, float $latitudeB, float $longitudeB): float { - $distanceInMeters = match ($this->getFormula()) { + $distanceInMeters = match ($this->formula) { Formula::VINCENTY => $this->betweenVincenty($latitudeA, $longitudeA, $latitudeB, $longitudeB), Formula::HAVERSINE => $this->betweenHaversine($latitudeA, $longitudeA, $latitudeB, $longitudeB), }; @@ -70,14 +70,6 @@ private function convert(float $distance): float return $distance * $this->unit->multiplierFromMetres(); } - /** - * Returns the currently set formula used for distance calculation. - */ - public function getFormula(): Formula - { - return $this->formula; - } - /** * Sets the formula to be used for distance calculation. */ @@ -88,14 +80,6 @@ public function setFormula(Formula $formula): self return $this; } - /** - * Returns the currently set unit used when returning the distance between two coordinates. - */ - public function getUnit(): Unit - { - return $this->unit; - } - /** * Sets the unit to be used when returning the distance between two coordinates. */ diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php index f4ffc5c..f01e3ef 100644 --- a/tests/DistanceTest.php +++ b/tests/DistanceTest.php @@ -18,24 +18,6 @@ class DistanceTest extends TestCase private const float LATITUDE_B = 37.422546; private const float LONGITUDE_B = -122.084250; - #[Test] - public function canSetUnit(): void - { - $distance = new Distance(); - $distance->setUnit(Unit::MILES); - - self::assertSame(Unit::MILES, $distance->getUnit()); - } - - #[Test] - public function canSetFormula(): void - { - $distance = new Distance(); - $distance->setFormula(Formula::VINCENTY); - - self::assertSame(Formula::VINCENTY, $distance->getFormula()); - } - #[Test] #[TestWith([Unit::METRES, 11164.0])] #[TestWith([Unit::KILOMETRES, 11.164])] From 295a2e34aec1f92b520406f4595771a759cb2ab9 Mon Sep 17 00:00:00 2001 From: Luc De Brouwer Date: Mon, 23 Feb 2026 20:19:07 +0000 Subject: [PATCH 11/11] Update README.md. --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3517722..b9b96ec 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,20 @@ When using [Composer](https://getcomposer.org) you can always load in the latest ```bash { "require": { - "ldebrouwer/distance": "~0.2" + "ldebrouwer/distance": "^1.0" } } ``` Check it out [on Packagist](https://packagist.org/packages/ldebrouwer/distance). -## Documentation +## Usage -This is still on the to-do list. The code is pretty well documented though and should autocomplete in most IDEs. +```php +$distance = new Distance() + ->setFormula(Formula::HAVERSINE) + ->setUnit(Unit::KILOMETRES) + ->between(37.331741, -122.030333, 37.422546, -122.084250); +``` ## Still to come - Expanded unit support.