<commit_before><?php

namespace Olympus\Handler;

use Composer\IO\IOInterface;

/**
 * Gets its own config via composer, inspired from Incenteev ParameterHandler script.
 * @see https://github.com/Incenteev/ParameterHandler
 *
 * @package Olympus
 * @subpackage Handler\Processor
 * @author <NAME> <<EMAIL>>
 * @since 0.0.3
 *
 */

class Processor
{
    /**
     * @var IOInterface
     */
    private $io;

    /**
     * Constructor.
     *
     * @param IOInterface $io
     *
     * @since 0.0.3
     */
    public function __construct(IOInterface $io)
    {
        $this->io = $io;
    }

    /**
     * Starts creating file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    private function _start($realFile)
    {
        // Check file
        $exists = is_file($realFile);
        $action = $exists ? 'Updating' : 'Creating';

        # Write
        $this->io->write(sprintf('<info>%s the "%s" file</info>', $action, $realFile));

        return $exists;
    }

    /**
     * Create `env.php` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processEnv($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Find the expected params from dist file
        $expectedParams = (array) require_once $realFile.'.dist';
        $actualValues = [];

        // Update contents
        if ($exists) {
            $existingValues = (array) require_once $realFile;

            // Check validity
            if ($existingValues === null) {
                $existingValues = [];
            }

            // Params must be stored in an array
            if (!is_array($existingValues)) {
                throw new \InvalidArgumentException(sprintf('The existing "%s" file does not contain an array', $realFile));
            }

            $actualValues = $existingValues;
        }

        // Build Q&A
        $userValues = $this->getParams($expectedParams, $actualValues);

        # Write
        $this->io->write(sprintf("<comment>All parameters are defined now in your '%s' file</comment>", $realFile));

        // Merge values
        $actualValues = array_merge_recursive($actualValues, $userValues);

        // Write in file
        file_put_contents($realFile, "<?php\n\n// This file is auto-generated during the composer install\n\nreturn ".var_export($actualValues, true).";\n");
    }

    /**
     * Create `salt.php` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processSalt($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Check file and rebuild it
        if ($exists) {
            unlink($realFile);
        }

        # Write
        $this->io->write("<comment>Get values directly from 'http://api.wordpress.org/secret-key/1.1/salt/'</comment>");

        // Get salt keys
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://api.wordpress.org/secret-key/1.1/salt/');
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        $salt = curl_exec($ch);
        curl_close($ch);

        // Write in file
        file_put_contents($realFile, "<?php\n\n// This file is auto-generated during the composer install\n\n".$salt."\n");
    }

    /**
     * Create `config.rb` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processConfig($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Check file and rebuild it
        if ($exists) {
            # Write
            $this->io->write(sprintf("<comment>Your '%s' already exists</comment>", $realFile));

            return;
        }

        # Write
        $this->io->write(sprintf("<comment>Your '%s' is copied from '%s'</comment>", $realFile, $realFile.'.dist'));

        // Get contents, simply
        $contents = file_get_contents($realFile.'.dist');

        // Write in file
        file_put_contents($realFile, "# This file is auto-generated during the composer install\n\n" . $contents);
    }

    /**
     * Get actual params and display Q&A.
     *
     * @param array $expectedParams
     * @param array $actualValues
     *
     * @since 0.0.3
     */
    private function getParams(array $expectedParams, array $actualValues)
    {
        // Simply use the expectedParams value as default for the missing params.
        if (!$this->io->isInteractive()) {
            return array_replace($expectedParams, $actualValues);
        }

        // Get forgotten keys
        $keys = $this->keyMatch($expectedParams, $actualValues);

        // Iterate on expectedParams and display Q&A<commit_msg>add comment when no interactions<commit_after><?php

namespace Olympus\Handler;

use Composer\IO\IOInterface;

/**
 * Gets its own config via composer, inspired from Incenteev ParameterHandler script.
 * @see https://github.com/Incenteev/ParameterHandler
 *
 * @package Olympus
 * @subpackage Handler\Processor
 * @author <NAME> <<EMAIL>>
 * @since 0.0.3
 *
 */

class Processor
{
    /**
     * @var IOInterface
     */
    private $io;

    /**
     * Constructor.
     *
     * @param IOInterface $io
     *
     * @since 0.0.3
     */
    public function __construct(IOInterface $io)
    {
        $this->io = $io;
    }

    /**
     * Starts creating file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    private function _start($realFile)
    {
        // Check file
        $exists = is_file($realFile);
        $action = $exists ? 'Updating' : 'Creating';

        # Write
        $this->io->write(sprintf('<info>%s the "%s" file</info>', $action, $realFile));

        return $exists;
    }

    /**
     * Create `env.php` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processEnv($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Find the expected params from dist file
        $expectedParams = (array) require_once $realFile.'.dist';
        $actualValues = [];

        // Update contents
        if ($exists) {
            $existingValues = (array) require_once $realFile;

            // Check validity
            if ($existingValues === null) {
                $existingValues = [];
            }

            // Params must be stored in an array
            if (!is_array($existingValues)) {
                throw new \InvalidArgumentException(sprintf('The existing "%s" file does not contain an array', $realFile));
            }

            $actualValues = $existingValues;
        }

        // Build Q&A
        $userValues = $this->getParams($expectedParams, $actualValues, $realFile);

        # Write
        $this->io->write(sprintf("<comment>All parameters are defined now in your '%s' file</comment>", $realFile));

        // Merge values
        $actualValues = array_merge_recursive($actualValues, $userValues);

        // Write in file
        file_put_contents($realFile, "<?php\n\n// This file is auto-generated during the composer install\n\nreturn ".var_export($actualValues, true).";\n");
    }

    /**
     * Create `salt.php` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processSalt($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Check file and rebuild it
        if ($exists) {
            unlink($realFile);
        }

        # Write
        $this->io->write("<comment>Get values directly from 'http://api.wordpress.org/secret-key/1.1/salt/'</comment>");

        // Get salt keys
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://api.wordpress.org/secret-key/1.1/salt/');
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        $salt = curl_exec($ch);
        curl_close($ch);

        // Write in file
        file_put_contents($realFile, "<?php\n\n// This file is auto-generated during the composer install\n\n".$salt."\n");
    }

    /**
     * Create `config.rb` file.
     *
     * @param string $realFile
     *
     * @since 0.0.3
     */
    public function processConfig($realFile)
    {
        // Check if file exists and display headers
        $exists = $this->_start($realFile);

        // Check file and rebuild it
        if ($exists) {
            # Write
            $this->io->write(sprintf("<comment>Your '%s' already exists</comment>", $realFile));

            return;
        }

        # Write
        $this->io->write(sprintf("<comment>Your '%s' is copied from '%s'</comment>", $realFile, $realFile.'.dist'));

        // Get contents, simply
        $contents = file_get_contents($realFile.'.dist');

        // Write in file
        file_put_contents($realFile, "# This file is auto-generated during the composer install\n\n" . $contents);
    }

    /**
     * Get actual params and display Q&A.
     *
     * @param array $expectedParams
     * @param array $actualValues
     * @param string $realFile
     *
     * @since 0.0.3
     */
    private function getParams(array $expectedParams, array $actualValues, $realFile)
    {
        // Simply use the expectedParams value as default for the missing params.
        if (!$this->io->isInteractive()) {
            $this->io->write(sprintf("\n<comment>Interactions are not permitted. Please, edit your \"%s\" file manually to define properly your parameters.</comment>", $realFile));
            return array_replace($expectedParams, $actualValues);
        }

        // Get forgotten keys
        $keys = $this->keyMatch($expectedParams, $actualValues);

        // Iterate on expectedParams and display Q&A
{'fraction_non_alphanumeric': 0.13682349474511343, 'fraction_numerical': 0.004911108928396032, 'mean_word_length': 2.0613349368610945, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 69, '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': '5307421', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2935, 'n_words': 738}