<commit_before><?php declare(strict_types=1);
namespace Pietdevries94\SingleClassCollection;

use Illuminate\Support\Collection;

abstract class AbstractSingleClassCollection extends Collection
{
    /** @var $collectionClass string */
    protected $collectionClass;

    /**
     * AbstractSingleClassCollection constructor.
     * @param array $items
     * @throws \RuntimeException
     * @throws \TypeError
     */
    public function __construct(array $items = [])
    {
        $this->validateCollectionClass();

        if (!$this->areValidItems($items)) {
            throw new \TypeError('All elements in array must be instances of '.$collectionClass);
        }

        parent::__construct($items);
    }

    public function offsetSet($key, $value)
    {
        if (!$this->isValidItem($value)) {
            throw new \TypeError('Value must be an instance of '.$collectionClass);
        }
        parent::offsetSet($key, $value);
    }

    /**
     * @param callable $callback
     * @return Collection
     */
    public function mapToCollection(callable $callback): Collection
    {
        return collect($this)->map($callback);
    }

    /**
     * @param mixed $itemOrArrayOfItems
     * @return bool
     */
    protected function areValidItems(mixed $itemOrArrayOfItems): bool
    {
        $array = is_a($itemOrArrayOfItems) ? $itemOrArrayOfItems : [$itemOrArrayOfItems];

        foreach ($array as $item) {
            if (!($item instanceof $this->collectionClass)) {
                return $false;
            }
        }
        return true;
    }

    /**
     * @throws \RuntimeException
     */
    private function validateCollectionClass(): void
    {
        if(!class_exists($this->collectionClass)) {
            throw new \RuntimeException('Static property $collectionClass is not implemented');
        }
    }
}<commit_msg>Bugfixes<commit_after><?php declare(strict_types=1);
namespace Pietdevries94\SingleClassCollection;

use Illuminate\Support\Collection;

abstract class AbstractSingleClassCollection extends Collection
{
    /** @var $collectionClass string */
    protected $collectionClass;

    /**
     * AbstractSingleClassCollection constructor.
     * @param array $items
     * @throws \RuntimeException
     * @throws \TypeError
     */
    public function __construct(array $items = [])
    {
        $this->validateCollectionClass();

        if (!$this->areValidItems($items)) {
            throw new \TypeError('All elements in array must be instances of '.$collectionClass);
        }

        parent::__construct($items);
    }

    /**
     * @param mixed $key
     * @param mixed $value
     * @throws \TypeError
     */
    public function offsetSet($key, $value)
    {
        if (!$this->isValidItem($value)) {
            throw new \TypeError('Value must be an instance of '.$collectionClass);
        }
        parent::offsetSet($key, $value);
    }

    /**
     * @param callable $callback
     * @return Collection
     */
    public function mapToCollection(callable $callback): Collection
    {
        return collect($this)->map($callback);
    }

    /**
     * @param array|mixed $itemOrArrayOfItems
     * @return bool
     */
    protected function areValidItems($itemOrArrayOfItems): bool
    {
        $array = is_array($itemOrArrayOfItems) ? $itemOrArrayOfItems : [$itemOrArrayOfItems];

        foreach ($array as $item) {
            if (!($item instanceof $this->collectionClass)) {
                return $false;
            }
        }
        return true;
    }

    /**
     * @throws \RuntimeException
     */
    private function validateCollectionClass(): void
    {
        if(!class_exists($this->collectionClass)) {
            throw new \RuntimeException('Static property $collectionClass is not implemented');
        }
    }
}
{'fraction_non_alphanumeric': 0.11738562091503268, 'fraction_numerical': 0.001568627450980392, 'mean_word_length': 2.453068592057762, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3815983', 'n_tokens_mistral': 1014, 'n_tokens_neox': 999, 'n_words': 235}