<commit_before><?php
/*
 * @author <NAME> <<EMAIL>>
 */

namespace N98\Magento\Application;

use Composer\Autoload\ClassLoader;
use InvalidArgumentException;
use N98\Magento\Application;
use N98\Util\ArrayFunctions;
use N98\Util\BinaryString;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class Config
 *
 * Class representing the application configuration. Created to factor out configuration related application
 * functionality from @see N98\Magento\Application
 *
 * @package N98\Magento\Application
 */
class Config
{
    /**
     * @var array config data
     */
    private $config = array();

    /**
     * @var array
     */
    private $partialConfig = array();

    /**
     * @var ConfigurationLoader
     */
    private $loader;

    /**
     * @var array
     */
    private $initConfig;

    /**
     * @var boolean
     */
    private $isPharMode;

    /**
     * @var OutputInterface
     */
    private $output;


    /**
     * Config constructor.
     *
     * @param array $initConfig
     * @param bool $isPharMode
     * @param OutputInterface $output [optional]
     */
    public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null)
    {
        $this->initConfig = $initConfig;
        $this->isPharMode = (bool)$isPharMode;
        $this->output = $output ?: new NullOutput();
    }

    /**
     * alias magerun command in input from config
     *
     * @param InputInterface $input
     * @return ArgvInput|InputInterface
     */
    public function checkConfigCommandAlias(InputInterface $input)
    {
        foreach ($this->getArray(array('commands', 'aliases')) as $alias) {
            if (!is_array($alias)) {
                continue;
            }
            $aliasCommandName = key($alias);
            if ($input->getFirstArgument() !== $aliasCommandName) {
                continue;
            }
            $aliasCommandParams = array_slice(
                BinaryString::trimExplodeEmpty(' ', $alias[$aliasCommandName]),
                1
            );
            if (count($aliasCommandParams) > 0) {
                // replace with aliased data
                $mergedParams = array_merge(
                    array_slice($_SERVER['argv'], 0, 2),
                    $aliasCommandParams,
                    array_slice($_SERVER['argv'], 2)
                );
                $input = new ArgvInput($mergedParams);
            }
        }

        return $input;
    }

    /**
     * @param Command $command
     */
    public function registerConfigCommandAlias(Command $command)
    {
        foreach ($this->getArray(array('commands', 'aliases')) as $alias) {
            if (!is_array($alias)) {
                continue;
            }

            $aliasCommandName = key($alias);
            $commandString = $alias[$aliasCommandName];
            list($originalCommand) = explode(' ', $commandString, 2);
            if ($command->getName() !== $originalCommand) {
                continue;
            }

            $command->setAliases(array_merge($command->getAliases(), array($aliasCommandName)));
        }
    }

    /**
     * @param Application $application
     */
    public function registerCustomCommands(Application $application)
    {
        foreach ($this->getArray(array('commands', 'customCommands')) as $commandClass) {
            $commandName = null;
            if (is_array($commandClass)) {
                // Support for key => value (name -> class)
                $commandName = key($commandClass);
                $commandClass = current($commandClass);
            }
            $command = $this->newCommand($commandClass, $commandName);
            $this->debugWriteln(sprintf('<debug>Add command </debug><comment>%s</comment>', get_class($command)));
            $application->add($command);
        }
    }

    /**
     * @param string $className
     * @param string|null $commandName
     * @return Command
     * @throws InvalidArgumentException
     */
    private function newCommand($className, $commandName)
    {
        /** @var Command $command */
        if (!(is_string($className) || is_object($className))) {
            throw new InvalidArgumentException(
                sprintf('Command classname must be string, %s given', gettype($className))
            );
        }

        $command = new $className();
        if (null !== $commandName) {
            $command->setName($commandName);
        }

        return $command;
    }

    /**
     * Adds autoloader prefixes from user's config
     *
     * @param ClassLoader $autoloader
     */
    public function registerCustomAutoloaders(ClassLoader $autoloader)
    {
        $mask = '<debug>Registered %s autoloader </debug> <info>%s</info> -> <comment>%s</comment>';

        foreach ($this->getArray('autoloaders') as $prefix => $path) {
            $autoloader->add($prefix, $path);
            $this->debugWriteln(sprintf($mask, 'PSR-2', $prefix, $path));
        }
<commit_msg>Preserve closing tag on PSR-4 namespace prefixes<commit_after><?php
/*
 * @author <NAME> <<EMAIL>>
 */

namespace N98\Magento\Application;

use Composer\Autoload\ClassLoader;
use InvalidArgumentException;
use N98\Magento\Application;
use N98\Util\ArrayFunctions;
use N98\Util\BinaryString;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class Config
 *
 * Class representing the application configuration. Created to factor out configuration related application
 * functionality from @see N98\Magento\Application
 *
 * @package N98\Magento\Application
 */
class Config
{
    /**
     * @var array config data
     */
    private $config = array();

    /**
     * @var array
     */
    private $partialConfig = array();

    /**
     * @var ConfigurationLoader
     */
    private $loader;

    /**
     * @var array
     */
    private $initConfig;

    /**
     * @var boolean
     */
    private $isPharMode;

    /**
     * @var OutputInterface
     */
    private $output;


    /**
     * Config constructor.
     *
     * @param array $initConfig
     * @param bool $isPharMode
     * @param OutputInterface $output [optional]
     */
    public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null)
    {
        $this->initConfig = $initConfig;
        $this->isPharMode = (bool)$isPharMode;
        $this->output = $output ?: new NullOutput();
    }

    /**
     * alias magerun command in input from config
     *
     * @param InputInterface $input
     * @return ArgvInput|InputInterface
     */
    public function checkConfigCommandAlias(InputInterface $input)
    {
        foreach ($this->getArray(array('commands', 'aliases')) as $alias) {
            if (!is_array($alias)) {
                continue;
            }
            $aliasCommandName = key($alias);
            if ($input->getFirstArgument() !== $aliasCommandName) {
                continue;
            }
            $aliasCommandParams = array_slice(
                BinaryString::trimExplodeEmpty(' ', $alias[$aliasCommandName]),
                1
            );
            if (count($aliasCommandParams) > 0) {
                // replace with aliased data
                $mergedParams = array_merge(
                    array_slice($_SERVER['argv'], 0, 2),
                    $aliasCommandParams,
                    array_slice($_SERVER['argv'], 2)
                );
                $input = new ArgvInput($mergedParams);
            }
        }

        return $input;
    }

    /**
     * @param Command $command
     */
    public function registerConfigCommandAlias(Command $command)
    {
        foreach ($this->getArray(array('commands', 'aliases')) as $alias) {
            if (!is_array($alias)) {
                continue;
            }

            $aliasCommandName = key($alias);
            $commandString = $alias[$aliasCommandName];
            list($originalCommand) = explode(' ', $commandString, 2);
            if ($command->getName() !== $originalCommand) {
                continue;
            }

            $command->setAliases(array_merge($command->getAliases(), array($aliasCommandName)));
        }
    }

    /**
     * @param Application $application
     */
    public function registerCustomCommands(Application $application)
    {
        foreach ($this->getArray(array('commands', 'customCommands')) as $commandClass) {
            $commandName = null;
            if (is_array($commandClass)) {
                // Support for key => value (name -> class)
                $commandName = key($commandClass);
                $commandClass = current($commandClass);
            }
            $command = $this->newCommand($commandClass, $commandName);
            $this->debugWriteln(sprintf('<debug>Add command </debug><comment>%s</comment>', get_class($command)));
            $application->add($command);
        }
    }

    /**
     * @param string $className
     * @param string|null $commandName
     * @return Command
     * @throws InvalidArgumentException
     */
    private function newCommand($className, $commandName)
    {
        /** @var Command $command */
        if (!(is_string($className) || is_object($className))) {
            throw new InvalidArgumentException(
                sprintf('Command classname must be string, %s given', gettype($className))
            );
        }

        $command = new $className();
        if (null !== $commandName) {
            $command->setName($commandName);
        }

        return $command;
    }

    /**
     * Adds autoloader prefixes from user's config
     *
     * @param ClassLoader $autoloader
     */
    public function registerCustomAutoloaders(ClassLoader $autoloader)
    {
        $mask = '<debug>Registered %s autoloader </debug> <info>%s </info>-> <comment>%s</comment>';

        foreach ($this->getArray('autoloaders') as $prefix => $path) {
            $autoloader->add($prefix, $path);
            $this->debugWriteln(sprintf($mask, 'PSR-2', $prefix, $path));
        }

{'fraction_non_alphanumeric': 0.12984403406372597, 'fraction_numerical': 0.0037317003157592574, 'mean_word_length': 2.150090415913201, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 73, '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': '1352700', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2785, 'n_words': 597}