<commit_before><?php

namespace LogSafe\ParameterHandler;

use Composer\IO\IOInterface;
use Composer\Script\Event;
use Symfony\Component\Yaml\Inline;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

class ScriptHandler
{
    public static function buildParameters(Event $event)
    {
        $extras = $event->getComposer()->getPackage()->getExtra();

        if (empty($extras['logsafe-parameters']['file'])) {
            throw new \InvalidArgumentException('The extra.logsafe-parameters.file setting is required to use this script handler.');
        }

        $realFile = $extras['logsafe-parameters']['file'];

        if (empty($extras['logsafe-parameters']['dist-file'])) {
            $distFile = $realFile.'.dist';
        } else {
            $distFile = $extras['logsafe-parameters']['dist-file'];
        }

        if (!is_file($distFile)) {
            throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $distFile));
        }

        $exists = is_file($realFile);

        $yamlParser = new Parser();
        $io = $event->getIO();

        if ($exists) {
            $io->write(sprintf('<info>Updating the "%s" file.</info>', $realFile));
        } else {
            $io->write(sprintf('<info>Creating the "%s" file.</info>', $realFile));
        }

        // Find the expected params
        $expectedValues = $yamlParser->parse(file_get_contents($distFile));
        if (!isset($expectedValues['parameters'])) {
            throw new \InvalidArgumentException('The dist file seems invalid.');
        }
        $expectedParams = (array) $expectedValues['parameters'];

        // find the actual params
        $actualValues = array('parameters' => array());
        if ($exists) {
            $actualValues = array_merge($actualValues, $yamlParser->parse(file_get_contents($realFile)));
        }
        $actualParams = (array) $actualValues['parameters'];

        // Remove the outdated params
        foreach ($actualParams as $key => $value) {
            if (!array_key_exists($key, $expectedParams)) {
                unset($actualParams[$key]);
            }
        }

        $envMap = empty($extras['logsafe-parameters']['env-map']) ? array() : (array) $extras['logsafe-parameters']['env-map'];

        // Add the params coming from the environment values
        $actualParams = array_replace($actualParams, self::getEnvValues($envMap));

        $actualParams = self::getParams($io, $expectedParams, $actualParams);

        file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump(array('parameters' => $actualParams)));
    }
<commit_msg>Added some error handling in case an invalid yaml file exists<commit_after><?php

namespace LogSafe\ParameterHandler;

use Composer\IO\IOInterface;
use Composer\Script\Event;
use Symfony\Component\Yaml\Inline;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;

class ScriptHandler
{
    public static function buildParameters(Event $event)
    {
        $extras = $event->getComposer()->getPackage()->getExtra();

        if (empty($extras['logsafe-parameters']['file'])) {
            throw new \InvalidArgumentException('The extra.logsafe-parameters.file setting is required to use this script handler.');
        }

        $realFile = $extras['logsafe-parameters']['file'];

        if (empty($extras['logsafe-parameters']['dist-file'])) {
            $distFile = $realFile.'.dist';
        } else {
            $distFile = $extras['logsafe-parameters']['dist-file'];
        }

        if (!is_file($distFile)) {
            throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $distFile));
        }

        $exists = is_file($realFile);

        $yamlParser = new Parser();
        $io = $event->getIO();

        if ($exists) {
            $io->write(sprintf('<info>Updating the "%s" file.</info>', $realFile));
        } else {
            $io->write(sprintf('<info>Creating the "%s" file.</info>', $realFile));
        }

        // Find the expected params
        $expectedValues = $yamlParser->parse(file_get_contents($distFile));
        if (!isset($expectedValues['parameters'])) {
            throw new \InvalidArgumentException('The dist file seems invalid.');
        }
        $expectedParams = (array) $expectedValues['parameters'];

        // find the actual params
        $actualValues = array('parameters' => array());
        if ($exists) {
            $existingValues = $yamlParser->parse(file_get_contents($realFile));
            if (!is_array($existingValues)) {
                throw new \InvalidArgumentException(sprintf('The existing "%s" file does not contain an array', $realFile));
            }
            $actualValues = array_merge($actualValues, $existingValues);
        }
        $actualParams = (array) $actualValues['parameters'];

        // Remove the outdated params
        foreach ($actualParams as $key => $value) {
            if (!array_key_exists($key, $expectedParams)) {
                unset($actualParams[$key]);
            }
        }

        $envMap = empty($extras['logsafe-parameters']['env-map']) ? array() : (array) $extras['logsafe-parameters']['env-map'];

        // Add the params coming from the environment values
        $actualParams = array_replace($actualParams, self::getEnvValues($envMap));

        $actualParams = self::getParams($io, $expectedParams, $actualParams);

        file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump(array('parameters' => $actualParams)));
    }

{'fraction_non_alphanumeric': 0.14893244662233113, 'fraction_numerical': 0.0, 'mean_word_length': 2.858879135719109, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '2176474', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1522, 'n_words': 348}