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

namespace Nextras\Dbal\Result;


use ArrayIterator;
use Nextras\Dbal\Exception\InvalidArgumentException;
use OutOfBoundsException;
use function assert;


class BufferedResultAdapter implements IResultAdapter
{
	/** @var IResultAdapter */
	private $adapter;

	/** @var ArrayIterator<mixed, mixed>|null */
	private $data;


	public function __construct(IResultAdapter $adapter)
	{
		$this->adapter = $adapter;
	}


	public function toBuffered(): IResultAdapter
	{
		return $this;
	}


	public function toUnbuffered(): IResultAdapter
	{
		if ($this->data === null) {
			return $this->adapter->toUnbuffered();
		} else {
			return $this;
		}
	}


	public function seek(int $index): void
	{
		if ($this->data === null) {
			$this->init();
		}
		assert($this->data !== null);

		if ($index === 0) {
			$this->data->rewind();
			return;
		}

		try {
			$this->data->seek($index);
		} catch (OutOfBoundsException $e) {
			throw new InvalidArgumentException("Unable to seek in row set to {$index} index.", 0, $e);
		}
	}


	public function fetch(): ?array
	{
		if ($this->data === null) {
			$this->init();
		}
		assert($this->data !== null);

		$fetched = $this->data->valid() ? $this->data->current() : null;
		$this->data->next();
		return $fetched;
	}


	public function getTypes(): array
	{
		return $this->adapter->getTypes();
	}


	public function getRowsCount(): int
	{
		if ($this->data === null) {
			$this->init();
		}
		assert($this->data !== null);

		return $this->data->count();
	}


	private function init(): void
	{
		$rows = [];
		while (($row = $this->adapter->fetch()) !== null) {
			$rows[] = $row;
		}
		$this->data = new ArrayIterator($rows);
	}
}
<commit_msg>BufferedResult: rewrite implementation to be more typesafe<commit_after><?php declare(strict_types = 1);

namespace Nextras\Dbal\Result;


use ArrayIterator;
use Nextras\Dbal\Exception\InvalidArgumentException;
use OutOfBoundsException;


/**
 * @internal
 */
class BufferedResultAdapter implements IResultAdapter
{
	/** @var IResultAdapter */
	protected $adapter;

	/** @var ArrayIterator<mixed, mixed>|null */
	protected $data;


	public function __construct(IResultAdapter $adapter)
	{
		$this->adapter = $adapter;
	}


	public function toBuffered(): IResultAdapter
	{
		return $this;
	}


	public function toUnbuffered(): IResultAdapter
	{
		if ($this->data === null) {
			return $this->adapter->toUnbuffered();
		} else {
			return $this;
		}
	}


	public function seek(int $index): void
	{
		$data = $this->getData();

		if ($index === 0) {
			$data->rewind();
			return;
		}

		try {
			$data->seek($index);
		} catch (OutOfBoundsException $e) {
			throw new InvalidArgumentException("Unable to seek in row set to {$index} index.", 0, $e);
		}
	}


	public function fetch(): ?array
	{
		$data = $this->getData();
		$fetched = $data->valid() ? $data->current() : null;
		$data->next();
		return $fetched;
	}


	public function getTypes(): array
	{
		return $this->adapter->getTypes();
	}


	public function getRowsCount(): int
	{
		return $this->getData()->count();
	}


	/**
	 * @return ArrayIterator<mixed, mixed>
	 */
	protected function getData(): ArrayIterator
	{
		if ($this->data === null) {
			$this->data = $this->fetchData();
		}
		return $this->data;
	}


	/**
	 * @return ArrayIterator<mixed, mixed>
	 */
	protected function fetchData(): ArrayIterator
	{
		$rows = [];
		while (($row = $this->adapter->fetch()) !== null) {
			$rows[] = $row;
		}
		return new ArrayIterator($rows);
	}
}

{'fraction_non_alphanumeric': 0.17436915225404026, 'fraction_numerical': 0.001701162461015027, 'mean_word_length': 3.6666666666666665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 58, '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': '5269518', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1163, 'n_words': 275}