<commit_before><?php
/*
    Condorcet PHP - Election manager and results calculator.
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.

    By <NAME> and contributors - MIT LICENSE (Please read LICENSE.txt)
    https://github.com/julien-boudry/Condorcet
*/
declare(strict_types=1);

namespace CondorcetPHP\Condorcet\Algo;

use CondorcetPHP\Condorcet\Dev\CondorcetDocumentationGenerator\CondorcetDocAttributes\{Description, Example, FunctionReturn, PublicAPI, Related};
use CondorcetPHP\Condorcet\{CondorcetVersion, Election, Result};
use CondorcetPHP\Condorcet\Throwable\CondorcetException;

// Generic for Algorithms
abstract class Method
{
    use CondorcetVersion;

    public const IS_PROPORTIONAL = false;

    public static ?int $MaxCandidates = null;

    protected Election $_selfElection;
    protected ?Result $_Result = null;

    // Static

    final public static function setOption (string $optionName, mixed $optionValue): bool
    {
        $optionVar = 'option'.ucfirst($optionName);

        static::$$optionVar = $optionValue;

        return true;
    }

    //

    public function __construct (Election $mother)
    {
        $this->_selfElection = $mother;

        if (!\is_null(static::$MaxCandidates) && $this->_selfElection->countCandidates() > static::$MaxCandidates) :
            throw new CondorcetException(101, static::METHOD_NAME[0].' is configured to accept only '.static::$MaxCandidates.' candidates');
        endif;
    }

    public function __destruct ()
    {
        unset($this->_selfElection, $this->_Result);
    }

    public function getResult (): Result
    {
        // Cache
        if ( $this->_Result !== null ) :
            return $this->_Result;
        endif;

        $this->compute();

        return $this->_Result;
    }

    abstract protected function getStats (): array;

    protected function createResult (array $result): Result
    {
    	$optionsList = \array_keys((new \ReflectionClass(static::class))->getStaticProperties());
        $optionsList = \array_filter($optionsList, function (string $name): bool {
            return \str_starts_with($name ,'option');
        });

        $methodOptions = [];

        foreach ($optionsList as $oneOption) :
            $methodOptions[substr($oneOption,6)] = static::$$oneOption;
        endforeach;

        return new Result (
            fromMethod: static::METHOD_NAME[0],
            byClass: $this::class,
    		election: $this->_selfElection,
    		result: $result,
            stats: $this->getStats(),
            seats: (static::IS_PROPORTIONAL) ? $this->_selfElection->getNumberOfSeats() : null,
            methodOptions: $methodOptions
    	);
    }
}
<commit_msg>PHP 8.1 | Methodclass: Election Readonly propertie #54<commit_after><?php
/*
    Condorcet PHP - Election manager and results calculator.
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.

    By <NAME> and contributors - MIT LICENSE (Please read LICENSE.txt)
    https://github.com/julien-boudry/Condorcet
*/
declare(strict_types=1);

namespace CondorcetPHP\Condorcet\Algo;

use CondorcetPHP\Condorcet\Dev\CondorcetDocumentationGenerator\CondorcetDocAttributes\{Description, Example, FunctionReturn, PublicAPI, Related};
use CondorcetPHP\Condorcet\{CondorcetVersion, Election, Result};
use CondorcetPHP\Condorcet\Throwable\CondorcetException;

// Generic for Algorithms
abstract class Method
{
    use CondorcetVersion;

    public const IS_PROPORTIONAL = false;

    public static ?int $MaxCandidates = null;

    protected readonly Election $_selfElection;
    protected ?Result $_Result = null;

    // Static

    final public static function setOption (string $optionName, mixed $optionValue): bool
    {
        $optionVar = 'option'.ucfirst($optionName);

        static::$$optionVar = $optionValue;

        return true;
    }

    //

    public function __construct (Election $mother)
    {
        $this->_selfElection = $mother;

        if (!\is_null(static::$MaxCandidates) && $this->_selfElection->countCandidates() > static::$MaxCandidates) :
            throw new CondorcetException(101, static::METHOD_NAME[0].' is configured to accept only '.static::$MaxCandidates.' candidates');
        endif;
    }

    public function getResult (): Result
    {
        // Cache
        if ( $this->_Result !== null ) :
            return $this->_Result;
        endif;

        $this->compute();

        return $this->_Result;
    }

    abstract protected function getStats (): array;

    protected function createResult (array $result): Result
    {
    	$optionsList = \array_keys((new \ReflectionClass(static::class))->getStaticProperties());
        $optionsList = \array_filter($optionsList, function (string $name): bool {
            return \str_starts_with($name ,'option');
        });

        $methodOptions = [];

        foreach ($optionsList as $oneOption) :
            $methodOptions[substr($oneOption,6)] = static::$$oneOption;
        endforeach;

        return new Result (
            fromMethod: static::METHOD_NAME[0],
            byClass: $this::class,
    		election: $this->_selfElection,
    		result: $result,
            stats: $this->getStats(),
            seats: (static::IS_PROPORTIONAL) ? $this->_selfElection->getNumberOfSeats() : null,
            methodOptions: $methodOptions
    	);
    }
}

{'fraction_non_alphanumeric': 0.11404779145546705, 'fraction_numerical': 0.0032585083272990588, 'mean_word_length': 3.0240349599417335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2442085', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1528, 'n_words': 374}