<commit_before><?php
namespace Mongolid\Cursor;

use IteratorIterator;
use MongoDB\Collection;
use MongoDB\Driver\Cursor as DriverCursor;
use MongoDB\Driver\Exception\LogicException;
use Mongolid\ActiveRecord;
use Mongolid\Connection\Pool;
use Mongolid\Container\Ioc;
use Mongolid\DataMapper\EntityAssembler;
use Mongolid\Schema\Schema;
use Serializable;
use Traversable;

/**
 * This class wraps the query execution and the actual creation of the driver cursor.
 * By doing this we can, call 'sort', 'skip', 'limit' and others after calling
 * 'where'. Because the mongodb library's MongoDB\Cursor is much more
 * limited (in that regard) than the old driver MongoCursor.
 */
class Cursor implements CursorInterface, Serializable
{
    /**
     * Schema that describes the entity that will be retrieved when iterating through the cursor.
     *
     * @var string
     */
    public $entitySchema;

    /**
     * @var Collection
     */
    protected $collection;

    /**
     * The command that is being called in the $collection.
     *
     * @var string
     */
    protected $command;

    /**
     * The parameters of the $command.
     *
     * @var array
     */
    protected $params;

    /**
     * The MongoDB cursor used to interact with db.
     *
     * @var DriverCursor
     */
    protected $cursor = null;

    /**
     * Iterator position (to be used with foreach).
     *
     * @var int
     */
    protected $position = 0;

    /**
     * Have the responsibility of assembling the data coming from the database into actual entities.
     *
     * @var EntityAssembler
     */
    protected $assembler;

    /**
     * @param Schema     $entitySchema Schema that describes the entity that will be retrieved from the database.
     * @param Collection $collection   The raw collection object that will be used to retrieve the documents.
     * @param string     $command      The command that is being called in the $collection.
     * @param array      $params       The parameters of the $command.
     */
    public function __construct(
        Schema $entitySchema,
        Collection $collection,
        string $command,
        array $params
    ) {
        $this->cursor = null;
        $this->entitySchema = $entitySchema;
        $this->collection = $collection;
        $this->command = $command;
        $this->params = $params;
    }

    /**
     * Limits the number of results returned.
     *
     * @param int $amount The number of results to return.
     *
     * @return Cursor Returns this cursor.
     */
    public function limit(int $amount)
    {
        $this->params[1]['limit'] = $amount;

        return $this;
    }

    /**
     * Sorts the results by given fields.
     *
     * @param array $fields An array of fields by which to sort.
     *                      Each element in the array has as key the field name,
     *                      and as value either 1 for ascending sort, or -1 for descending sort.
     *
     * @return Cursor Returns this cursor.
     */
    public function sort(array $fields)
    {
        $this->params[1]['sort'] = $fields;

        return $this;
    }

    /**
     * Skips a number of results.
     *
     * @param int $amount The number of results to skip.
     *
     * @return Cursor Returns this cursor.
     */
    public function skip(int $amount)
    {
        $this->params[1]['skip'] = $amount;

        return $this;
    }

    /**
     * Disable idle timeout of 10 minutes from MongoDB cursor.
     * This method should be called before the cursor was started.
     *
     * @param bool $flag Toggle timeout on or off.
     *
     * @return Cursor Returns this cursor.
     */
    public function disableTimeout(bool $flag = true)
    {
        $this->params[1]['noCursorTimeout'] = $flag;

        return $this;
    }

    /**
     * Counts the number of results for this cursor.
     *
     * @return int The number of documents returned by this cursor's query.
     */
    public function count(): int
    {
        return $this->collection->count(...$this->params);
    }

    /**
     * Iterator interface rewind (used in foreach).
     *
     * @return void
     */
    public function rewind()
    {
        try {
            $this->getCursor()->rewind();
        } catch (LogicException $e) {
            $this->fresh();
            $this->getCursor()->rewind();
        }

        $this->position = 0;
    }

    /**
     * Iterator interface current. Return a model object
     * with cursor document. (used in foreach).
     *
     * @return mixed
     */
    public function current()
    {
        $document = $this->getCursor()->current();

        if ($document instanceof ActiveRecord) {
            $documentToArray = $document->toArray();
            $this->entitySchema = $document->getSchema();
        } else {
            $documentToArray = (array) $document;
        }

        return $this->getAssembler()->assemble($document, $this->entitySchema);
    }

    /**
     * Returns the first element of the cursor.
     *
     * @return mixed
     */
    public function first()
    {
        $this->rewind();
        $document = $this->getCursor()->current();

        if (!$document) {
            return;
        }

        return $this->getAssembler()->assemble($document, $this->entitySchema);
    }

