Skip to content

Commit

Permalink
install new PHP CodeSniffer config; adjust sources to pass phpcs test
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaschen committed Mar 22, 2019
1 parent 1687ac7 commit 69df86d
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 384 deletions.
104 changes: 0 additions & 104 deletions codesniffer_rules.xml

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"scripts": {
"ci:lint": "./vendor/bin/parallel-lint src",
"ci:psalm": "./vendor/bin/psalm",
"ci:sniff": "./vendor/bin/phpcs --standard=codesniffer_rules.xml src || true",
"ci:sniff": "./vendor/bin/phpcs src tests",
"ci:coding-standards": "./vendor/bin/phpmd src text cleancode,codesize,design,naming,unusedcode || true",
"ci:tests": "./vendor/bin/phpunit tests/",
"ci:static": [
Expand Down
124 changes: 124 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Coding Standard">
<description>
This standard requires PHP_CodeSniffer >= 3.4.
</description>

<arg name="colors"/>
<arg name="extensions" value="php"/>

<!--The complete PSR-2 rule set-->
<rule ref="PSR2"/>

<!--The complete PSR-12 rule set-->
<rule ref="PSR12"/>

<!-- Arrays -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>

<!-- Classes -->
<rule ref="Generic.Classes.DuplicateClassName"/>
<rule ref="Squiz.Classes.ClassFileName"/>
<rule ref="Squiz.Classes.DuplicateProperty"/>
<rule ref="Squiz.Classes.LowercaseClassKeywords"/>
<rule ref="Squiz.Classes.SelfMemberReference"/>

<!-- Code analysis -->
<!--<rule ref="Generic.CodeAnalysis.AssignmentInCondition"/>-->
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>

<!-- Commenting -->
<rule ref="Generic.Commenting.Fixme"/>
<!--<rule ref="Generic.Commenting.Todo"/>-->
<rule ref="PEAR.Commenting.InlineComment"/>
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
<rule ref="Squiz.Commenting.EmptyCatchComment"/>
<!--<rule ref="Squiz.Commenting.FunctionCommentThrowTag"/>-->
<!--<rule ref="Squiz.Commenting.PostStatementComment"/>-->

<!-- Control structures -->
<rule ref="PEAR.ControlStructures.ControlSignature"/>

<!-- Debug -->
<rule ref="Generic.Debug.ClosureLinter"/>

<!-- Files -->
<rule ref="Generic.Files.OneClassPerFile"/>
<rule ref="Generic.Files.OneInterfacePerFile"/>
<rule ref="Generic.Files.OneObjectStructurePerFile"/>
<rule ref="Zend.Files.ClosingTag"/>

<!-- Formatting -->
<rule ref="PEAR.Formatting.MultiLineAssignment"/>

<!-- Functions -->
<rule ref="Generic.Functions.CallTimePassByReference"/>
<rule ref="PSR12.Functions.NullableTypeDeclaration"/>
<rule ref="Squiz.Functions.FunctionDuplicateArgument"/>
<rule ref="Squiz.Functions.GlobalFunction"/>

<!-- Metrics -->
<!--<rule ref="Generic.Metrics.CyclomaticComplexity"/>-->
<rule ref="Generic.Metrics.NestingLevel"/>

<!-- Naming conventions -->
<rule ref="Generic.NamingConventions.ConstructorName"/>
<rule ref="PEAR.NamingConventions.ValidClassName"/>

<!-- Objects -->
<rule ref="Squiz.Objects.ObjectMemberComma"/>

<!-- Operators -->
<rule ref="Squiz.Operators.IncrementDecrementUsage"/>
<rule ref="Squiz.Operators.ValidLogicalOperators"/>

<!-- PHP -->
<rule ref="Generic.PHP.BacktickOperator"/>
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Generic.PHP.DisallowAlternativePHPTags"/>
<rule ref="Generic.PHP.DiscourageGoto"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Generic.PHP.NoSilencedErrors"/>
<rule ref="Squiz.PHP.CommentedOutCode">
<properties>
<property name="maxPercentage" value="70"/>
</properties>
</rule>
<rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
<rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
<rule ref="Squiz.PHP.Eval"/>
<rule ref="Squiz.PHP.GlobalKeyword"/>
<rule ref="Squiz.PHP.Heredoc"/>
<rule ref="Squiz.PHP.InnerFunctions"/>
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>

<!-- Scope -->
<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>

<!-- Strings -->
<!--<rule ref="Squiz.Strings.DoubleQuoteUsage"/>-->

<!-- Whitespace -->
<rule ref="PEAR.WhiteSpace.ObjectOperatorIndent"/>
<rule ref="PEAR.WhiteSpace.ScopeClosingBrace"/>
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.PropertyLabelSpacing"/>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
</ruleset>
86 changes: 52 additions & 34 deletions src/Bearing/BearingEllipsoidal.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public function calculateDestinationFinalBearing(Coordinate $point, float $beari
*/
private function directVincenty(Coordinate $point, float $bearing, float $distance): DirectVincentyBearing
{
$phi1 = deg2rad($point->getLat());
$phi1 = deg2rad($point->getLat());
$lambda1 = deg2rad($point->getLng());
$alpha1 = deg2rad($bearing);
$alpha1 = deg2rad($bearing);

$a = $point->getEllipsoid()->getA();
$b = $point->getEllipsoid()->getB();
Expand All @@ -103,38 +103,50 @@ private function directVincenty(Coordinate $point, float $bearing, float $distan
$sinAlpha1 = sin($alpha1);
$cosAlpha1 = cos($alpha1);

$tanU1 = (1 - $f) * tan($phi1);
$cosU1 = 1 / sqrt(1 + $tanU1 * $tanU1);
$sinU1 = $tanU1 * $cosU1;
$sigma1 = atan2($tanU1, $cosAlpha1);
$sinAlpha = $cosU1 * $sinAlpha1;
$tanU1 = (1 - $f) * tan($phi1);
$cosU1 = 1 / sqrt(1 + $tanU1 * $tanU1);
$sinU1 = $tanU1 * $cosU1;
$sigma1 = atan2($tanU1, $cosAlpha1);
$sinAlpha = $cosU1 * $sinAlpha1;
$cosSquAlpha = 1 - $sinAlpha * $sinAlpha;
$uSq = $cosSquAlpha * ($a * $a - $b * $b) / ($b * $b);
$A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq)));
$B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq)));
$uSq = $cosSquAlpha * ($a * $a - $b * $b) / ($b * $b);
$A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq)));
$B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq)));

