From aa836e859d60c7d09dee6f3d60ba9293e9ec6551 Mon Sep 17 00:00:00 2001 From: Jens True Date: Tue, 8 Aug 2023 07:00:51 +0000 Subject: [PATCH] Additional Psalm warnings resolving --- composer.lock | 10 +++++----- src/Team.php | 2 +- src/TrueSkill/FactorGraphTrueSkillCalculator.php | 5 +++-- src/TrueSkill/Factors/GaussianFactor.php | 2 +- src/TrueSkill/Factors/GaussianGreaterThanFactor.php | 2 +- src/TrueSkill/Factors/GaussianLikelihoodFactor.php | 2 +- src/TrueSkill/Factors/GaussianPriorFactor.php | 4 ++-- src/TrueSkill/Factors/GaussianWeightedSumFactor.php | 4 ++-- src/TrueSkill/Factors/GaussianWithinFactor.php | 2 +- src/TrueSkill/TwoTeamTrueSkillCalculator.php | 8 ++++---- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/composer.lock b/composer.lock index d6c8f92..7e58ea2 100644 --- a/composer.lock +++ b/composer.lock @@ -1197,16 +1197,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.26", + "version": "1.10.27", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "5d660cbb7e1b89253a47147ae44044f49832351f" + "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/5d660cbb7e1b89253a47147ae44044f49832351f", - "reference": "5d660cbb7e1b89253a47147ae44044f49832351f", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/a9f44dcea06f59d1363b100bb29f297b311fa640", + "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640", "shasum": "" }, "require": { @@ -1255,7 +1255,7 @@ "type": "tidelift" } ], - "time": "2023-07-19T12:44:37+00:00" + "time": "2023-08-05T09:57:55+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Team.php b/src/Team.php index a184e41..10c06f1 100644 --- a/src/Team.php +++ b/src/Team.php @@ -8,7 +8,7 @@ class Team extends RatingContainer { parent::__construct(); - if (! is_null($player)) { + if (! is_null($player) && ! is_null($rating)) { $this->addPlayer($player, $rating); } } diff --git a/src/TrueSkill/FactorGraphTrueSkillCalculator.php b/src/TrueSkill/FactorGraphTrueSkillCalculator.php index a89f334..0a18e22 100644 --- a/src/TrueSkill/FactorGraphTrueSkillCalculator.php +++ b/src/TrueSkill/FactorGraphTrueSkillCalculator.php @@ -16,6 +16,7 @@ use DNW\Skills\SkillCalculatorSupportedOptions; use DNW\Skills\Team; use DNW\Skills\TeamsRange; use DNW\Skills\RatingContainer; +use DNW\Skills\Rating; /** * Calculates TrueSkill using a full factor graph. @@ -100,7 +101,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator return new Vector( self::getPlayerRatingValues( $teamAssignmentsList, - fn ($rating) => $rating->getMean() + fn (Rating $rating): float => $rating->getMean() ) ); } @@ -115,7 +116,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator return new DiagonalMatrix( self::getPlayerRatingValues( $teamAssignmentsList, - fn ($rating) => BasicMath::square($rating->getStandardDeviation()) + fn (Rating $rating): float => BasicMath::square($rating->getStandardDeviation()) ) ); } diff --git a/src/TrueSkill/Factors/GaussianFactor.php b/src/TrueSkill/Factors/GaussianFactor.php index b30788c..ed5d7cb 100644 --- a/src/TrueSkill/Factors/GaussianFactor.php +++ b/src/TrueSkill/Factors/GaussianFactor.php @@ -30,7 +30,7 @@ abstract class GaussianFactor extends Factor $variable, new Message( $newDistribution, - sprintf('message from %s to %s', $this, $variable) + sprintf('message from %s to %s', (string)$this, (string)$variable) ) ); } diff --git a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php index 1b55fe1..04ad1cd 100644 --- a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php +++ b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php @@ -18,7 +18,7 @@ class GaussianGreaterThanFactor extends GaussianFactor public function __construct(float $epsilon, Variable $variable) { - parent::__construct(\sprintf('%s > %.2f', $variable, $epsilon)); + parent::__construct(\sprintf('%s > %.2f', (string)$variable, $epsilon)); $this->epsilon = $epsilon; $this->createVariableToMessageBinding($variable); } diff --git a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php index 114ae04..4787f04 100644 --- a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php +++ b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php @@ -19,7 +19,7 @@ class GaussianLikelihoodFactor extends GaussianFactor public function __construct(float $betaSquared, Variable $variable1, Variable $variable2) { - parent::__construct(sprintf('Likelihood of %s going to %s', $variable2, $variable1)); + parent::__construct(sprintf('Likelihood of %s going to %s', (string)$variable2, (string)$variable1)); $this->precision = 1.0 / $betaSquared; $this->createVariableToMessageBinding($variable1); $this->createVariableToMessageBinding($variable2); diff --git a/src/TrueSkill/Factors/GaussianPriorFactor.php b/src/TrueSkill/Factors/GaussianPriorFactor.php index f6418d5..dea5135 100644 --- a/src/TrueSkill/Factors/GaussianPriorFactor.php +++ b/src/TrueSkill/Factors/GaussianPriorFactor.php @@ -17,11 +17,11 @@ class GaussianPriorFactor extends GaussianFactor public function __construct(float $mean, float $variance, Variable $variable) { - parent::__construct(sprintf('Prior value going to %s', $variable)); + parent::__construct(sprintf('Prior value going to %s', (string)$variable)); $this->newMessage = new GaussianDistribution($mean, sqrt($variance)); $newMessage = new Message( GaussianDistribution::fromPrecisionMean(0, 0), - sprintf('message from %s to %s', $this, $variable) + sprintf('message from %s to %s', (string)$this, (string)$variable) ); $this->createVariableToMessageBindingWithMessage($variable, $newMessage); diff --git a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php index df84d8f..c6a11de 100644 --- a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -38,7 +38,7 @@ class GaussianWeightedSumFactor extends GaussianFactor */ public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights = null) { - parent::__construct(self::createName($sumVariable, $variablesToSum, $variableWeights)); + parent::__construct(self::createName((string)$sumVariable, $variablesToSum, $variableWeights)); // The first weights are a straightforward copy // v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n @@ -237,7 +237,7 @@ class GaussianWeightedSumFactor extends GaussianFactor private static function createName(string $sumVariable, array $variablesToSum, array $weights): string { // TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays? - $result = (string) $sumVariable; + $result = $sumVariable; $result .= ' = '; $totalVars = count($variablesToSum); diff --git a/src/TrueSkill/Factors/GaussianWithinFactor.php b/src/TrueSkill/Factors/GaussianWithinFactor.php index c8c786c..7b5d56b 100644 --- a/src/TrueSkill/Factors/GaussianWithinFactor.php +++ b/src/TrueSkill/Factors/GaussianWithinFactor.php @@ -18,7 +18,7 @@ class GaussianWithinFactor extends GaussianFactor public function __construct(float $epsilon, Variable $variable) { - parent::__construct(sprintf('%s <= %.2f', $variable, $epsilon)); + parent::__construct(sprintf('%s <= %.2f', (string)$variable, $epsilon)); $this->epsilon = $epsilon; $this->createVariableToMessageBinding($variable); } diff --git a/src/TrueSkill/TwoTeamTrueSkillCalculator.php b/src/TrueSkill/TwoTeamTrueSkillCalculator.php index 3c01951..06b7411 100644 --- a/src/TrueSkill/TwoTeamTrueSkillCalculator.php +++ b/src/TrueSkill/TwoTeamTrueSkillCalculator.php @@ -79,12 +79,12 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator $totalPlayers = $selfTeam->count() + $otherTeam->count(); - $meanGetter = fn ($currentRating) => $currentRating->getMean(); + $meanGetter = fn (Rating $currentRating): float => $currentRating->getMean(); $selfMeanSum = BasicMath::sum($selfTeam->getAllRatings(), $meanGetter); $otherTeamMeanSum = BasicMath::sum($otherTeam->getAllRatings(), $meanGetter); - $varianceGetter = fn ($currentRating): float => BasicMath::square($currentRating->getStandardDeviation()); + $varianceGetter = fn (Rating $currentRating): float => BasicMath::square($currentRating->getStandardDeviation()); $c = sqrt( BasicMath::sum($selfTeam->getAllRatings(), $varianceGetter) @@ -160,9 +160,9 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator $betaSquared = BasicMath::square($gameInfo->getBeta()); - $meanGetter = fn ($currentRating) => $currentRating->getMean(); + $meanGetter = fn (Rating $currentRating): float => $currentRating->getMean(); - $varianceGetter = fn ($currentRating): float => BasicMath::square($currentRating->getStandardDeviation()); + $varianceGetter = fn (Rating $currentRating): float => BasicMath::square($currentRating->getStandardDeviation()); $team1MeanSum = BasicMath::sum($team1Ratings, $meanGetter); $team1StdDevSquared = BasicMath::sum($team1Ratings, $varianceGetter);