    /**
     * Refresh the cursor in order to be able to perform a rewind and iterate
     * through it again. A new request to the database will be made in the next
     * iteration.
     *
     * @return void
     */
    public function fresh()<commit_msg>Fixed logic mistake in Cursor class<commit_after><?php
namespace Mongolid\Cursor;

use IteratorIterator;
use MongoDB\Collection;
use MongoDB\Driver\Cursor as DriverCursor;
use MongoDB\Driver\Exception\LogicException;
use Mongolid\ActiveRecord;
use Mongolid\Connection\Pool;
use Mongolid\Container\Ioc;
use Mongolid\DataMapper\EntityAssembler;
use Mongolid\Schema\Schema;
use Serializable;
use Traversable;

/**
 * This class wraps the query execution and the actual creation of the driver cursor.
 * By doing this we can, call 'sort', 'skip', 'limit' and others after calling
 * 'where'. Because the mongodb library's MongoDB\Cursor is much more
 * limited (in that regard) than the old driver MongoCursor.
 */
class Cursor implements CursorInterface, Serializable
{
    /**
     * Schema that describes the entity that will be retrieved when iterating through the cursor.
     *
     * @var string
     */
    public $entitySchema;

    /**
     * @var Collection
     */
    protected $collection;

    /**
     * The command that is being called in the $collection.
     *
     * @var string
     */
    protected $command;

    /**
     * The parameters of the $command.
     *
     * @var array
     */
    protected $params;

    /**
     * The MongoDB cursor used to interact with db.
     *
     * @var DriverCursor
     */
    protected $cursor = null;

    /**
     * Iterator position (to be used with foreach).
     *
     * @var int
     */
    protected $position = 0;

    /**
     * Have the responsibility of assembling the data coming from the database into actual entities.
     *
     * @var EntityAssembler
     */
    protected $assembler;

    /**
     * @param Schema     $entitySchema Schema that describes the entity that will be retrieved from the database.
     * @param Collection $collection   The raw collection object that will be used to retrieve the documents.
     * @param string     $command      The command that is being called in the $collection.
     * @param array      $params       The parameters of the $command.
     */
    public function __construct(
        Schema $entitySchema,
        Collection $collection,
        string $command,
        array $params
    ) {
        $this->cursor = null;
        $this->entitySchema = $entitySchema;
        $this->collection = $collection;
        $this->command = $command;
        $this->params = $params;
    }

    /**
     * Limits the number of results returned.
     *
     * @param int $amount The number of results to return.
     *
     * @return Cursor Returns this cursor.
     */
    public function limit(int $amount)
    {
        $this->params[1]['limit'] = $amount;

        return $this;
    }

    /**
     * Sorts the results by given fields.
     *
     * @param array $fields An array of fields by which to sort.
     *                      Each element in the array has as key the field name,
     *                      and as value either 1 for ascending sort, or -1 for descending sort.
     *
     * @return Cursor Returns this cursor.
     */
    public function sort(array $fields)
    {
        $this->params[1]['sort'] = $fields;

        return $this;
    }

    /**
     * Skips a number of results.
     *
     * @param int $amount The number of results to skip.
     *
     * @return Cursor Returns this cursor.
     */
    public function skip(int $amount)
    {
        $this->params[1]['skip'] = $amount;

        return $this;
    }

    /**
     * Disable idle timeout of 10 minutes from MongoDB cursor.
     * This method should be called before the cursor was started.
     *
     * @param bool $flag Toggle timeout on or off.
     *
     * @return Cursor Returns this cursor.
     */
    public function disableTimeout(bool $flag = true)
    {
        $this->params[1]['noCursorTimeout'] = $flag;

        return $this;
    }

    /**
     * Counts the number of results for this cursor.
     *
     * @return int The number of documents returned by this cursor's query.
     */
    public function count(): int
    {
        return $this->collection->count(...$this->params);
    }

    /**
     * Iterator interface rewind (used in foreach).
     *
     * @return void
     */
    public function rewind()
    {
        try {
            $this->getCursor()->rewind();
        } catch (LogicException $e) {
            $this->fresh();
            $this->getCursor()->rewind();
        }

        $this->position = 0;
    }

    /**
     * Iterator interface current. Return a model object
     * with cursor document. (used in foreach).
     *
     * @return mixed
     */
    public function current()
    {
        $document = $this->getCursor()->current();

        if ($document instanceof ActiveRecord) {
            $documentToArray = $document->toArray();
            $this->entitySchema = $document->getSchema();
        } else {
            $documentToArray = (array) $document;
        }

        return $this->getAssembler()->assemble(
            $documentToArray,
            $this->entitySchema
        );
    }

    /**
     * Returns the first element of the cursor.
     *
     * @return mixed
     */
    public function first()
    {
        $this->rewind();
        $document = $this->getCursor()->current();

        if (!$document) {
            return;
        }

        return $this->getAssembler()->assemble($document, $this->entitySchema);
    }

    /**
     * Refresh the cursor in order to be able to perform a rewind and iterate
     * through it again. A new request to the database will be made in the next
     * iteration.
     *
     * @return void
     */
    public function fresh()
{'fraction_non_alphanumeric': 0.1052538003378078, 'fraction_numerical': 0.0017779358165170237, 'mean_word_length': 2.0080213903743314, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '2465561', 'n_tokens_mistral': 3112, 'n_tokens_neox': 3013, 'n_words': 1047}