<commit_before><?php

/*
 * This file is part of the Valkyrja framework.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Valkyrja\Enum;

use InvalidArgumentException;
use JsonSerializable;
use ReflectionClass;

/**
 * Class Enum.
 *
 * @author <NAME>
 */
abstract class Enum implements JsonSerializable
{
    /**
     * The allowable enum values.
     *
     * @var array
     */
    protected const VALUES = null;

    /**
     * The enum cache to avoid more than one reflection class per enum.
     *
     * @var array
     */
    protected static $cache = [];
    /**
     * The value of this enum.
     *
     * @var string
     */
    protected $value;

    /**
     * Enum constructor.
     *
     * @param mixed $value The value to set
     *
     * @throws \InvalidArgumentException
     * @throws \ReflectionException
     */
    public function __construct($value)
    {
        $this->setValue($value);
    }

    /**
     * Check if the set value on this enum is a valid value for the enum.
     *
     * @param mixed $value The value to check
     *
     * @throws \ReflectionException
     *
     * @return bool
     */
    public static function isValid($value): bool
    {
        // If the value isset in the valid values array and the value matches the value to check
        // ?? Why is this here ??
        // As is known by all isset is faster than in_array. We want to capitalize on that with
        // some enums by making the const VALUES array a key value pair of the value itself
        // so that we've essentially got value => value for each item in the const VALUES
        // array. This way we can take advantage of the quickness of isset over in_array.
        // However, because not all enums may do this we need to ensure if the value
        // isset as the key in the array that it also matches as the value of that
        // item in the array, otherwise we'll get false positives where its a
        // normal array of 0 => value, 1 => value and we check for 0 being
        // a valid value where it may very well not be valid at all.
        if (isset(static::validValues()[$value]) && static::validValues()[$value] === $value) {
            return true;
        }

        return in_array($value, static::validValues(), true);
    }

    /**<commit_msg>Updating Enum for more efficient valid values check.<commit_after><?php

/*
 * This file is part of the Valkyrja framework.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Valkyrja\Enum;

use InvalidArgumentException;
use JsonSerializable;
use ReflectionClass;

/**
 * Class Enum.
 *
 * @author <NAME>
 */
abstract class Enum implements JsonSerializable
{
    /**
     * The allowable enum values.
     *
     * @var array
     */
    protected const VALUES = null;

    /**
     * The enum cache to avoid more than one reflection class per enum.
     *
     * @var array
     */
    protected static $cache = [];
    /**
     * The value of this enum.
     *
     * @var string
     */
    protected $value;

    /**
     * Enum constructor.
     *
     * @param mixed $value The value to set
     *
     * @throws \InvalidArgumentException
     * @throws \ReflectionException
     */
    public function __construct($value)
    {
        $this->setValue($value);
    }

    /**
     * Check if the set value on this enum is a valid value for the enum.
     *
     * @param mixed $value The value to check
     *
     * @throws \ReflectionException
     *
     * @return bool
     */
    public static function isValid($value): bool
    {
        // Get the valid values to compare with
        $validValues = static::validValues();

        // If the value isset in the valid values array and the value matches the value to check
        // ?? Why is this here ??
        // As is known by all isset is faster than in_array. We want to capitalize on that with
        // some enums by making the const VALUES array a key value pair of the value itself
        // so that we've essentially got value => value for each item in the const VALUES
        // array. This way we can take advantage of the quickness of isset over in_array.
        // However, because not all enums may do this we need to ensure if the value
        // isset as the key in the array that it also matches as the value of that
        // item in the array, otherwise we'll get false positives where its a
        // normal array of 0 => value, 1 => value and we check for 0 being
        // a valid value where it may very well not be valid at all.
        if (isset($validValues[$value]) && $validValues[$value] === $value) {
            return true;
        }

        return in_array($value, $validValues, true);
    }

    /**
{'fraction_non_alphanumeric': 0.0870006083958629, 'fraction_numerical': 0.0012167917258162643, 'mean_word_length': 2.139401654996817, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '1652803', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1297, 'n_words': 600}