Skip to content

Commit

Permalink
Add PHPMD to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Jul 26, 2019
1 parent 0bd6edd commit 6f9aed8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions phpmd.xml
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<ruleset xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd">

<rule ref="rulesets/codesize.xml">
<exclude name="ExcessiveClassComplexity" />
<exclude name="TooManyMethods" />
<exclude name="TooManyPublicMethods" />
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="maxmethods" value="20" />
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="maxmethods" value="15" />
</properties>
</rule>

<!-- todo: exclude the camel case method rule for the tests -->
<rule ref="rulesets/controversial.xml" />

<rule ref="rulesets/design.xml">
<exclude name="CouplingBetweenObjects" />
</rule>

<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="LongVariable" />
</rule>

<rule ref="rulesets/unusedcode.xml">
<exclude name="UnusedLocalVariable" />
</rule>
</ruleset>
18 changes: 13 additions & 5 deletions src/Formatters/LatLongFormatter.php
Expand Up @@ -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 ) {
Expand All @@ -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 );
Expand Down
20 changes: 12 additions & 8 deletions src/GlobeMath.php
Expand Up @@ -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.
Expand All @@ -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;
}

}

0 comments on commit 6f9aed8

Please sign in to comment.