<commit_before><?php

declare(strict_types=1);

namespace Mihaeu\PhpDependencies\Cli;

use Mihaeu\PhpDependencies\Analyser\Analyser;
use Mihaeu\PhpDependencies\Analyser\Parser;
use Mihaeu\PhpDependencies\Dependencies\DependencyPairCollection;
use Mihaeu\PhpDependencies\OS\PhpFileCollection;
use Mihaeu\PhpDependencies\OS\PhpFileFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

abstract class BaseCommand extends Command
{
    /** @var PhpFileFinder */
    protected $phpFileFinder;

    /** @var Parser */
    protected $parser;

    /** @var Analyser */
    protected $analyser;

    /** @var string */
    protected $defaultFormat;

    /** @var string[] */
    protected $allowedFormats;

    /**
     * @param string        $name
     * @param PhpFileFinder $phpFileFinder
     * @param Parser        $parser
     * @param Analyser      $analyser
     */
    public function __construct(
        string $name,
        PhpFileFinder $phpFileFinder,
        Parser $parser,
        Analyser $analyser
    ) {
        parent::__construct($name);

        $this->phpFileFinder = $phpFileFinder;
        $this->parser = $parser;
        $this->analyser = $analyser;
    }

    protected function configure()
    {
        $this
            ->addArgument(
                'source',
                InputArgument::IS_ARRAY | InputArgument::REQUIRED,
                'Location of your PHP source files.'
            )
            ->addOption(
                'internals',
                null,
                InputOption::VALUE_NONE,
                'Check for dependencies from internal PHP Classes like SplFileInfo.'
            )
            ->addOption(
                'depth',
                'd',
                InputOption::VALUE_OPTIONAL,
                'Output dependencies as packages instead of single classes.',
                0
            )
            ->addOption(
                'memory',
                'm',
                InputOption::VALUE_REQUIRED,
                'Set maximum memory e.g. 2048M'
            )
            ->addOption(
                'underscore-namespaces',
                'u',
                InputOption::VALUE_NONE,
                'Parse underscores in Class names as namespaces.'
            )
            ->addOption(
                'filter-namespace',
                null,
                InputOption::VALUE_REQUIRED,
                'Analyse only classes from this namespace.'
            )
            ->addOption(
                'no-classes',
                null,
                InputOption::VALUE_NONE,
                'Remove all classes and analyse only namespaces.'
            )
        ;
    }

    /**
     * @param string $destination
     *
     * @throws \Exception
     */
    protected function ensureOutputFormatIsValid(string $destination)
    {
        if (!in_array(preg_replace('/.+\.(\w+)$/', '$1', $destination), $this->allowedFormats, true)) {
            throw new \InvalidArgumentException('Output format is not allowed ('.implode(', ', $this->allowedFormats).')');
        }
    }

    /**
     * @param string[] $sources
     *
     * @throws \Exception
     */
    protected function ensureSourcesAreReadable(array $sources)
    {
        foreach ($sources as $source) {
            if (!is_readable($source)) {
                throw new \InvalidArgumentException('File/Directory does not exist or is not readable.');
            }
        }
    }

    /**
     * @param string[] $sources
     *
     * @return DependencyPairCollection
     */
    protected function detectDependencies(array $sources) : DependencyPairCollection
    {
        $files = array_reduce($sources, function (PhpFileCollection $collection, string $source) {
            return $collection->addAll($this->phpFileFinder->find(new \SplFileInfo($source)));
        }, new PhpFileCollection());

        return $this->analyser->analyse(
            $this->parser->parse($files)
        );
    }

    /**
     * @param DependencyPairCollection $dependencies
     * @param string[] $options
     *
     * @return DependencyPairCollection
     */
    protected function filterByInputOptions(DependencyPairCollection $dependencies, array $options) : DependencyPairCollection
    {
        if (!$options['internals']) {
            $dependencies = $dependencies->removeInternals();
        }

        if ($options['filter-namespace']) {
            $dependencies = $dependencies->filterByNamespace($options['filter-namespace']);
        }

        if (isset($options['no-classes'])) {
            $dependencies = $dependencies->filterClasses();
        }

        return $dependencies;
    }

    /**
     * @param string $destination
     *
     * @throws \Exception
     */
    protected function ensureDestinationIsWritable(string $destination)
    {
        if (!is_writable(dirname($destination))) {
            throw new \InvalidArgumentException('Destination is not writable.');
        }
    }
}<commit_msg>Check if the no-classes option is true<commit_after><?php

declare(strict_types=1);

namespace Mihaeu\PhpDependencies\Cli;

