<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 Adviser\ValidatorLoader;
use Adviser\Validators\ValidatorInterface;

class AnalyseCommand extends Command
{

    /**
     * @var ValidatorLoader
     */
    protected $loader;

    /**
     * @var array
     */
    protected $messageCounter = [
        "normal"  => 0,
        "warning" => 0,
        "error"   => 0,
    ];

    /**
     * @var array
     */
    protected $validators = [];

    /**
     * Four spaces.
     *
     * @var string
     */
    protected $indentation = "    ";

    /**
     * @param ValidatorLoader|null $loader
     * @return AnalyseCommand
     */
    public function __construct(ValidatorLoader $loader = null)
    {
        $this->loader = $loader ?: new ValidatorLoader;

        parent::__construct();
    }

    /**
     * Configure the command.
     *
     * @return void
     */
    protected function configure()
    {
        $this->setName("analyse")
             ->setDescription("Suggests you possible improvements for your project");
    }

    /**
     * Execute the command.
     *
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        $this->validators = $this->loader->load();

        $this->writeHead($output);

        foreach ($this->validators as $validator) {
            $this->runValidator($validator, $output);
        }

        $this->writeSummary($output);
    }

    /**
     * @codeCoverageIgnore
     *
     * @param ValidatorInterface $validator
     * @param Output $output
     * @return void
     */
    protected function runValidator(ValidatorInterface $validator, Output $output)
    {
        // We don't want to run the validators in our unit tests.
        if (defined("ADVISER_UNDER_TEST")) {
            return null;
        }

        $output->write($this->indentation);
        $output->writeln("Running <comment>".$validator->getName()."</comment> validator...");

        $bag = $validator->handle();

        // Update the counters.
        $this->messageCounter['normal']  += count($bag->getNormalMessages());
        $this->messageCounter['warning'] += count($bag->getWarnings());
        $this->messageCounter['error']   += count($bag->getErrors());

        $output->writeln("");

        // Display the messages stored in the bag.
        foreach ($bag->getAll() as $message) {
            $output->write(str_repeat($this->indentation, 2));
            $output->writeln($message->format());
        }

        $output->writeln("");
    }

    /**<commit_msg>Show execution time.<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 Adviser\ValidatorLoader;
use Adviser\Validators\ValidatorInterface;

class AnalyseCommand extends Command
{

    /**
     * @var ValidatorLoader
     */
    protected $loader;

    /**
     * @var array
     */
    protected $messageCounter = [
        "normal"  => 0,
        "warning" => 0,
        "error"   => 0,
    ];

    /**
     * @var array
     */
    protected $validators = [];

    /**
     * Four spaces.
     *
     * @var string
     */
    protected $indentation = "    ";

    /**
     * @param ValidatorLoader|null $loader
     * @return AnalyseCommand
     */
    public function __construct(ValidatorLoader $loader = null)
    {
        $this->loader = $loader ?: new ValidatorLoader;

        parent::__construct();
    }

    /**
     * Configure the command.
     *
     * @return void
     */
    protected function configure()
    {
        $this->setName("analyse")
             ->setDescription("Suggests you possible improvements for your project");
    }

    /**
     * Execute the command.
     *
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        $this->validators = $this->loader->load();

        $this->writeHead($output);

        foreach ($this->validators as $validator) {
            $this->runValidator($validator, $output);
        }

        $this->writeSummary($output);
    }

    /**
     * @codeCoverageIgnore
     *
     * @param ValidatorInterface $validator
     * @param Output $output
     * @return void
     */
    protected function runValidator(ValidatorInterface $validator, Output $output)
    {
        // We don't want to run the validators in our unit tests.
        if (defined("ADVISER_UNDER_TEST")) {
            return null;
        }

        $output->write($this->indentation);
        $output->writeln("Running <comment>".$validator->getName()."</comment> validator...");

        $startTime = microtime(true);

        $bag = $validator->handle();

        // Display how many seconds it took to run this validator.
        $output->write($this->indentation);
        $output->write("Done in ");
        $output->write(round(microtime(true) - $startTime, 5));
        $output->writeln(" seconds.");

        // Update the counters.
        $this->messageCounter['normal']  += count($bag->getNormalMessages());
        $this->messageCounter['warning'] += count($bag->getWarnings());
        $this->messageCounter['error']   += count($bag->getErrors());

        $output->writeln("");

        // Display the messages stored in the bag.
        foreach ($bag->getAll() as $message) {
            $output->write(str_repeat($this->indentation, 2));
            $output->writeln($message->format());
        }

        $output->writeln("");
    }

    /**
{'fraction_non_alphanumeric': 0.14561613399418902, 'fraction_numerical': 0.0015381985985301657, 'mean_word_length': 2.2153846153846155, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 74, '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': '6187953', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1623, 'n_words': 350}