Bunch of generic code standard items. Will need a cleanup.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Jens True 2024-02-02 15:16:11 +00:00
parent 3dddfc05db
commit c18ccd38e2
38 changed files with 117 additions and 26 deletions

@ -13,6 +13,31 @@
<rule ref="PSR2"></rule>
<rule ref="PSR12"></rule>
<rule ref="Generic">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed"/>
<exclude name="Generic.Files.LowercasedFilename.NotFound"/>
<exclude name="Generic.PHP.ClosingPHPTag.NotFound"/>
<exclude name="Generic.Files.EndFileNoNewline.Found"/>
<exclude name="Generic.Files.EndFileNoNewline.Found"/>
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found"/>
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine"/>
<exclude name="Generic.Classes.OpeningBraceSameLine.BraceOnNewLine"/>
<exclude name="Generic.PHP.LowerCaseConstant.Found"/>
<exclude name="Generic.Formatting.SpaceAfterCast"/>
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Generic.NamingConventions.AbstractClassNamePrefix.Missing"/>
<exclude name="Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed"/>
<exclude name="Generic.NamingConventions.InterfaceNameSuffix.Missing"/>
<exclude name="Generic.Commenting.Todo.TaskFound"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUse"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed"/>
<exclude name="Generic.Formatting.SpaceBeforeCast.NoSpace"/>
<exclude name="Generic.CodeAnalysis.UselessOverridingMethod.Found"/>
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.NewlineBeforeOpenBrace"/>
</rule>
<!-- Ban some functions -->
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs;
class FactorGraph

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs;
class KeyedVariable extends Variable

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs;
use DNW\Skills\Numerics\GaussianDistribution;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs;
abstract class Schedule implements \Stringable

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
use Exception;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
/**

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
/**

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
interface ISupportPartialUpdate

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
/**
@ -14,6 +16,7 @@ class BasicMath
* Squares the input (x^2 = x * x)
*
* @param float $x Value to square (x)
*
* @return float The squared value (x^2)
*/
public static function square($x): float
@ -26,6 +29,7 @@ class BasicMath
*
* @param mixed[] $itemsToSum The items to sum,
* @param \Closure $callback The function to apply to each array element before summing.
*
* @return float The sum.
*/
public static function sum(array $itemsToSum, \Closure $callback): float

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
/**
@ -198,7 +200,8 @@ class GaussianDistribution implements \Stringable
-1.523e-15,
-9.4e-17,
1.21e-16,
-2.8e-17, ];
-2.8e-17,
];
$ncof = count($coefficients);
$d = 0.0;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
class IdentityMatrix extends DiagonalMatrix

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
// The whole purpose of this class is to make the code for the SkillCalculator(s)

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
class SquareMatrix extends Matrix

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Numerics;
class Vector extends Matrix

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
/**

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
class PartialPlay

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
/**
@ -26,7 +28,8 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
private mixed $Id,
float $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE,
float $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE
) {
)
{
// If they don't want to give a player an id, that's ok...
Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage');
Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage');

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
use DNW\Skills\Numerics\Range;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
// Container for a player's rating.

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
class RatingContainer

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
class SkillCalculatorSupportedOptions

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills;
class Team extends RatingContainer

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill;
use DNW\Skills\GameInfo;
@ -34,7 +36,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
GameInfo $gameInfo,
array $teams,
array $teamRanks
): RatingContainer {
): RatingContainer
{
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
@ -124,7 +127,9 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
/**
* Helper function that gets a list of values for all player ratings
*
* @param Team[] $teamAssignmentsList
*
* @return int[]
*/
private static function getPlayerRatingValues(array $teamAssignmentsList, \Closure $playerRatingFunction): array

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Factor;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message;
@ -62,11 +64,9 @@ class GaussianGreaterThanFactor extends GaussianFactor
$denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC);
$newPrecision = $c / $denom;
$newPrecisionMean = (
$d +
$newPrecisionMean = ($d +
$sqrtC *
TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)
) / $denom;
TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)) / $denom;
$newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\KeyedVariable;

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message;

@ -26,6 +26,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
/**
* This following is used for convenience, for example, the first entry is [0, 1, 2]
* corresponding to v[0] = a1*v[1] + a2*v[2]
*
* @var array<float[]> $weights
*/
private array $weights = [];

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message;
@ -65,8 +67,7 @@ class GaussianWithinFactor extends GaussianFactor
$newPrecision = $c / $denominator;
$newPrecisionMean = ($d +
$sqrtC *
TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)
) / $denominator;
TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)) / $denominator;
$newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
$newMessage = GaussianDistribution::divide(

@ -17,7 +17,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
TrueSkillFactorGraph $parentGraph,
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer
) {
)
{
parent::__construct($parentGraph);
}

@ -32,7 +32,8 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
Variable $strongerTeam,
Variable $weakerTeam,
Variable $output
): GaussianWeightedSumFactor {
): GaussianWeightedSumFactor
{
$teams = [$strongerTeam, $weakerTeam];
$weights = [1.0, -1.0];

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Layers;
use DNW\Skills\FactorGraphs\FactorGraphLayer;

@ -122,8 +122,7 @@ class TruncatedGaussianCorrectionFunctions
$vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin);
return $vt * $vt +
(
($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
(($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
*
GaussianDistribution::at(
$drawMargin - $teamPerformanceDifferenceAbsoluteValue

@ -37,7 +37,8 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
GameInfo $gameInfo,
array $teams,
array $teamRanks
): RatingContainer {
): RatingContainer
{
// Basic argument checking
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);

@ -70,7 +70,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
Team $selfTeam,
Team $otherTeam,
PairwiseComparison $selfToOtherTeamComparison
): void {
): void
{
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
$gameInfo->getDrawProbability(),
$gameInfo->getBeta()