<commit_before><?php

namespace Armandsar\LaravelTranslationio;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Gettext\Extractors;
use Gettext\Translations;
use Gettext\Merge;

class GettextPOGenerator
{
    /**
     * @var Application
     */
    private $application;
    /**
     * @var FileSystem
     */
    private $filesystem;

    private $config;

    public function __construct(Application $application, FileSystem $filesystem)
    {
        $this->application = $application;
        $this->filesystem = $filesystem;
        $this->config = $application['config']['translationio'];
    }

    public function call($targets)
    {
        $po_data = $this->scan($targets);

        return $po_data;
    }

    // https://github.com/eusonlito/laravel-Gettext/blob/170c284c9feb8cb6073149791bed02889d820b90/src/Eusonlito/LaravelGettext/Gettext.php#L73
    private function scan($targets)
    {
        $this->gettextFunctionsToExtract();

        $translations = new Translations();
        $directories = $this->gettextParsePaths();

        foreach ($directories as $dir) {
            if (!is_dir($dir)) {
                throw new \Exception('Folder "' . $dir . '" doest not exists. Gettext scan aborted.');
            }

            foreach ($this->scanDir($dir) as $file) {
                if (strstr($file, '.blade.php')) {
                    Extractors\Blade::fromFile($file, $translations);
                } elseif (strstr($file, '.php')) {
                    Extractors\PhpCode::fromFile($file, $translations);
                }
            }
        }

        # Create Temporary path (create po files to load them in memory)
        $tmpDir = $this->tmpPath();
        $tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'app.po';
        $this->filesystem->makeDirectory($tmpDir, 0777, true, true);

        # Create pot file (will be kept in app structure)
        $potDir = $this->gettextLocalesPath();
        $potFile = $potDir . DIRECTORY_SEPARATOR . 'app.pot';
        $this->filesystem->makeDirectory($potDir, 0777, true, true);

        # po(t) headers
        $this->setPoHeaders($translations);

        # create pot data
        $translations->toPoFile($potFile);
        $poLocales = [
            'pot_data' => $this->filesystem->get($potFile)
        ];

        # create po data for each language
        foreach ($targets as $target) {
            $translations = clone $translations;
            $translations->setLanguage($target);

            $translations = $this->mergeWithExistingTargetPoFile($translations, $target);

            $translations->toPoFile($tmpFile);
            $poLocales[$target] = $this->filesystem->get($tmpFile);
        }

        $this->filesystem->delete($tmpFile);
<commit_msg>wrong use of cloned variable<commit_after><?php

namespace Armandsar\LaravelTranslationio;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Gettext\Extractors;
use Gettext\Translations;
use Gettext\Merge;

class GettextPOGenerator
{
    /**
     * @var Application
     */
    private $application;
    /**
     * @var FileSystem
     */
    private $filesystem;

    private $config;

    public function __construct(Application $application, FileSystem $filesystem)
    {
        $this->application = $application;
        $this->filesystem = $filesystem;
        $this->config = $application['config']['translationio'];
    }

    public function call($targets)
    {
        $po_data = $this->scan($targets);

        return $po_data;
    }

    // https://github.com/eusonlito/laravel-Gettext/blob/170c284c9feb8cb6073149791bed02889d820b90/src/Eusonlito/LaravelGettext/Gettext.php#L73
    private function scan($targets)
    {
        $this->gettextFunctionsToExtract();

        $translations = new Translations();
        $directories = $this->gettextParsePaths();

        foreach ($directories as $dir) {
            if (!is_dir($dir)) {
                throw new \Exception('Folder "' . $dir . '" doest not exists. Gettext scan aborted.');
            }

            foreach ($this->scanDir($dir) as $file) {
                if (strstr($file, '.blade.php')) {
                    Extractors\Blade::fromFile($file, $translations);
                } elseif (strstr($file, '.php')) {
                    Extractors\PhpCode::fromFile($file, $translations);
                }
            }
        }

        # Create Temporary path (create po files to load them in memory)
        $tmpDir = $this->tmpPath();
        $tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'app.po';
        $this->filesystem->makeDirectory($tmpDir, 0777, true, true);

        # Create pot file (will be kept in app structure)
        $potDir = $this->gettextLocalesPath();
        $potFile = $potDir . DIRECTORY_SEPARATOR . 'app.pot';
        $this->filesystem->makeDirectory($potDir, 0777, true, true);

        # po(t) headers
        $this->setPoHeaders($translations);

        # create pot data
        $translations->toPoFile($potFile);
        $poLocales = [
            'pot_data' => $this->filesystem->get($potFile)
        ];

        # create po data for each language
        foreach ($targets as $target) {
            $targetTranslations = clone $translations;
            $targetTranslations->setLanguage($target);

            $targetTranslations = $this->mergeWithExistingTargetPoFile($targetTranslations, $target);

            $targetTranslations->toPoFile($tmpFile);
            $poLocales[$target] = $this->filesystem->get($tmpFile);
        }

        $this->filesystem->delete($tmpFile);

{'fraction_non_alphanumeric': 0.12920076615009576, 'fraction_numerical': 0.013233501654187707, 'mean_word_length': 2.470694864048338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 2, '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': '3190292', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1654, 'n_words': 322}