From b7322362bd26c3032174adeb0af13a33350e105b Mon Sep 17 00:00:00 2001 From: Jens True Date: Tue, 1 Aug 2023 12:26:38 +0000 Subject: [PATCH] More PHP types for static analysis --- src/FactorGraphs/Factor.php | 12 ++++++------ src/FactorGraphs/FactorGraphLayer.php | 12 +++++------- src/Team.php | 2 +- src/Teams.php | 2 +- .../TruncatedGaussianCorrectionFunctions.php | 18 +++++++++--------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/FactorGraphs/Factor.php b/src/FactorGraphs/Factor.php index c64bbe3..1440a12 100644 --- a/src/FactorGraphs/Factor.php +++ b/src/FactorGraphs/Factor.php @@ -12,11 +12,11 @@ abstract class Factor implements \Stringable private $_messageToVariableBinding; - private $_name; + private string $_name; private array $_variables = []; - protected function __construct($name) + protected function __construct(string $name) { $this->_name = 'Factor['.$name.']'; $this->_messageToVariableBinding = new HashMap(); @@ -33,17 +33,17 @@ abstract class Factor implements \Stringable /** * @return int The number of messages that the factor has */ - public function getNumberOfMessages() + public function getNumberOfMessages(): int { return count($this->_messages); } - protected function getVariables() + protected function getVariables(): array { return $this->_variables; } - protected function getMessages() + protected function getMessages(): array { return $this->_messages; } @@ -55,7 +55,7 @@ abstract class Factor implements \Stringable * * @throws Exception */ - public function updateMessageIndex($messageIndex) + public function updateMessageIndex(int $messageIndex) { Guard::argumentIsValidIndex($messageIndex, count($this->_messages), 'messageIndex'); $message = $this->_messages[$messageIndex]; diff --git a/src/FactorGraphs/FactorGraphLayer.php b/src/FactorGraphs/FactorGraphLayer.php index 8b79644..b45bb08 100644 --- a/src/FactorGraphs/FactorGraphLayer.php +++ b/src/FactorGraphs/FactorGraphLayer.php @@ -28,30 +28,28 @@ abstract class FactorGraphLayer /** * This reference is still needed - * - * @return array */ - public function &getOutputVariablesGroups() + public function &getOutputVariablesGroups(): array { return $this->_outputVariablesGroups; } - public function getLocalFactors() + public function getLocalFactors(): array { return $this->_localFactors; } - public function setInputVariablesGroups($value) + public function setInputVariablesGroups(array $value): void { $this->_inputVariablesGroups = $value; } - protected function scheduleSequence(array $itemsToSequence, $name): ScheduleSequence + protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence { return new ScheduleSequence($name, $itemsToSequence); } - protected function addLayerFactor(Factor $factor) + protected function addLayerFactor(Factor $factor): void { $this->_localFactors[] = $factor; } diff --git a/src/Team.php b/src/Team.php index 749bbf5..a184e41 100644 --- a/src/Team.php +++ b/src/Team.php @@ -13,7 +13,7 @@ class Team extends RatingContainer } } - public function addPlayer(Player $player, Rating $rating) + public function addPlayer(Player $player, Rating $rating): self { $this->setRating($player, $rating); diff --git a/src/Teams.php b/src/Teams.php index ab88dec..4502702 100644 --- a/src/Teams.php +++ b/src/Teams.php @@ -4,7 +4,7 @@ namespace DNW\Skills; class Teams { - public static function concat(...$args/*variable arguments*/) + public static function concat(Team ...$args/*variable arguments*/): array { $result = []; diff --git a/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php b/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php index b3f0125..271accd 100644 --- a/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php +++ b/src/TrueSkill/TruncatedGaussianCorrectionFunctions.php @@ -15,15 +15,15 @@ class TruncatedGaussianCorrectionFunctions * correction of a single-sided truncated Gaussian with unit variance." * * @param $teamPerformanceDifference - * @param number $drawMargin In the paper, it's referred to as just "ε". + * @param $drawMargin In the paper, it's referred to as just "ε". * @param $c */ - public static function vExceedsMarginScaled($teamPerformanceDifference, float|int $drawMargin, $c): float + public static function vExceedsMarginScaled(float $teamPerformanceDifference, float $drawMargin, float $c): float { return self::vExceedsMargin($teamPerformanceDifference / $c, $drawMargin / $c); } - public static function vExceedsMargin($teamPerformanceDifference, $drawMargin) + public static function vExceedsMargin(float $teamPerformanceDifference, float $drawMargin): float { $denominator = GaussianDistribution::cumulativeTo($teamPerformanceDifference - $drawMargin); @@ -44,12 +44,12 @@ class TruncatedGaussianCorrectionFunctions * @param $drawMargin * @param $c */ - public static function wExceedsMarginScaled($teamPerformanceDifference, float|int $drawMargin, $c): float + public static function wExceedsMarginScaled(float $teamPerformanceDifference, float $drawMargin, float $c): float { return self::wExceedsMargin($teamPerformanceDifference / $c, $drawMargin / $c); } - public static function wExceedsMargin($teamPerformanceDifference, $drawMargin): float + public static function wExceedsMargin(float $teamPerformanceDifference, float $drawMargin): float { $denominator = GaussianDistribution::cumulativeTo($teamPerformanceDifference - $drawMargin); @@ -67,13 +67,13 @@ class TruncatedGaussianCorrectionFunctions } // the additive correction of a double-sided truncated Gaussian with unit variance - public static function vWithinMarginScaled($teamPerformanceDifference, float|int $drawMargin, $c): float + public static function vWithinMarginScaled(float $teamPerformanceDifference, float $drawMargin, float $c): float { return self::vWithinMargin($teamPerformanceDifference / $c, $drawMargin / $c); } // from F#: - public static function vWithinMargin($teamPerformanceDifference, $drawMargin): float + public static function vWithinMargin(float $teamPerformanceDifference, float $drawMargin): float { $teamPerformanceDifferenceAbsoluteValue = abs($teamPerformanceDifference); $denominator = @@ -99,13 +99,13 @@ class TruncatedGaussianCorrectionFunctions } // the multiplicative correction of a double-sided truncated Gaussian with unit variance - public static function wWithinMarginScaled($teamPerformanceDifference, float|int $drawMargin, $c): float + public static function wWithinMarginScaled(float $teamPerformanceDifference, float $drawMargin, float $c): float { return self::wWithinMargin($teamPerformanceDifference / $c, $drawMargin / $c); } // From F#: - public static function wWithinMargin($teamPerformanceDifference, float|int $drawMargin) + public static function wWithinMargin(float $teamPerformanceDifference, float $drawMargin): float { $teamPerformanceDifferenceAbsoluteValue = abs($teamPerformanceDifference); $denominator = GaussianDistribution::cumulativeTo($drawMargin - $teamPerformanceDifferenceAbsoluteValue)