From 3dddfc05db25a6a62a7f9da1347604f781838307 Mon Sep 17 00:00:00 2001 From: Jens True Date: Fri, 2 Feb 2024 14:53:38 +0000 Subject: [PATCH] More code standards --- benchmark/BasicBench.php | 8 +++++++ src/FactorGraphs/Factor.php | 3 +++ src/FactorGraphs/FactorGraphLayer.php | 7 ++++-- src/FactorGraphs/FactorList.php | 2 ++ src/FactorGraphs/ScheduleLoop.php | 2 ++ src/FactorGraphs/ScheduleSequence.php | 2 ++ src/FactorGraphs/ScheduleStep.php | 2 ++ src/FactorGraphs/Variable.php | 2 ++ src/FactorGraphs/VariableFactory.php | 2 ++ src/GameInfo.php | 5 +++- src/Guard.php | 2 +- src/Numerics/DiagonalMatrix.php | 2 ++ src/Numerics/Matrix.php | 10 ++++---- src/RankSorter.php | 7 ++++-- src/SkillCalculator.php | 13 +++++++---- src/Team.php | 2 +- src/Teams.php | 2 ++ src/TeamsRange.php | 2 ++ src/TrueSkill/DrawMargin.php | 2 ++ .../Factors/GaussianWeightedSumFactor.php | 2 ++ .../IteratedTeamDifferencesInnerLayer.php | 2 ++ ...yerPerformancesToTeamPerformancesLayer.php | 4 +++- .../Layers/PlayerPriorValuesToSkillsLayer.php | 2 ++ .../PlayerSkillsToPerformancesLayer.php | 2 ++ .../Layers/TeamDifferencesComparisonLayer.php | 2 ++ ...ancesToTeamPerformanceDifferencesLayer.php | 2 ++ src/TrueSkill/TrueSkillFactorGraph.php | 10 ++++---- .../TruncatedGaussianCorrectionFunctions.php | 2 ++ .../TwoPlayerTrueSkillCalculator.php | 2 ++ src/TrueSkill/TwoTeamTrueSkillCalculator.php | 2 ++ tests/FactorGraphs/ScheduleStepTest.php | 2 ++ tests/FactorGraphs/VariableTest.php | 5 +++- tests/GuardTest.php | 6 +++-- tests/Numerics/BasicMathTest.php | 2 ++ tests/Numerics/GaussianDistributionTest.php | 16 +++++++------ tests/Numerics/MatrixTest.php | 4 +++- tests/Numerics/RangeTest.php | 4 +++- tests/PlayerTest.php | 4 +++- tests/RankSorterTest.php | 2 ++ tests/RatingTest.php | 10 ++++---- tests/TrueSkill/DrawMarginTest.php | 4 +++- ...FactorGraphTeamTrueSkillCalculatorTest.php | 6 +++-- .../FactorGraphTrueSkillCalculatorTest.php | 6 +++-- tests/TrueSkill/TrueSkillCalculatorTests.php | 23 ++++++++++--------- .../TwoPlayerTrueSkillCalculatorTest.php | 4 +++- .../TwoTeamTrueSkillCalculatorTest.php | 4 +++- 46 files changed, 156 insertions(+), 55 deletions(-) diff --git a/benchmark/BasicBench.php b/benchmark/BasicBench.php index 2303fc9..fd42365 100644 --- a/benchmark/BasicBench.php +++ b/benchmark/BasicBench.php @@ -15,6 +15,8 @@ use DNW\Skills\Teams; class BasicBench { /** + * To benchmark performance when using TwoPlayerTrueSkillCalculator + * * @Revs(20) * @Iterations(20) */ @@ -44,6 +46,8 @@ class BasicBench } /** + * To benchmark performance when using TwoTeamTrueSkillCalculator for just two players in two teams + * * @Revs(20) * @Iterations(20) */ @@ -73,6 +77,8 @@ class BasicBench } /** + * To benchmark performance when using FactorGraphTrueSkillCalculator for just two players in two teams + * * @Revs(20) * @Iterations(20) */ @@ -102,6 +108,8 @@ class BasicBench } /** + * To benchmark performance when using FactorGraphTrueSkillCalculator with 3 players in 3 teams + * * @Revs(20) * @Iterations(20) */ diff --git a/src/FactorGraphs/Factor.php b/src/FactorGraphs/Factor.php index 81b9169..d1c5636 100644 --- a/src/FactorGraphs/Factor.php +++ b/src/FactorGraphs/Factor.php @@ -1,5 +1,7 @@ > */ public function &getOutputVariablesGroups(): array @@ -82,11 +85,11 @@ abstract class FactorGraphLayer public function createPriorSchedule(): ?ScheduleSequence { - return null; + return NULL; } public function createPosteriorSchedule(): ?ScheduleSequence { - return null; + return NULL; } } diff --git a/src/FactorGraphs/FactorList.php b/src/FactorGraphs/FactorList.php index 8fec3d2..390a029 100644 --- a/src/FactorGraphs/FactorList.php +++ b/src/FactorGraphs/FactorList.php @@ -1,5 +1,7 @@ > $matrixRowData */ - public function __construct(private int $rowCount = 0, private int $columnCount = 0, private array $matrixRowData = array()) + public function __construct(private int $rowCount = 0, private int $columnCount = 0, private array $matrixRowData = []) { } @@ -307,7 +309,7 @@ class Matrix public function equals(Matrix $otherMatrix): bool { if (($this->rowCount != $otherMatrix->getRowCount()) || ($this->columnCount != $otherMatrix->getColumnCount())) { - return false; + return FALSE; } for ($currentRow = 0; $currentRow < $this->rowCount; $currentRow++) { @@ -319,11 +321,11 @@ class Matrix ); if ($delta > self::ERROR_TOLERANCE) { - return false; + return FALSE; } } } - return true; + return TRUE; } } diff --git a/src/RankSorter.php b/src/RankSorter.php index 8f8f26a..3f75a97 100644 --- a/src/RankSorter.php +++ b/src/RankSorter.php @@ -1,5 +1,7 @@ $teams The items to sort according to the order specified by ranks. - * @param array $teamRanks The ranks for each item where 1 is first place. + * @param array $teams The items to sort according to the order specified by ranks. + * @param array $teamRanks The ranks for each item where 1 is first place. + * * @return array */ public static function sort(array &$teams, array &$teamRanks): array diff --git a/src/SkillCalculator.php b/src/SkillCalculator.php index b8df1d5..5fe94cb 100644 --- a/src/SkillCalculator.php +++ b/src/SkillCalculator.php @@ -1,5 +1,7 @@ (string) ($currentPlayer->getKey()), $team); + $memberNames = array_map(fn ($currentPlayer) => (string)($currentPlayer->getKey()), $team); $teamMemberNames = \implode(', ', $memberNames); diff --git a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index ac1b3d5..e359164 100644 --- a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -1,5 +1,7 @@ layers; foreach ($layers as $currentLayer) { - if ($lastOutput != null) { + if ($lastOutput != NULL) { $currentLayer->setInputVariablesGroups($lastOutput); } @@ -105,7 +107,7 @@ class TrueSkillFactorGraph extends FactorGraph $layers = $this->layers; foreach ($layers as $currentLayer) { $currentPriorSchedule = $currentLayer->createPriorSchedule(); - if ($currentPriorSchedule != null) { + if ($currentPriorSchedule != NULL) { $fullSchedule[] = $currentPriorSchedule; } } @@ -114,7 +116,7 @@ class TrueSkillFactorGraph extends FactorGraph foreach ($allLayersReverse as $currentLayer) { $currentPosteriorSchedule = $currentLayer->createPosteriorSchedule(); - if ($currentPosteriorSchedule != null) { + if ($currentPosteriorSchedule != NULL) { $fullSchedule[] = $currentPosteriorSchedule; } } diff --git a/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php b/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php index c52cfc8..9cfdc89 100644 --- a/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php +++ b/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php @@ -1,5 +1,7 @@ assertEquals($gd_prior, $var->getValue()); + $gd_new = new GaussianDistribution(); $this->assertEquals($gd_new, $var->getValue()); $var->resetToPrior(); diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 18d95af..285e965 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -1,4 +1,6 @@ -expectException(Exception::class); $this->expectExceptionMessage('dummy can not be null'); - Guard::argumentNotNull(null, "dummy"); + Guard::argumentNotNull(NULL, "dummy"); } public function testargumentIsValidIndex(): void diff --git a/tests/Numerics/BasicMathTest.php b/tests/Numerics/BasicMathTest.php index 6574a73..24085c0 100644 --- a/tests/Numerics/BasicMathTest.php +++ b/tests/Numerics/BasicMathTest.php @@ -1,5 +1,7 @@ assertEqualsWithDelta(0.2, $product->getMean(), GaussianDistributionTest::ERROR_TOLERANCE); $this->assertEqualsWithDelta(3.0 / sqrt(10), $product->getStandardDeviation(), GaussianDistributionTest::ERROR_TOLERANCE); @@ -61,15 +63,15 @@ class GaussianDistributionTest extends TestCase public function testDivision(): void { // Since the multiplication was worked out by hand, we use the same numbers but work backwards - $product = new GaussianDistribution(0.2, 3.0 / sqrt(10)); + $product = new GaussianDistribution(0.2, 3.0 / sqrt(10)); $standardNormal = new GaussianDistribution(0, 1); $productDividedByStandardNormal = GaussianDistribution::divide($product, $standardNormal); $this->assertEqualsWithDelta(2.0, $productDividedByStandardNormal->getMean(), GaussianDistributionTest::ERROR_TOLERANCE); $this->assertEqualsWithDelta(3.0, $productDividedByStandardNormal->getStandardDeviation(), GaussianDistributionTest::ERROR_TOLERANCE); - $product2 = new GaussianDistribution((4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7)), sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7))))); - $m4s5 = new GaussianDistribution(4, 5); + $product2 = new GaussianDistribution((4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7)), sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7))))); + $m4s5 = new GaussianDistribution(4, 5); $product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5); $this->assertEqualsWithDelta(6.0, $product2DividedByM4S5->getMean(), GaussianDistributionTest::ERROR_TOLERANCE); $this->assertEqualsWithDelta(7.0, $product2DividedByM4S5->getStandardDeviation(), GaussianDistributionTest::ERROR_TOLERANCE); @@ -93,7 +95,7 @@ class GaussianDistributionTest extends TestCase // Verified with Ralf Herbrich's F# implementation $m1s2 = new GaussianDistribution(1, 2); $m3s4 = new GaussianDistribution(3, 4); - $lrn = GaussianDistribution::logRatioNormalization($m1s2, $m3s4); + $lrn = GaussianDistribution::logRatioNormalization($m1s2, $m3s4); $this->assertEqualsWithDelta(2.6157405972171204, $lrn, GaussianDistributionTest::ERROR_TOLERANCE); } @@ -101,7 +103,7 @@ class GaussianDistributionTest extends TestCase { // Verified with Ralf Herbrich's F# implementation $standardNormal = new GaussianDistribution(0, 1); - $absDiff = GaussianDistribution::absoluteDifference($standardNormal, $standardNormal); + $absDiff = GaussianDistribution::absoluteDifference($standardNormal, $standardNormal); $this->assertEqualsWithDelta(0.0, $absDiff, GaussianDistributionTest::ERROR_TOLERANCE); $m1s2 = new GaussianDistribution(1, 2); diff --git a/tests/Numerics/MatrixTest.php b/tests/Numerics/MatrixTest.php index 70843a1..56bceaa 100644 --- a/tests/Numerics/MatrixTest.php +++ b/tests/Numerics/MatrixTest.php @@ -1,4 +1,6 @@ -getPartialUpdate($rating_prior, $rating_new, 0.5); + $rating_partial = $rating->getPartialUpdate($ratingOld, $ratingNew, 0.5); $this->assertEquals(150, $rating_partial->getMean()); diff --git a/tests/TrueSkill/DrawMarginTest.php b/tests/TrueSkill/DrawMarginTest.php index 099a995..4584f9e 100644 --- a/tests/TrueSkill/DrawMarginTest.php +++ b/tests/TrueSkill/DrawMarginTest.php @@ -1,4 +1,6 @@ -assertEquals(true, $calculator->isSupported(SkillCalculatorSupportedOptions::PARTIAL_PLAY)); + $this->assertEquals(TRUE, $calculator->isSupported(SkillCalculatorSupportedOptions::PARTIAL_PLAY)); } } diff --git a/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php b/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php index 8d07888..54227df 100644 --- a/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php +++ b/tests/TrueSkill/FactorGraphTrueSkillCalculatorTest.php @@ -1,4 +1,6 @@ -getDefaultRating()); } diff --git a/tests/TrueSkill/TrueSkillCalculatorTests.php b/tests/TrueSkill/TrueSkillCalculatorTests.php index 4ef743e..680a9e0 100644 --- a/tests/TrueSkill/TrueSkillCalculatorTests.php +++ b/tests/TrueSkill/TrueSkillCalculatorTests.php @@ -1,4 +1,6 @@ -getDefaultRating()); @@ -108,6 +110,7 @@ class TrueSkillCalculatorTests $team2 = new Team($player2, $gameInfo->getDefaultRating()); $teams = Teams::concat($team1, $team2); + $newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 1]); $player1NewRating = $newRatings->getRating($player1); @@ -122,8 +125,8 @@ class TrueSkillCalculatorTests private static function twoPlayerChessTestNotDrawn(TestCase $testClass, SkillCalculator $calculator): void { // Inspired by a real bug :-) - $player1 = new Player(1); - $player2 = new Player(2); + $player1 = new Player(1); + $player2 = new Player(2); $gameInfo = new GameInfo(1200.0, 1200.0 / 3.0, 200.0, 1200.0 / 300.0, 0.03); $team1 = new Team($player1, new Rating(1301.0007, 42.9232)); @@ -169,11 +172,9 @@ class TrueSkillCalculatorTests private static function oneOnTwoSimpleTest(TestCase $testClass, SkillCalculator $calculator): void { - $player1 = new Player(1); - + $player1 = new Player(1); $gameInfo = new GameInfo(); - - $team1 = new Team(); + $team1 = new Team(); $team1->addPlayer($player1, $gameInfo->getDefaultRating()); $player2 = new Player(2); @@ -975,7 +976,7 @@ class TrueSkillCalculatorTests { $gameInfo = new GameInfo(); - $p1 = new Player(1); + $p1 = new Player(1); $team1 = new Team($p1, $gameInfo->getDefaultRating()); $p2 = new Player(2, 0.0); @@ -985,7 +986,7 @@ class TrueSkillCalculatorTests $team2->addPlayer($p2, $gameInfo->getDefaultRating()); $team2->addPlayer($p3, $gameInfo->getDefaultRating()); - $teams = Teams::concat($team1, $team2); + $teams = Teams::concat($team1, $team2); $newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2]); $p1NewRating = $newRatings->getRating($p1); diff --git a/tests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php b/tests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php index 97b8466..43b63d6 100644 --- a/tests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php +++ b/tests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php @@ -1,4 +1,6 @@ -