Skip to content

Commit

Permalink
PHP 8.3: Deep-cloning of readonly properties + more readonly (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed May 29, 2024
1 parent 3b6de2f commit e99d992
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Algo/Methods/Borda/BordaCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BordaCount extends Method implements MethodInterface

public static int $optionStarting = 1;

protected ?array $Stats = null;
protected readonly array $Stats;

protected function getStats(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Algo/Methods/Dodgson/DodgsonQuick.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DodgsonQuick extends Method implements MethodInterface
// Method Name
public const array METHOD_NAME = ['Dodgson Quick', 'DodgsonQuick', 'Dodgson Quick Winner'];

protected ?array $Stats = null;
protected readonly array $Stats;

protected function getStats(): array
{
Expand Down
6 changes: 4 additions & 2 deletions src/Algo/Methods/InstantRunoff/InstantRunoff.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InstantRunoff extends Method implements MethodInterface
// Method Name
public const array METHOD_NAME = ['Instant-runoff', 'InstantRunoff', 'IRV', 'preferential voting', 'ranked-choice voting', 'alternative vote', 'AlternativeVote', 'transferable vote', 'Vote alternatif'];

protected ?array $Stats = null;
protected readonly array $Stats;

public readonly float $majority;

Expand Down Expand Up @@ -57,6 +57,7 @@ protected function compute(): void

$candidateDone = [];
$result = [];
$stats = [];
$CandidatesWinnerCount = 0;
$CandidatesLoserCount = 0;

Expand All @@ -67,7 +68,7 @@ protected function compute(): void
$maxScore = max($score);
$minScore = min($score);

$this->Stats[++$iteration] = $score;
$stats[++$iteration] = $score;

if ($maxScore > $this->majority) {
foreach ($score as $candidateKey => $candidateScore) {
Expand Down Expand Up @@ -101,6 +102,7 @@ protected function compute(): void
}
}

$this->Stats = $stats;
$this->Result = $this->createResult($result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Algo/Methods/Majority/Majority_Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class Majority_Core extends Method implements MethodInterface
protected int $numberOfTargetedCandidatesAfterEachRound;

protected array $admittedCandidates = [];
protected ?array $Stats = null;
protected readonly array $Stats;

protected function getStats(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Algo/Methods/RankedPairs/RankedPairs_Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class RankedPairs_Core extends Method implements MethodInterface
// Ranked Pairs
protected readonly array $PairwiseSort;
protected array $Arcs = [];
protected ?array $Stats = null;
protected array $Stats = [];
protected bool $StatsDone = false;


Expand Down
2 changes: 0 additions & 2 deletions src/Algo/Methods/STV/CPO_STV.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class CPO_STV extends SingleTransferableVote
public static array $optionCondorcetCompletionMethod = self::DEFAULT_METHODS_CHAINING;
public static array $optionTieBreakerMethods = self::DEFAULT_METHODS_CHAINING;

protected ?array $Stats = null;

protected SplFixedArray $outcomes;
protected readonly array $initialScoreTable;
protected array $candidatesElectedFromFirstRound = [];
Expand Down
6 changes: 4 additions & 2 deletions src/Algo/Methods/STV/SingleTransferableVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SingleTransferableVote extends Method implements MethodInterface

public static StvQuotas $optionQuota = StvQuotas::DROOP;

protected ?array $Stats = null;
protected readonly array $Stats;

protected float $votesNeededToWin;

Expand All @@ -40,6 +40,7 @@ protected function compute(): void
Vote::initCache(); // Performances

$result = [];
$stats = [];
$rank = 0;

$this->votesNeededToWin = round(self::$optionQuota->getQuota($election->sumValidVotesWeightWithConstraints(), $election->getNumberOfSeats()), self::DECIMAL_PRECISION, \PHP_ROUND_HALF_DOWN);
Expand Down Expand Up @@ -79,7 +80,7 @@ protected function compute(): void
$end = true;
}

$this->Stats[++$round] = $scoreTable;
$stats[++$round] = $scoreTable;
}

while ($rank < $election->getNumberOfSeats() && !empty($candidateEliminated)) {
Expand All @@ -88,6 +89,7 @@ protected function compute(): void
unset($candidateEliminated[$rescueCandidateKey]);
}

$this->Stats = $stats;
$this->Result = $this->createResult($result);

Vote::clearCache(); // Performances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PdoHandlerDriver implements DataHandlerDriverInterface
#[PublicAPI]
public const array SEGMENT = [499, 50, 4, 1]; // Must be ordered desc.

protected \PDO $handler;
protected readonly \PDO $handler;
protected bool $transaction = false;
protected bool $queryError = false;

Expand Down

0 comments on commit e99d992

Please sign in to comment.