<commit_before><?php

namespace Palmtree\Csv;

class CsvFileObject extends \SplFileObject
{
    protected $bytesWritten = 0;
    protected $lineEnding = "\r\n";

    public function fwriteCsv(array $row, $delimiter = null, $enclosure = null)
    {
        $bytes = $this->fwrite($this->getCsvString($row, $delimiter, $enclosure));

        if ($bytes === false) {
            return false;
        }

        $this->bytesWritten += $bytes;

        return $bytes;
    }

    public function __destruct()
    {
        $this->trimFinalLineEnding();
    }

    /**
     * Returns a string representation of a row to be written
     * as a line in a CSV file.
     *
     * @param array $row
     *
     * @param       $delimiter
     * @param       $enclosure
     *
     * @return string
     */
    protected function getCsvString(array $row, $delimiter, $enclosure)
    {
        $csvControl = $this->getCsvControl();

        $delimiter = $delimiter ? : $csvControl[0];
        $enclosure = $enclosure ? : $csvControl[1];

        $result = $enclosure;
        $result .= implode($enclosure . $delimiter . $enclosure, self::escapeEnclosure($row, $enclosure));
        $result .= $enclosure;

        $result .= $this->getLineEnding();

        return $result;
    }

    /**
     * @return int
     */
    public function getBytesWritten()
    {
        return (int)$this->bytesWritten;
    }

    /**
     * @return string
     */
    public function getLineEnding()
    {
        return $this->lineEnding;
    }

    /**
     * @param string $lineEnding
     *
     * @return CsvFileObject
     */
    public function setLineEnding($lineEnding)
    {
        $this->lineEnding = $lineEnding;

        return $this;
    }

    public function getSize()
    {
        try {
            $size = parent::getSize();
        } catch (\RuntimeException $exception) {
            $size = parent::fstat()['size'];
        }

        return $size;
    }

    /**
     * Trims the line ending delimiter from the end of the CSV file.
     * RFC-4180 states CSV files should not contain a trailing new line.
     */
    public function trimFinalLineEnding()
    {
        if ($this->getBytesWritten() > 0) {
            // Only trim the file if it ends with the line ending delimiter.
            $length = mb_strlen($this->getLineEnding());

            $this->fseek(-$length, SEEK_END);
<commit_msg>Replace parent::fstat with ->fstat<commit_after><?php

namespace Palmtree\Csv;

class CsvFileObject extends \SplFileObject
{
    protected $bytesWritten = 0;
    protected $lineEnding = "\r\n";

    public function fwriteCsv(array $row, $delimiter = null, $enclosure = null)
    {
        $bytes = $this->fwrite($this->getCsvString($row, $delimiter, $enclosure));

        if ($bytes === false) {
            return false;
        }

        $this->bytesWritten += $bytes;

        return $bytes;
    }

    public function __destruct()
    {
        $this->trimFinalLineEnding();
    }

    /**
     * Returns a string representation of a row to be written
     * as a line in a CSV file.
     *
     * @param array $row
     *
     * @param       $delimiter
     * @param       $enclosure
     *
     * @return string
     */
    protected function getCsvString(array $row, $delimiter, $enclosure)
    {
        $csvControl = $this->getCsvControl();

        $delimiter = $delimiter ? : $csvControl[0];
        $enclosure = $enclosure ? : $csvControl[1];

        $result = $enclosure;
        $result .= implode($enclosure . $delimiter . $enclosure, self::escapeEnclosure($row, $enclosure));
        $result .= $enclosure;

        $result .= $this->getLineEnding();

        return $result;
    }

    /**
     * @return int
     */
    public function getBytesWritten()
    {
        return (int)$this->bytesWritten;
    }

    /**
     * @return string
     */
    public function getLineEnding()
    {
        return $this->lineEnding;
    }

    /**
     * @param string $lineEnding
     *
     * @return CsvFileObject
     */
    public function setLineEnding($lineEnding)
    {
        $this->lineEnding = $lineEnding;

        return $this;
    }

    public function getSize()
    {
        try {
            $size = parent::getSize();
        } catch (\RuntimeException $exception) {
            $size = $this->fstat()['size'];
        }

        return $size;
    }

    /**
     * Trims the line ending delimiter from the end of the CSV file.
     * RFC-4180 states CSV files should not contain a trailing new line.
     */
    public function trimFinalLineEnding()
    {
        if ($this->getBytesWritten() > 0) {
            // Only trim the file if it ends with the line ending delimiter.
            $length = mb_strlen($this->getLineEnding());

            $this->fseek(-$length, SEEK_END);

{'fraction_non_alphanumeric': 0.12760199833472108, 'fraction_numerical': 0.0033305578684429643, 'mean_word_length': 1.9770755885997522, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '1838220', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1425, 'n_words': 355}