use Mihaeu\PhpDependencies\Analyser\Analyser;
use Mihaeu\PhpDependencies\Analyser\Parser;
use Mihaeu\PhpDependencies\Dependencies\DependencyPairCollection;
use Mihaeu\PhpDependencies\OS\PhpFileCollection;
use Mihaeu\PhpDependencies\OS\PhpFileFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

abstract class BaseCommand extends Command
{
    /** @var PhpFileFinder */
    protected $phpFileFinder;

    /** @var Parser */
    protected $parser;

    /** @var Analyser */
    protected $analyser;

    /** @var string */
    protected $defaultFormat;

    /** @var string[] */
    protected $allowedFormats;

    /**
     * @param string        $name
     * @param PhpFileFinder $phpFileFinder
     * @param Parser        $parser
     * @param Analyser      $analyser
     */
    public function __construct(
        string $name,
        PhpFileFinder $phpFileFinder,
        Parser $parser,
        Analyser $analyser
    ) {
        parent::__construct($name);

        $this->phpFileFinder = $phpFileFinder;
        $this->parser = $parser;
        $this->analyser = $analyser;
    }

    protected function configure()
    {
        $this
            ->addArgument(
                'source',
                InputArgument::IS_ARRAY | InputArgument::REQUIRED,
                'Location of your PHP source files.'
            )
            ->addOption(
                'internals',
                null,
                InputOption::VALUE_NONE,
                'Check for dependencies from internal PHP Classes like SplFileInfo.'
            )
            ->addOption(
                'depth',
                'd',
                InputOption::VALUE_OPTIONAL,
                'Output dependencies as packages instead of single classes.',
                0
            )
            ->addOption(
                'memory',
                'm',
                InputOption::VALUE_REQUIRED,
                'Set maximum memory e.g. 2048M'
            )
            ->addOption(
                'underscore-namespaces',
                'u',
                InputOption::VALUE_NONE,
                'Parse underscores in Class names as namespaces.'
            )
            ->addOption(
                'filter-namespace',
                null,
                InputOption::VALUE_REQUIRED,
                'Analyse only classes from this namespace.'
            )
            ->addOption(
                'no-classes',
                null,
                InputOption::VALUE_NONE,
                'Remove all classes and analyse only namespaces.'
            )
        ;
    }

    /**
     * @param string $destination
     *
     * @throws \Exception
     */
    protected function ensureOutputFormatIsValid(string $destination)
    {
        if (!in_array(preg_replace('/.+\.(\w+)$/', '$1', $destination), $this->allowedFormats, true)) {
            throw new \InvalidArgumentException('Output format is not allowed ('.implode(', ', $this->allowedFormats).')');
        }
    }

    /**
     * @param string[] $sources
     *
     * @throws \Exception
     */
    protected function ensureSourcesAreReadable(array $sources)
    {
        foreach ($sources as $source) {
            if (!is_readable($source)) {
                throw new \InvalidArgumentException('File/Directory does not exist or is not readable.');
            }
        }
    }

    /**
     * @param string[] $sources
     *
     * @return DependencyPairCollection
     */
    protected function detectDependencies(array $sources) : DependencyPairCollection
    {
        $files = array_reduce($sources, function (PhpFileCollection $collection, string $source) {
            return $collection->addAll($this->phpFileFinder->find(new \SplFileInfo($source)));
        }, new PhpFileCollection());

        return $this->analyser->analyse(
            $this->parser->parse($files)
        );
    }

    /**
     * @param DependencyPairCollection $dependencies
     * @param string[] $options
     *
     * @return DependencyPairCollection
     */
    protected function filterByInputOptions(DependencyPairCollection $dependencies, array $options) : DependencyPairCollection
    {
        if (!$options['internals']) {
            $dependencies = $dependencies->removeInternals();
        }

        if ($options['filter-namespace']) {
            $dependencies = $dependencies->filterByNamespace($options['filter-namespace']);
        }

        if (isset($options['no-classes']) && $options['no-classes'] === true) {
            $dependencies = $dependencies->filterClasses();
        }

        return $dependencies;
    }

    /**
     * @param string $destination
     *
     * @throws \Exception
     */
    protected function ensureDestinationIsWritable(string $destination)
    {
        if (!is_writable(dirname($destination))) {
            throw new \InvalidArgumentException('Destination is not writable.');
        }
    }
}
{'fraction_non_alphanumeric': 0.11150303742896335, 'fraction_numerical': 0.0013717421124828531, 'mean_word_length': 1.9088059276147051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '3675790', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2620, 'n_words': 560}