<commit_before><?php declare(strict_types=1);

namespace Aircury\Collection;

use Aircury\Collection\Exceptions\InvalidKeyException;
use Aircury\Collection\Exceptions\ProtectedKeyException;
use Aircury\Collection\Exceptions\UnexpectedElementException;

abstract class AbstractCollection implements CollectionInterface
{
    /**
     * @var mixed[]
     */
    private $elements;

    /**
     * @var string
     */
    private $class;

    /**
     * @var bool
     */
    private $isAssociative = false;

    public function __construct(array $elements = [])
    {
        $this->class = $this->getClass();

        foreach ($elements as $element) {
            if (!is_a($element, $this->class)) {
                throw UnexpectedElementException::classConstraint($this->class, $element);
            }
        }

        $this->elements = $elements;

        $this->evaluateIfItIsAssociative();
    }

    private function evaluateIfItIsAssociative(): void
    {
        if (
            0 !== ($count = \count($this->elements)) &&
            ($keys = array_keys($this->elements)) !== ($range = range(0, $count - 1)) &&
            !$this->isSameButOutOfOrder($keys, $range)
        ) {
            $this->isAssociative = true;
        }
    }

    private function isSameButOutOfOrder(array $a, array $b): bool
    {
        sort($a);
        sort($b);

        return $a === $b;
    }

    public function offsetExists($offset): bool
    {
        return array_key_exists($offset, $this->elements);
    }

    public function offsetGet($offset)
    {
        if (!isset($this->elements[$offset])) {
            throw InvalidKeyException::invalidOffset($offset, array_keys($this->elements));
        }

        return $this->elements[$offset];
    }

    public function offsetSet($offset, $element): void
    {
        if (!is_a($element, $this->class)) {
            throw UnexpectedElementException::classConstraint($this->class, $element);
        }

        if (null === $offset) {
            $this->elements[] = $element;
        } else {
            $this->elements[$offset] = $element;

            if (!$this->isAssociative && (!\is_int($offset) || $offset < 0 || $offset > \count($this->elements))) {
                $this->isAssociative = true;
            }
        }
    }

    public function offsetUnset($offset): void
    {
        if (!$this->isAssociative && \is_int($offset) && $offset < \count($this->elements) - 1) {
            $this->isAssociative = true;
        }

        unset($this->elements[$offset]);
    }

    protected function setElements(array $elements): void
    {
        $this->elements = $elements;
    }

    public function toArray(): array
    {
        return $this->elements;
    }

    public function toValuesArray(): array
    {
        return array_values($this->elements);
    }<commit_msg>Collection | Change `is_int` method call for `is_numeric` to solve detecting string keys casted as integers<commit_after><?php declare(strict_types=1);

namespace Aircury\Collection;

use Aircury\Collection\Exceptions\InvalidKeyException;
use Aircury\Collection\Exceptions\ProtectedKeyException;
use Aircury\Collection\Exceptions\UnexpectedElementException;

abstract class AbstractCollection implements CollectionInterface
{
    /**
     * @var mixed[]
     */
    private $elements;

    /**
     * @var string
     */
    private $class;

    /**
     * @var bool
     */
    private $isAssociative = false;

    public function __construct(array $elements = [])
    {
        $this->class = $this->getClass();

        foreach ($elements as $element) {
            if (!is_a($element, $this->class)) {
                throw UnexpectedElementException::classConstraint($this->class, $element);
            }
        }

        $this->elements = $elements;

        $this->evaluateIfItIsAssociative();
    }

    private function evaluateIfItIsAssociative(): void
    {
        if (
            0 !== ($count = \count($this->elements)) &&
            ($keys = array_keys($this->elements)) !== ($range = range(0, $count - 1)) &&
            !$this->isSameButOutOfOrder($keys, $range)
        ) {
            $this->isAssociative = true;
        }
    }

    private function isSameButOutOfOrder(array $a, array $b): bool
    {
        sort($a);
        sort($b);

        return $a === $b;
    }

    public function offsetExists($offset): bool
    {
        return array_key_exists($offset, $this->elements);
    }

    public function offsetGet($offset)
    {
        if (!isset($this->elements[$offset])) {
            throw InvalidKeyException::invalidOffset($offset, array_keys($this->elements));
        }

        return $this->elements[$offset];
    }

    public function offsetSet($offset, $element): void
    {
        if (!is_a($element, $this->class)) {
            throw UnexpectedElementException::classConstraint($this->class, $element);
        }

        if (null === $offset) {
            $this->elements[] = $element;
        } else {
            $this->elements[$offset] = $element;

            if (!$this->isAssociative && (!\is_numeric($offset) || $offset < 0 || $offset > \count($this->elements))) {
                $this->isAssociative = true;
            }
        }
    }

    public function offsetUnset($offset): void
    {
        if (!$this->isAssociative && \is_numeric($offset) && $offset < \count($this->elements) - 1) {
            $this->isAssociative = true;
        }

        unset($this->elements[$offset]);
    }

    protected function setElements(array $elements): void
    {
        $this->elements = $elements;
    }

    public function toArray(): array
    {
        return $this->elements;
    }

    public function toValuesArray(): array
    {
        return array_values($this->elements);
    }
{'fraction_non_alphanumeric': 0.14445978598550224, 'fraction_numerical': 0.0020711080428028996, 'mean_word_length': 2.274011299435028, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 61, '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': '7210586', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1597, 'n_words': 322}