<commit_before><?php
/**
 * DirCommand.php
 *
 * MIT LICENSE
 *
 * LICENSE: This source file is subject to the MIT license.
 * A copy of the licenses text was distributed alongside this
 * file (usually the repository or package root). The text can also
 * be obtained through one of the following sources:
 * * http://opensource.org/licenses/MIT
 * * https://github.com/suralc/pvra/blob/master/LICENSE
 *
 * @author     suralc <<EMAIL>>
 * @license    http://opensource.org/licenses/MIT  MIT
 */
namespace Pvra\Console\Commands;


use Pvra\AnalysisResult;
use Pvra\Console\Services\FileFinderBuilder;
use Pvra\Result\Collection;
use Pvra\Result\Reasoning;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
 * Class DirCommand
 *
 * @package Pvra\Console\Commands
 */
class DirCommand extends PvraBaseCommand
{
    const GROUP_BY_NAME = 'name',
        GROUP_BY_VERSION = 'version';

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this
            ->setName('analyse:dir')
            ->setDescription('Iterates over a directory and runs the specified analysers.');

        parent::configure();

        $this
            ->addOption('recursive', 'r', InputOption::VALUE_NONE, 'Iterate recursive over directory')
            ->addOption('groupBy', 'g', InputOption::VALUE_REQUIRED, 'Group output by name or required version.',
                self::GROUP_BY_NAME)
            ->addOption('sortBy', 'o', InputOption::VALUE_REQUIRED,
                'Sort order of remaining files. Only takes effect while using --groupBy=n[ame]',
                FileFinderBuilder::SORT_BY_NAME)
            ->addOption('listFilesOnly', null, InputOption::VALUE_NONE,
                'Only list matched files and do not run analysis.')
            ->addArgument('filters', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Filter', ['name:*.php']);
    }

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $dir = $input->getArgument('target');

        $files = (new FileFinderBuilder($dir))
            ->isRecursive($input->getOption('recursive'))
            ->sortBy($input->getOption('sortBy'))
            ->withFilters($input->getArgument('filters'))
            ->getFinder();

        if ($files->count() === 0) {
            $output->writeln('<error>No files processed!</error>');
            return;
        }

        if ($input->getOption('listFilesOnly')) {
            $output->writeln($files->count() . ' files to process.');
            /** @var \SplFileInfo $file */
            foreach ($files as $file) {
                $output->writeln($file->getRealPath());
            }
            return;
        }


        $results = new Collection();

        /** @var \SplFileInfo $file */
        foreach ($files as $file) {
            if ($file->isFile()) {
                $req = $this->createFileAnalyserInstance($file->getPathname());
                $req->attachRequirementVisitors($this->createNodeWalkerInstances($input->getOption('libraryDataSource')));
                $results->add($req->run());
            }
        }

        if ($input->getOption('groupBy') === self::GROUP_BY_NAME) {
            $this->renderResultCollectionByName($results, $output, $input);
        } elseif ($input->getOption('groupBy') === self::GROUP_BY_VERSION) {
            $this->renderResultCollectionByRequiredVersion($results, $output);
        } else {
            throw new \InvalidArgumentException('The value given to the groupBy option is not supported.');
        }

        if ($file = $input->getOption('saveAsFile')) {
            if (file_exists($file)) {
                $output->writeln(sprintf('<error>%s already exists. Cannot override an already existing file!</error>',
                    $file));
            } else {
                $output->writeln(sprintf('<info>Generating output file at %s</info>', $file));
                switch ($input->getOption('saveFormat')) {
                    case 'json': {
                        file_put_contents($file, json_encode($results));
                        break;
                    }
                    default: {
                        $output->writeln('<error>Invalid save format.</error>');
                    }<commit_msg>pathname -> realpath<commit_after><?php
/**
 * DirCommand.php
 *
 * MIT LICENSE
 *
 * LICENSE: This source file is subject to the MIT license.
 * A copy of the licenses text was distributed alongside this
 * file (usually the repository or package root). The text can also
 * be obtained through one of the following sources:
 * * http://opensource.org/licenses/MIT
 * * https://github.com/suralc/pvra/blob/master/LICENSE
 *
 * @author     suralc <<EMAIL>>
 * @license    http://opensource.org/licenses/MIT  MIT
 */
namespace Pvra\Console\Commands;


use Pvra\AnalysisResult;
use Pvra\Console\Services\FileFinderBuilder;
use Pvra\Result\Collection;
use Pvra\Result\Reasoning;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
 * Class DirCommand
 *
 * @package Pvra\Console\Commands
 */
class DirCommand extends PvraBaseCommand
{
    const GROUP_BY_NAME = 'name',
        GROUP_BY_VERSION = 'version';

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this
            ->setName('analyse:dir')
            ->setDescription('Iterates over a directory and runs the specified analysers.');

        parent::configure();

        $this
            ->addOption('recursive', 'r', InputOption::VALUE_NONE, 'Iterate recursive over directory')
            ->addOption('groupBy', 'g', InputOption::VALUE_REQUIRED, 'Group output by name or required version.',
                self::GROUP_BY_NAME)
            ->addOption('sortBy', 'o', InputOption::VALUE_REQUIRED,
                'Sort order of remaining files. Only takes effect while using --groupBy=n[ame]',
                FileFinderBuilder::SORT_BY_NAME)
            ->addOption('listFilesOnly', null, InputOption::VALUE_NONE,
                'Only list matched files and do not run analysis.')
            ->addArgument('filters', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Filter', ['name:*.php']);
    }

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $dir = $input->getArgument('target');

        $files = (new FileFinderBuilder($dir))
            ->isRecursive($input->getOption('recursive'))
            ->sortBy($input->getOption('sortBy'))
            ->withFilters($input->getArgument('filters'))
            ->getFinder();

        if ($files->count() === 0) {
            $output->writeln('<error>No files processed!</error>');
            return;
        }

        if ($input->getOption('listFilesOnly')) {
            $output->writeln($files->count() . ' files to process.');
            /** @var \SplFileInfo $file */
            foreach ($files as $file) {
                $output->writeln($file->getRealPath());
            }
            return;
        }


        $results = new Collection();

        /** @var \SplFileInfo $file */
        foreach ($files as $file) {
            if ($file->isFile()) {
                $req = $this->createFileAnalyserInstance($file->getRealPath());
                $req->attachRequirementVisitors($this->createNodeWalkerInstances($input->getOption('libraryDataSource')));
                $results->add($req->run());
            }
        }

        if ($input->getOption('groupBy') === self::GROUP_BY_NAME) {
            $this->renderResultCollectionByName($results, $output, $input);
        } elseif ($input->getOption('groupBy') === self::GROUP_BY_VERSION) {
            $this->renderResultCollectionByRequiredVersion($results, $output);
        } else {
            throw new \InvalidArgumentException('The value given to the groupBy option is not supported.');
        }

        if ($file = $input->getOption('saveAsFile')) {
            if (file_exists($file)) {
                $output->writeln(sprintf('<error>%s already exists. Cannot override an already existing file!</error>',
                    $file));
            } else {
                $output->writeln(sprintf('<info>Generating output file at %s</info>', $file));
                switch ($input->getOption('saveFormat')) {
                    case 'json': {
                        file_put_contents($file, json_encode($results));
                        break;
                    }
                    default: {
                        $output->writeln('<error>Invalid save format.</error>');
                    }
{'fraction_non_alphanumeric': 0.1376660445713031, 'fraction_numerical': 0.0002195630694917115, 'mean_word_length': 2.3616236162361623, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 102, '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': '956049', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2397, 'n_words': 531}