<commit_before><?php namespace Adviser\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface as Input;
use Symfony\Component\Console\Output\OutputInterface as Output;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\ArrayInput;

use Adviser\Utilities\GitUtility;

class AnalyseRepositoryCommand extends Command
{

    /**
     * @var Git
     */
    protected $git;

    /**
     * @param GitUtility|null $git
     * @return AnalyseRepositoryCommand
     */
    public function __construct(GitUtility $git = null)
    {
        $this->git = $git ?: new GitUtility();

        parent::__construct();
    }

    /**
     * Configure the command.
     *
     * @return void
     */
    protected function configure()
    {
        $this->setName("analyse-repository")
             ->setDescription("Clones a GitHub repository and runs 'analyse' command on it");

        $this->addArgument("name", InputArgument::REQUIRED, "Repository name");
    }

    /**
     * Execute the command.
     *
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        $name = $input->getArgument("name");

        // Validate the name straight away.
        if (count($chunks = explode("/", $name)) != 2) {
            throw new \InvalidArgumentException("Invalid repository name '{$name}'.");
        }

        $output->writeln(sprintf(
            "Cloning <comment>%s</comment> into <info>./%s</info>...",
            $name,
            end($chunks)
        ));

        // If we're in a test environment, stop executing.
        if (defined("ADVISER_UNDER_TEST")) {
            return null;
        }

        // @codeCoverageIgnoreStart

        if ( ! $this->git->cloneGithubRepository($name)) {
            throw new \UnexpectedValueException(
                "Repository https://github.com/{$name} doesn't exist."
            );
        }

        // @codeCoverageIgnoreStop
    }
}<commit_msg>Finish AnalyseRepositoryCommand.<commit_after><?php namespace Adviser\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface as Input;
use Symfony\Component\Console\Output\OutputInterface as Output;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\ArrayInput;

use Adviser\Utilities\GitUtility;

class AnalyseRepositoryCommand extends Command
{

    /**
     * @var Git
     */
    protected $git;

    /**
     * @param GitUtility|null $git
     * @return AnalyseRepositoryCommand
     */
    public function __construct(GitUtility $git = null)
    {
        $this->git = $git ?: new GitUtility();

        parent::__construct();
    }

    /**
     * Configure the command.
     *
     * @return void
     */
    protected function configure()
    {
        $this->setName("analyse-repository")
             ->setDescription("Clones a GitHub repository and runs 'analyse' command on it");

        $this->addArgument("name", InputArgument::REQUIRED, "Repository name");
    }

    /**
     * Execute the command.
     *
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        $name = $input->getArgument("name");

        // Validate the name straight away.
        if (count($chunks = explode("/", $name)) != 2) {
            throw new \InvalidArgumentException("Invalid repository name '{$name}'.");
        }

        $output->writeln(sprintf(
            "Cloning <comment>%s</comment> into <info>%s/%s</info>...",
            $name,
            getcwd(),
            end($chunks)
        ));

        // If we're in a test environment, stop executing.
        if (defined("ADVISER_UNDER_TEST")) {
            return null;
        }

        // @codeCoverageIgnoreStart

        if ( ! $this->git->cloneGithubRepository($name)) {
            throw new \UnexpectedValueException(
                "Repository https://github.com/{$name} doesn't exist."
            );
        }

        // Change the working directory.
        chdir($path = getcwd()."/".end($chunks));

        $output->writeln(sprintf(
            "Changed the current working directory to <comment>%s</comment>.",
            $path
        ));
        $output->writeln("");

        // Running "AnalyseCommand"...
        $this->getApplication()->find("analyse")->run(new ArrayInput([""]), $output);

        // Change back, remove the directory.
        chdir(getcwd()."/..");
        $this->removeDirectory($path);

        $output->writeln("");
        $output->writeln(sprintf(
            "Switching back to <info>%s</info>, removing <comment>%s</comment>...",
            getcwd(),
            $path
        ));

        // @codeCoverageIgnoreStop
    }

    /**
     * Remove a directory recursively (with all files/directories stored in it).
     * @codeCoverageIgnore
     *
     * @param string $path
     * @return void
     */
    protected function removeDirectory($path)
    {
        $iterator = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
            \RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($iterator as $element) {
            if ($element->isDir()) {
                rmdir($element->getRealPath());
            } else {
                unlink($element->getRealPath());
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1356937455068296, 'fraction_numerical': 0.0003594536304816679, 'mean_word_length': 2.136978579481398, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 44, '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': '3625308', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1528, 'n_words': 335}