From 73781e90001f7dfd3f449d9cdc9ba878fb9ffd9e Mon Sep 17 00:00:00 2001 From: Jens True Date: Wed, 2 Aug 2023 13:19:35 +0000 Subject: [PATCH] More type work --- src/FactorGraphs/Factor.php | 12 +++++++++--- src/FactorGraphs/VariableFactory.php | 2 +- src/GameInfo.php | 12 ++++++------ src/Numerics/GaussianDistribution.php | 6 +++--- src/Numerics/Range.php | 2 +- src/Numerics/SquareMatrix.php | 2 +- src/PartialPlay.php | 3 +-- src/PlayersRange.php | 2 +- src/RankSorter.php | 2 +- src/RatingContainer.php | 6 ++++++ src/SkillCalculator.php | 4 ++-- src/TeamsRange.php | 2 +- src/TrueSkill/Factors/GaussianWeightedSumFactor.php | 5 ++--- 13 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/FactorGraphs/Factor.php b/src/FactorGraphs/Factor.php index 2f1b1cf..81b9169 100644 --- a/src/FactorGraphs/Factor.php +++ b/src/FactorGraphs/Factor.php @@ -44,11 +44,17 @@ abstract class Factor implements \Stringable return count($this->messages); } + /** + * @return Variable[] + */ protected function getVariables(): array { return $this->variables; } + /** + * @return Message[] + */ protected function getMessages(): array { return $this->messages; @@ -61,7 +67,7 @@ abstract class Factor implements \Stringable * * @throws Exception */ - public function updateMessageIndex(int $messageIndex) + public function updateMessageIndex(int $messageIndex): float { Guard::argumentIsValidIndex($messageIndex, count($this->messages), 'messageIndex'); $message = $this->messages[$messageIndex]; @@ -78,7 +84,7 @@ abstract class Factor implements \Stringable /** * Resets the marginal of the variables a factor is connected to */ - public function resetMarginals() + public function resetMarginals(): void { $allValues = $this->messageToVariableBinding->getAllValues(); foreach ($allValues as $currentVariable) { @@ -102,7 +108,7 @@ abstract class Factor implements \Stringable abstract protected function sendMessageVariable(Message $message, Variable $variable): float|int; - abstract public function createVariableToMessageBinding(Variable $variable); + abstract public function createVariableToMessageBinding(Variable $variable): Message; protected function createVariableToMessageBindingWithMessage(Variable $variable, Message $message): Message { diff --git a/src/FactorGraphs/VariableFactory.php b/src/FactorGraphs/VariableFactory.php index 5163ca2..9790770 100644 --- a/src/FactorGraphs/VariableFactory.php +++ b/src/FactorGraphs/VariableFactory.php @@ -4,7 +4,7 @@ namespace DNW\Skills\FactorGraphs; class VariableFactory { - public function __construct(private $variablePriorInitializer) + public function __construct(private \Closure $variablePriorInitializer) { } diff --git a/src/GameInfo.php b/src/GameInfo.php index 1daf746..3f2e8e9 100644 --- a/src/GameInfo.php +++ b/src/GameInfo.php @@ -26,32 +26,32 @@ class GameInfo ) { } - public function getInitialMean() + public function getInitialMean(): float { return $this->initialMean; } - public function getInitialStandardDeviation() + public function getInitialStandardDeviation(): float { return $this->initialStandardDeviation; } - public function getBeta() + public function getBeta(): float { return $this->beta; } - public function getDynamicsFactor() + public function getDynamicsFactor(): float { return $this->dynamicsFactor; } - public function getDrawProbability() + public function getDrawProbability(): float { return $this->drawProbability; } - public function getDefaultRating() + public function getDefaultRating(): Rating { return new Rating($this->initialMean, $this->initialStandardDeviation); } diff --git a/src/Numerics/GaussianDistribution.php b/src/Numerics/GaussianDistribution.php index f404076..6880baf 100644 --- a/src/Numerics/GaussianDistribution.php +++ b/src/Numerics/GaussianDistribution.php @@ -12,11 +12,11 @@ class GaussianDistribution implements \Stringable { // precision and precisionMean are used because they make multiplying and dividing simpler // (the the accompanying math paper for more details) - private $precision; + private float $precision; - private $precisionMean; + private float $precisionMean; - private $variance; + private float $variance; public function __construct(private float $mean = 0.0, private float $standardDeviation = 1.0) { diff --git a/src/Numerics/Range.php b/src/Numerics/Range.php index f49add5..f984585 100644 --- a/src/Numerics/Range.php +++ b/src/Numerics/Range.php @@ -26,7 +26,7 @@ class Range return $this->max; } - protected static function create(int $min, int $max): self + protected static function create(int $min, int $max): static { return new Range($min, $max); } diff --git a/src/Numerics/SquareMatrix.php b/src/Numerics/SquareMatrix.php index 25b371d..69e78a4 100644 --- a/src/Numerics/SquareMatrix.php +++ b/src/Numerics/SquareMatrix.php @@ -4,7 +4,7 @@ namespace DNW\Skills\Numerics; class SquareMatrix extends Matrix { - public function __construct(...$allValues) + public function __construct(float|int ...$allValues) { $rows = (int) sqrt(count($allValues)); $cols = $rows; diff --git a/src/PartialPlay.php b/src/PartialPlay.php index ad21385..966aca5 100644 --- a/src/PartialPlay.php +++ b/src/PartialPlay.php @@ -7,8 +7,7 @@ class PartialPlay public static function getPartialPlayPercentage(Player $player): float { // If the player doesn't support the interface, assume 1.0 == 100% - $supportsPartialPlay = $player instanceof ISupportPartialPlay; - if (! $supportsPartialPlay) { + if (! $player instanceof ISupportPartialPlay) { return 1.0; } diff --git a/src/PlayersRange.php b/src/PlayersRange.php index ea465f6..cc0f527 100644 --- a/src/PlayersRange.php +++ b/src/PlayersRange.php @@ -6,7 +6,7 @@ use DNW\Skills\Numerics\Range; class PlayersRange extends Range { - protected static function create(int $min, int $max): self + protected static function create(int $min, int $max): static { return new PlayersRange($min, $max); } diff --git a/src/RankSorter.php b/src/RankSorter.php index 133f5e5..d75537b 100644 --- a/src/RankSorter.php +++ b/src/RankSorter.php @@ -14,7 +14,7 @@ class RankSorter * @param array $teamRanks The ranks for each item where 1 is first place. * @return array */ - public static function sort(array &$teams, array &$teamRanks) + public static function sort(array &$teams, array &$teamRanks): array { array_multisort($teamRanks, $teams); diff --git a/src/RatingContainer.php b/src/RatingContainer.php index 1281d59..49f8ceb 100644 --- a/src/RatingContainer.php +++ b/src/RatingContainer.php @@ -21,11 +21,17 @@ class RatingContainer return $this->playerToRating->setValue($player, $rating); } + /** + * @return Player[] + */ public function getAllPlayers(): array { return $this->playerToRating->getAllKeys(); } + /** + * @return Rating[] + */ public function getAllRatings(): array { return $this->playerToRating->getAllValues(); diff --git a/src/SkillCalculator.php b/src/SkillCalculator.php index 43e7dbe..759e8cf 100644 --- a/src/SkillCalculator.php +++ b/src/SkillCalculator.php @@ -39,9 +39,9 @@ abstract class SkillCalculator */ abstract public function calculateMatchQuality(GameInfo $gameInfo, array $teamsOfPlayerToRatings): float; - public function isSupported(SkillCalculatorSupportedOptions $option): bool + public function isSupported(int $option): bool { - return (bool)($this->supportedOptions & $option->value) == $option; + return (bool)($this->supportedOptions & $option) == $option; } protected function validateTeamCountAndPlayersCountPerTeam(array $teamsOfPlayerToRatings): void diff --git a/src/TeamsRange.php b/src/TeamsRange.php index 19261a4..53dc3ba 100644 --- a/src/TeamsRange.php +++ b/src/TeamsRange.php @@ -6,7 +6,7 @@ use DNW\Skills\Numerics\Range; class TeamsRange extends Range { - protected static function create(int $min, int $max): self + protected static function create(int $min, int $max): static { return new TeamsRange($min, $max); } diff --git a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php index 447d87a..1bb3ce3 100644 --- a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -121,8 +121,7 @@ class GaussianWeightedSumFactor extends GaussianFactor $result = 0.0; // We start at 1 since offset 0 has the sum - $varCount = is_countable($vars) ? count($vars) : 0; - for ($i = 1; $i < $varCount; $i++) { + for ($i = 1; $i < count($vars); $i++) { $result += GaussianDistribution::logRatioNormalization($vars[$i]->getValue(), $messages[$i]->getValue()); } @@ -189,7 +188,7 @@ class GaussianWeightedSumFactor extends GaussianFactor $allMessages = $this->getMessages(); $allVariables = $this->getVariables(); - Guard::argumentIsValidIndex($messageIndex, is_countable($allMessages) ? count($allMessages) : 0, 'messageIndex'); + Guard::argumentIsValidIndex($messageIndex, count($allMessages), 'messageIndex'); $updatedMessages = []; $updatedVariables = [];