$sigmaS = $distance / ($b * $A);
$sigma = $sigmaS;
$sigmaS = $distance / ($b * $A);
$sigma = $sigmaS;
$iterations = 0;

do {
$cos2SigmaM = cos(2 * $sigma1 + $sigma);
$sinSigma = sin($sigma);
$cosSigma = cos($sigma);
$deltaSigma = $B * $sinSigma * ($cos2SigmaM + $B / 4 * ($cosSigma * (-1 + 2 * $cos2SigmaM * $cos2SigmaM) - $B / 6 * $cos2SigmaM * (-3 + 4 * $sinSigma * $sinSigma) * (-3 + 4 * $cos2SigmaM * $cos2SigmaM)));
$sigmaS = $sigma;
$sigma = $distance / ($b * $A) + $deltaSigma;
$sinSigma = sin($sigma);
$cosSigma = cos($sigma);
$deltaSigma = $B * $sinSigma
* ($cos2SigmaM + $B / 4
* ($cosSigma
* (-1 + 2 * $cos2SigmaM * $cos2SigmaM) - $B / 6
* $cos2SigmaM * (-3 + 4 * $sinSigma * $sinSigma)
* (-3 + 4 * $cos2SigmaM * $cos2SigmaM)
)
);
$sigmaS = $sigma;
$sigma = $distance / ($b * $A) + $deltaSigma;
} while (abs($sigma - $sigmaS) > 1e-12 && ++$iterations < 200);

if ($iterations >= 200) {
throw new NotConvergingException('Inverse Vincenty Formula did not converge');
}

