<commit_before><?php

declare(strict_types=1);

namespace Rector\Autoloading;

use Nette\Loaders\RobotLoader;
use Rector\Configuration\Option;
use Rector\FileSystem\FileGuard;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\PackageBuilder\FileSystem\FileSystem;

/**
 * Should it pass autoload files/directories to PHPStan analyzer?
 */
final class AdditionalAutoloader
{
    /**
     * @var string[]
     */
    private $autoloadPaths = [];

    /**
     * @var string[]
     */
    private $excludePaths = [];

    /**
     * @var FileGuard
     */
    private $fileGuard;

    /**
     * @var FileSystem
     */
    private $fileSystem;

    /**
     * @param string[] $autoloadPaths
     * @param string[] $excludePaths
     */
    public function __construct(
        FileGuard $fileGuard,
        FileSystem $fileSystem,
        array $autoloadPaths,
        array $excludePaths
    ) {
        $this->fileGuard = $fileGuard;
        $this->fileSystem = $fileSystem;
        $this->autoloadPaths = $autoloadPaths;
        $this->excludePaths = $excludePaths;
    }

    /**
     * @param string[] $source
     */
    public function autoloadWithInputAndSource(InputInterface $input, array $source): void
    {
        [$autoloadFiles, $autoloadDirectories] = $this->fileSystem->separateFilesAndDirectories($this->autoloadPaths);

        $this->autoloadFileFromInput($input);
        $this->autoloadDirectories($autoloadDirectories);
        $this->autoloadFiles($autoloadFiles);

        // the scanned file needs to be autoloaded
        [$files, $directories] = $this->fileSystem->separateFilesAndDirectories($source);
        $this->autoloadFiles($files);

        foreach ($directories as $directory) {
            // load project autoload
            if (file_exists($directory . '/vendor/autoload.php')) {
                require_once $directory . '/vendor/autoload.php';
            }
        }
    }

    private function autoloadFileFromInput(InputInterface $input): void
    {
        /** @var string|null $autoloadFile */
        $autoloadFile = $input->getOption(Option::OPTION_AUTOLOAD_FILE);
        if ($autoloadFile === null) {
            return;
        }

        $this->autoloadFiles([$autoloadFile]);<commit_msg>skip if option is missing<commit_after><?php

declare(strict_types=1);

namespace Rector\Autoloading;

use Nette\Loaders\RobotLoader;
use Rector\Configuration\Option;
use Rector\FileSystem\FileGuard;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\PackageBuilder\FileSystem\FileSystem;

/**
 * Should it pass autoload files/directories to PHPStan analyzer?
 */
final class AdditionalAutoloader
{
    /**
     * @var string[]
     */
    private $autoloadPaths = [];

    /**
     * @var string[]
     */
    private $excludePaths = [];

    /**
     * @var FileGuard
     */
    private $fileGuard;

    /**
     * @var FileSystem
     */
    private $fileSystem;

    /**
     * @param string[] $autoloadPaths
     * @param string[] $excludePaths
     */
    public function __construct(
        FileGuard $fileGuard,
        FileSystem $fileSystem,
        array $autoloadPaths,
        array $excludePaths
    ) {
        $this->fileGuard = $fileGuard;
        $this->fileSystem = $fileSystem;
        $this->autoloadPaths = $autoloadPaths;
        $this->excludePaths = $excludePaths;
    }

    /**
     * @param string[] $source
     */
    public function autoloadWithInputAndSource(InputInterface $input, array $source): void
    {
        [$autoloadFiles, $autoloadDirectories] = $this->fileSystem->separateFilesAndDirectories($this->autoloadPaths);

        $this->autoloadFileFromInput($input);
        $this->autoloadDirectories($autoloadDirectories);
        $this->autoloadFiles($autoloadFiles);

        // the scanned file needs to be autoloaded
        [$files, $directories] = $this->fileSystem->separateFilesAndDirectories($source);
        $this->autoloadFiles($files);

        foreach ($directories as $directory) {
            // load project autoload
            if (file_exists($directory . '/vendor/autoload.php')) {
                require_once $directory . '/vendor/autoload.php';
            }
        }
    }

    private function autoloadFileFromInput(InputInterface $input): void
    {
        if (! $input->hasOption(Option::OPTION_AUTOLOAD_FILE)) {
            return;
        }

        /** @var string|null $autoloadFile */
        $autoloadFile = $input->getOption(Option::OPTION_AUTOLOAD_FILE);
        if ($autoloadFile === null) {
            return;
        }

        $this->autoloadFiles([$autoloadFile]);
{'fraction_non_alphanumeric': 0.1257031588057118, 'fraction_numerical': 0.00043271311120726956, 'mean_word_length': 2.5865011636927853, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '5437759', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1364, 'n_words': 248}