From 6f9aed83b9cb147457ed0c1cf30c5cec8bbcf994 Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Sun, 21 Jul 2019 08:15:14 +0200 Subject: [PATCH] Add PHPMD to CI --- composer.json | 6 +++-- phpmd.xml | 38 +++++++++++++++++++++++++++++ src/Formatters/LatLongFormatter.php | 18 ++++++++++---- src/GlobeMath.php | 20 +++++++++------ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 phpmd.xml diff --git a/composer.json b/composer.json index ef9ae07..d33c291 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,8 @@ "jeroen/nyancat-phpunit-resultprinter": "~2.2", "wikibase/wikibase-codesniffer": "^0.5.0", "ockcyp/covers-validator": "~1.0", - "phpstan/phpstan": "~0.11.0" + "phpstan/phpstan": "~0.11.0", + "phpmd/phpmd": "^2.6.1" }, "autoload": { "psr-4": { @@ -71,7 +72,8 @@ ], "cs": [ "vendor/bin/phpcs -p -s", - "vendor/bin/phpstan analyse --level=1 --no-progress src/ tests/" + "vendor/bin/phpstan analyse --level=1 --no-progress src/ tests/", + "vendor/bin/phpmd src/ text phpmd.xml" ], "ci": [ "@test", diff --git a/phpmd.xml b/phpmd.xml new file mode 100644 index 0000000..1c6b27a --- /dev/null +++ b/phpmd.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Formatters/LatLongFormatter.php b/src/Formatters/LatLongFormatter.php index 94e7828..e000743 100644 --- a/src/Formatters/LatLongFormatter.php +++ b/src/Formatters/LatLongFormatter.php @@ -238,11 +238,7 @@ private function formatCoordinate( float $degrees, float $precision ): string { } if ( $format !== self::TYPE_DD ) { - if ( $precision >= 1 - 1 / 60 && $precision < 1 ) { - $precision = 1; - } elseif ( $precision >= 1 / 60 - 1 / 3600 && $precision < 1 / 60 ) { - $precision = 1 / 60; - } + $precision = $this->getUpdatedPrecision( $precision ); } if ( $format === self::TYPE_DD || $precision >= 1 ) { @@ -258,6 +254,18 @@ private function formatCoordinate( float $degrees, float $precision ): string { throw new InvalidArgumentException( 'Invalid coordinate format specified in the options' ); } + private function getUpdatedPrecision( float $precision ): float { + if ( $precision >= 1 - 1 / 60 && $precision < 1 ) { + return 1; + } + + if ( $precision >= 1 / 60 - 1 / 3600 && $precision < 1 / 60 ) { + return 1 / 60; + } + + return $precision; + } + private function roundDegrees( float $degrees, float $precision ): float { $sign = $degrees > 0 ? 1 : -1; $reduced = round( abs( $degrees ) / $precision ); diff --git a/src/GlobeMath.php b/src/GlobeMath.php index 984368b..23a2bd3 100644 --- a/src/GlobeMath.php +++ b/src/GlobeMath.php @@ -79,15 +79,8 @@ public function normalizeGlobeLatLong( LatLongValue $value, string $globe = null * @return LatLongValue */ public function normalizeLatLong( LatLongValue $value, float $minimumLongitude = -180.0 ): LatLongValue { + $lon = $this->getNormalizedLongitude( $value->getLongitude(), $minimumLongitude ); $lat = $value->getLatitude(); - $lon = $value->getLongitude(); - - // Normalize to [-180°..+180°[ on Earth/Moon, [0°..+360°[ on other globes. - if ( $lon >= $minimumLongitude + 360 ) { - $lon -= 360; - } elseif ( $lon < $minimumLongitude ) { - $lon += 360; - } if ( $lat >= 270 ) { // Same side of the globe, on the southern hemisphere. @@ -113,4 +106,15 @@ public function normalizeLatLong( LatLongValue $value, float $minimumLongitude = return new LatLongValue( $lat, $lon ); } + private function getNormalizedLongitude( float $longitude, float $minimumLongitude ): float { + // Normalize to [-180°..+180°[ on Earth/Moon, [0°..+360°[ on other globes. + if ( $longitude >= $minimumLongitude + 360 ) { + $longitude -= 360; + } elseif ( $longitude < $minimumLongitude ) { + $longitude += 360; + } + + return $longitude; + } + }