$tmp = $sinU1 * $sinSigma - $cosU1 * $cosSigma * $cosAlpha1;
$phi2 = atan2($sinU1 * $cosSigma + $cosU1 * $sinSigma * $cosAlpha1, (1 - $f) * sqrt($sinAlpha * $sinAlpha + $tmp * $tmp));
$lambda = atan2($sinSigma * $sinAlpha1, $cosU1 * $cosSigma - $sinU1 * $sinSigma * $cosAlpha1);
$C = $f / 16 * $cosSquAlpha * (4 + $f * (4 - 3 * $cosSquAlpha));
$L = $lambda - (1 - $C) * $f * $sinAlpha * ($sigma + $C * $sinSigma * ($cos2SigmaM + $C * $cosSigma * (-1 + 2 * $cos2SigmaM * $cos2SigmaM)));
$tmp = $sinU1 * $sinSigma - $cosU1 * $cosSigma * $cosAlpha1;
$phi2 = atan2(
$sinU1 * $cosSigma + $cosU1 * $sinSigma * $cosAlpha1,
(1 - $f) * sqrt($sinAlpha * $sinAlpha + $tmp * $tmp)
);
$lambda = atan2($sinSigma * $sinAlpha1, $cosU1 * $cosSigma - $sinU1 * $sinSigma * $cosAlpha1);
$C = $f / 16 * $cosSquAlpha * (4 + $f * (4 - 3 * $cosSquAlpha));
$L = $lambda
- (1 - $C) * $f * $sinAlpha
* ($sigma + $C * $sinSigma * ($cos2SigmaM + $C * $cosSigma * (-1 + 2 * $cos2SigmaM ** 2)));
$lambda2 = fmod($lambda1 + $L + 3 * M_PI, 2 * M_PI) - M_PI;

$alpha2 = atan2($sinAlpha, -$tmp);
Expand Down Expand Up @@ -179,39 +191,45 @@ private function inverseVincenty(Coordinate $point1, Coordinate $point2): Invers
$iterations = 0;

do {
$sinλ = sin($λ);
$cosλ = cos($λ);
$sinλ = sin($λ);
$cosλ = cos($λ);
$sinSqσ = ($cosU2 * $sinλ) * ($cosU2 * $sinλ)
+ ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ) * ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ);
$sinσ = sqrt($sinSqσ);
+ ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ) * ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ);
$sinσ = sqrt($sinSqσ);

if ($sinσ == 0) {
new InverseVincentyBearing(0, 0, 0);
}

$cosσ = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosλ;
$σ = atan2($sinσ, $cosσ);
$sinα = $cosU1 * $cosU2 * $sinλ / $sinσ;
$cosσ = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosλ;
$σ = atan2($sinσ, $cosσ);
$sinα = $cosU1 * $cosU2 * $sinλ / $sinσ;
$cosSqα = 1 - $sinα * $sinα;

$cos2σM = 0;
if ($cosSqα !== 0.0) {
$cos2σM = $cosσ - 2 * $sinU1 * $sinU2 / $cosSqα;
}

$C = $f / 16 * $cosSqα * (4 + $f * (4 - 3 * $cosSqα));
$C = $f / 16 * $cosSqα * (4 + $f * (4 - 3 * $cosSqα));
$λp = $λ;
$λ = $L + (1 - $C) * $f * $sinα * ($σ + $C * $sinσ * ($cos2σM + $C * $cosσ * (-1 + 2 * $cos2σM * $cos2σM)));
$λ = $L + (1 - $C) * $f * $sinα * ($σ + $C * $sinσ * ($cos2σM + $C * $cosσ * (-1 + 2 * $cos2σM * $cos2σM)));
} while (abs($λ - $λp) > 1e-12 && ++$iterations < 200);

if ($iterations >= 200) {
throw new NotConvergingException('Inverse Vincenty Formula did not converge');
}

$uSq = $cosSqα * ($a * $a - $b * $b) / ($b * $b);
$A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq)));
$B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq)));
$Δσ = $B * $sinσ * ($cos2σM + $B / 4 * ($cosσ * (-1 + 2 * $cos2σM * $cos2σM) - $B / 6 * $cos2σM * (-3 + 4 * $sinσ * $sinσ) * (-3 + 4 * $cos2σM * $cos2σM)));
$A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq)));
$B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq)));
$Δσ = $B * $sinσ
* ($cos2σM + $B / 4
* ($cosσ * (-1 + 2 * $cos2σM * $cos2σM) - $B / 6
* $cos2σM * (-3 + 4 * $sinσ * $sinσ)
* (-3 + 4 * $cos2σM * $cos2σM)
)
);

$s = $b * $A * ($σ - $Δσ);

Expand Down
2 changes: 1 addition & 1 deletion src/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getLng(): float
/**
* Returns an array containing the point
*
* @return array
* @return Coordinate[]
*/
public function getPoints(): array
{
Expand Down
Loading

0 comments on commit 69df86d

Please sign in to comment.