From ec000870259c62a4c3ba66362c7b957a3290a9ae Mon Sep 17 00:00:00 2001 From: Jens True Date: Thu, 29 Feb 2024 10:42:31 +0000 Subject: [PATCH] More pedantric types. --- composer.json | 4 +++- docs/index.rst | 2 +- examples/3teams.php | 2 +- examples/basic.php | 2 +- src/FactorGraphs/FactorGraph.php | 2 +- src/FactorGraphs/FactorGraphLayer.php | 10 +++++----- src/Guard.php | 7 ------- src/TrueSkill/FactorGraphTrueSkillCalculator.php | 1 - .../Layers/PlayerSkillsToPerformancesLayer.php | 8 ++++++-- src/TrueSkill/TrueSkillFactorGraph.php | 4 +++- src/TrueSkill/TwoPlayerTrueSkillCalculator.php | 2 -- src/TrueSkill/TwoTeamTrueSkillCalculator.php | 2 -- tests/GuardTest.php | 7 ------- 13 files changed, 21 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 3976e4b..4357c49 100644 --- a/composer.json +++ b/composer.json @@ -34,10 +34,12 @@ ], "static": [ "@analyze-phpstan", - "@analyze-psalm" + "@analyze-psalm", + "@analyze-rector" ], "analyze-phpstan":"vendor/bin/phpstan analyze --error-format=raw", "analyze-psalm": "vendor/bin/psalm --no-cache", + "analyze-rector": "vendor/bin/rector --dry-run", "html": [ "pandoc -s README.md -o output/README.html", "pandoc -s docs/index.rst -o output/index.html" diff --git a/docs/index.rst b/docs/index.rst index fc09db3..a739a1c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -47,6 +47,6 @@ PHP Tools --------- For development Composer and the following packages are used (Recommended as Phars installed via Phive) -* sudo phive install -g composer phpdocumentor infection phpcs phpcbf phploc phpbench overtrue/phplint +* sudo phive install -g composer phpdocumentor infection phpcs phpcbf phploc phpbench overtrue/phplint --force-accept-unsigned * composer install * composer all \ No newline at end of file diff --git a/examples/3teams.php b/examples/3teams.php index da37ae9..2113fb9 100644 --- a/examples/3teams.php +++ b/examples/3teams.php @@ -1,6 +1,6 @@ variableFactory = new VariableFactory(fn () => NULL); + $this->variableFactory = new VariableFactory(static fn(): null => NULL); } public function getVariableFactory(): VariableFactory diff --git a/src/FactorGraphs/FactorGraphLayer.php b/src/FactorGraphs/FactorGraphLayer.php index 4b7ed59..71c71ae 100644 --- a/src/FactorGraphs/FactorGraphLayer.php +++ b/src/FactorGraphs/FactorGraphLayer.php @@ -15,12 +15,12 @@ abstract class FactorGraphLayer private array $localFactors = []; /** - * @var array> + * @var array> */ private array $outputVariablesGroups = []; /** - * @var array> + * @var array> */ private array $inputVariablesGroups = []; @@ -29,7 +29,7 @@ abstract class FactorGraphLayer } /** - * @return array> + * @return array> */ protected function getInputVariablesGroups(): array { @@ -44,7 +44,7 @@ abstract class FactorGraphLayer /** * This reference is still needed * - * @return array> + * @return array> */ public function &getOutputVariablesGroups(): array { @@ -60,7 +60,7 @@ abstract class FactorGraphLayer } /** - * @param array> $value + * @param array> $value */ public function setInputVariablesGroups(array $value): void { diff --git a/src/Guard.php b/src/Guard.php index dc555b8..01b748b 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -13,13 +13,6 @@ use Exception; */ class Guard { - public static function argumentNotNull(mixed $value, string $parameterName): void - { - if ($value == NULL) { - throw new Exception($parameterName . ' can not be null'); - } - } - public static function argumentIsValidIndex(int $index, int $count, string $parameterName): void { if (($index < 0) || ($index >= $count)) { diff --git a/src/TrueSkill/FactorGraphTrueSkillCalculator.php b/src/TrueSkill/FactorGraphTrueSkillCalculator.php index 1a2ec3e..de4cb76 100644 --- a/src/TrueSkill/FactorGraphTrueSkillCalculator.php +++ b/src/TrueSkill/FactorGraphTrueSkillCalculator.php @@ -39,7 +39,6 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator array $teamRanks ): RatingContainer { - Guard::argumentNotNull($gameInfo, 'gameInfo'); $this->validateTeamCountAndPlayersCountPerTeam($teams); RankSorter::sort($teams, $teamRanks); diff --git a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index bca879a..2da4e09 100644 --- a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace DNW\Skills\TrueSkill\Layers; use DNW\Skills\FactorGraphs\KeyedVariable; +use DNW\Skills\FactorGraphs\Variable; use DNW\Skills\FactorGraphs\ScheduleStep; use DNW\Skills\Numerics\BasicMath; use DNW\Skills\TrueSkill\Factors\GaussianLikelihoodFactor; @@ -20,9 +21,12 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer foreach ($inputVariablesGroups as $currentTeam) { $currentTeamPlayerPerformances = []; + /** + * @var Variable $playerSkillVariable + */ foreach ($currentTeam as $playerSkillVariable) { $localPlayerSkillVariable = $playerSkillVariable; - $currentPlayer = $localPlayerSkillVariable->getKey(); + $currentPlayer = ($localPlayerSkillVariable instanceof KeyedVariable) ? $localPlayerSkillVariable->getKey() : ""; $playerPerformance = $this->createOutputVariable($currentPlayer); $newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance); $this->addLayerFactor($newLikelihoodFactor); @@ -33,7 +37,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer } } - private function createLikelihood(KeyedVariable $playerSkill, KeyedVariable $playerPerformance): GaussianLikelihoodFactor + private function createLikelihood(Variable $playerSkill, Variable $playerPerformance): GaussianLikelihoodFactor { return new GaussianLikelihoodFactor( BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()), diff --git a/src/TrueSkill/TrueSkillFactorGraph.php b/src/TrueSkill/TrueSkillFactorGraph.php index 0be279c..0c8dc4c 100644 --- a/src/TrueSkill/TrueSkillFactorGraph.php +++ b/src/TrueSkill/TrueSkillFactorGraph.php @@ -12,8 +12,10 @@ use DNW\Skills\GameInfo; use DNW\Skills\Numerics\GaussianDistribution; use DNW\Skills\Rating; use DNW\Skills\Team; +use DNW\Skills\Player; use DNW\Skills\RatingContainer; use DNW\Skills\FactorGraphs\FactorGraphLayer; +use DNW\Skills\FactorGraphs\KeyedVariable; use DNW\Skills\TrueSkill\Layers\IteratedTeamDifferencesInnerLayer; use DNW\Skills\TrueSkill\Layers\PlayerPerformancesToTeamPerformancesLayer; use DNW\Skills\TrueSkill\Layers\PlayerPriorValuesToSkillsLayer; @@ -132,7 +134,7 @@ class TrueSkillFactorGraph extends FactorGraph $priorLayerOutputVariablesGroups = $this->priorLayer->getOutputVariablesGroups(); foreach ($priorLayerOutputVariablesGroups as $currentTeam) { foreach ($currentTeam as $currentPlayer) { - $localCurrentPlayer = $currentPlayer->getKey(); + $localCurrentPlayer = ($currentPlayer instanceof KeyedVariable) ? $currentPlayer->getKey() : new Player(""); $newRating = new Rating( $currentPlayer->getValue()->getMean(), $currentPlayer->getValue()->getStandardDeviation() diff --git a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php index 9982f62..0528e28 100644 --- a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php +++ b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php @@ -40,7 +40,6 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator ): RatingContainer { // Basic argument checking - Guard::argumentNotNull($gameInfo, 'gameInfo'); $this->validateTeamCountAndPlayersCountPerTeam($teams); // Make sure things are in order @@ -143,7 +142,6 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator */ public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float { - Guard::argumentNotNull($gameInfo, 'gameInfo'); $this->validateTeamCountAndPlayersCountPerTeam($teams); $team1 = $teams[0]; diff --git a/src/TrueSkill/TwoTeamTrueSkillCalculator.php b/src/TrueSkill/TwoTeamTrueSkillCalculator.php index 2a1fba1..31950d7 100644 --- a/src/TrueSkill/TwoTeamTrueSkillCalculator.php +++ b/src/TrueSkill/TwoTeamTrueSkillCalculator.php @@ -34,7 +34,6 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator */ public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks): RatingContainer { - Guard::argumentNotNull($gameInfo, 'gameInfo'); $this->validateTeamCountAndPlayersCountPerTeam($teams); RankSorter::sort($teams, $teamRanks); @@ -150,7 +149,6 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator */ public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float { - Guard::argumentNotNull($gameInfo, 'gameInfo'); $this->validateTeamCountAndPlayersCountPerTeam($teams); // We've verified that there's just two teams diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 285e965..b5a418b 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -10,13 +10,6 @@ use PHPUnit\Framework\TestCase; class GuardTest extends TestCase { - public function testArgumentNotNull(): void - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('dummy can not be null'); - Guard::argumentNotNull(NULL, "dummy"); - } - public function testargumentIsValidIndex(): void { $this->expectException(Exception::class);