<commit_before><?php declare(strict_types=1);
/*
 * This file is part of the WP Starter package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace WeCodeMore\WpStarter\Util;

use Composer\Composer;
use WeCodeMore\WpStarter\ComposerPlugin;
use WeCodeMore\WpStarter\Config\Config;
use WeCodeMore\WpStarter\Step\NullStep;
use WeCodeMore\WpStarter\Step\Step;
use WeCodeMore\WpStarter\Step\WpCliCommandsStep;

/**
 * @package WeCodeMore\WpStarter\Util
 */
class SelectedStepsFactory
{
    const SKIP = 1;
    const SKIP_CUSTOM = 2;
    const IGNORE_SKIP_CONFIG = 4;
    const MODE_COMMAND = 16;

    /**
     * @var bool
     */
    private $commandMode;

    /**
     * @var bool
     */
    private $skip;

    /**
     * @var bool
     */
    private $skipCustom;

    /**
     * @var bool
     */
    private $ignoreSkipConfig;

    /**
     * @var string[]
     */
    private $names;

    /**
     * @var int
     */
    private $inputErrors = 0;

    /**
     * @var int
     */
    private $configErrors = 0;

    /**
     * @var bool
     */
    private $emptyInput = false;

    /**
     * @return SelectedStepsFactory
     */
    public static function autorun(): SelectedStepsFactory
    {
        return new static();
    }

    /**
     * @param int $flags
     * @param string[] $stepNames
     */
    public function __construct(int $flags = 0, string ...$stepNames)
    {
        $this->commandMode = ($flags & self::MODE_COMMAND) === self::MODE_COMMAND;
        if (!$this->commandMode) {
            $this->skip = false;
            $this->skipCustom = false;
            $this->ignoreSkipConfig = false;
            $this->names = [];
        }

        $this->skip = ($flags & self::SKIP) === self::SKIP;
        $this->skipCustom = ($flags & self::SKIP_CUSTOM) === self::SKIP_CUSTOM;
        $this->ignoreSkipConfig = ($flags & self::IGNORE_SKIP_CONFIG) === self::IGNORE_SKIP_CONFIG;
        $this->names = $stepNames;
    }

    /**
     * @return bool
     */
    public function isSelectedCommandMode(): bool
    {
        return $this->names && !$this->skip;
    }

    /**
     * @param Locator $locator
     * @param Composer $composer
     * @return Step[]
     */
    public function selectAndFactory(Locator $locator, Composer $composer): array
    {
        $this->inputErrors = 0;
        $this->configErrors = 0;
        $this->emptyInput = false;

        $config = $locator->config();

        $defaultSteps = ComposerPlugin::defaultSteps();
        $customSteps = $config[Config::CUSTOM_STEPS]->unwrapOrFallback([]);
        $commandSteps = $config[Config::COMMAND_STEPS]->unwrapOrFallback([]);

        $selectedCommandMode = $this->isSelectedCommandMode();

        $allSteps = $this->skipCustom ? $defaultSteps : array_merge($defaultSteps, $customSteps);
        if ($selectedCommandMode) {
            $allSteps = array_merge($allSteps, $commandSteps);
        }

        $allSteps = $this->filterValidSteps($allSteps);
        if (!$allSteps) {
            return [];
        }

        $validSelected = [];
        if ($this->names) {
            $allSelected = array_fill_keys($this->names, 1);
            $validSelected = array_intersect_key($allSelected, $allSteps);
            $this->inputErrors = count($allSelected) - count($validSelected);<commit_msg>Don't load WP CLI step if nothing to run<commit_after><?php declare(strict_types=1);
/*
 * This file is part of the WP Starter package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace WeCodeMore\WpStarter\Util;

use Composer\Composer;
use WeCodeMore\WpStarter\ComposerPlugin;
use WeCodeMore\WpStarter\Config\Config;
use WeCodeMore\WpStarter\Step\NullStep;
use WeCodeMore\WpStarter\Step\Step;
use WeCodeMore\WpStarter\Step\WpCliCommandsStep;

/**
 * @package WeCodeMore\WpStarter\Util
 */
class SelectedStepsFactory
{
    const SKIP = 1;
    const SKIP_CUSTOM = 2;
    const IGNORE_SKIP_CONFIG = 4;
    const MODE_COMMAND = 16;

    /**
     * @var bool
     */
    private $commandMode;

    /**
     * @var bool
     */
    private $skip;

    /**
     * @var bool
     */
    private $skipCustom;

    /**
     * @var bool
     */
    private $ignoreSkipConfig;

    /**
     * @var string[]
     */
    private $names;

    /**
     * @var int
     */
    private $inputErrors = 0;

    /**
     * @var int
     */
    private $configErrors = 0;

    /**
     * @var bool
     */
    private $emptyInput = false;

    /**
     * @return SelectedStepsFactory
     */
    public static function autorun(): SelectedStepsFactory
    {
        return new static();
    }

    /**
     * @param int $flags
     * @param string[] $stepNames
     */
    public function __construct(int $flags = 0, string ...$stepNames)
    {
        $this->commandMode = ($flags & self::MODE_COMMAND) === self::MODE_COMMAND;
        if (!$this->commandMode) {
            $this->skip = false;
            $this->skipCustom = false;
            $this->ignoreSkipConfig = false;
            $this->names = [];
        }

        $this->skip = ($flags & self::SKIP) === self::SKIP;
        $this->skipCustom = ($flags & self::SKIP_CUSTOM) === self::SKIP_CUSTOM;
        $this->ignoreSkipConfig = ($flags & self::IGNORE_SKIP_CONFIG) === self::IGNORE_SKIP_CONFIG;
        $this->names = $stepNames;
    }

    /**
     * @return bool
     */
    public function isSelectedCommandMode(): bool
    {
        return $this->names && !$this->skip;
    }

    /**
     * @param Locator $locator
     * @param Composer $composer
     * @return Step[]
     */
    public function selectAndFactory(Locator $locator, Composer $composer): array
    {
        $this->inputErrors = 0;
        $this->configErrors = 0;
        $this->emptyInput = false;

        $config = $locator->config();

        $defaultSteps = ComposerPlugin::defaultSteps();
        $customSteps = $config[Config::CUSTOM_STEPS]->unwrapOrFallback([]);
        $commandSteps = $config[Config::COMMAND_STEPS]->unwrapOrFallback([]);

        $hasCliConfig = $config[Config::WP_CLI_FILES]->notEmpty()
            || $config[Config::WP_CLI_COMMANDS]->notEmpty();
        if (!$hasCliConfig) {
            unset($defaultSteps[WpCliCommandsStep::NAME]);
        }

        $selectedCommandMode = $this->isSelectedCommandMode();

        $allSteps = $this->skipCustom ? $defaultSteps : array_merge($defaultSteps, $customSteps);
        if ($selectedCommandMode) {
            $allSteps = array_merge($allSteps, $commandSteps);
        }

        $allSteps = $this->filterValidSteps($allSteps);
        if (!$allSteps) {
            return [];
        }

        $validSelected = [];
        if ($this->names) {
            $allSelected = array_fill_keys($this->names, 1);
            $validSelected = array_intersect_key($allSelected, $allSteps);
            $this->inputErrors = count($allSelected) - count($validSelected);
{'fraction_non_alphanumeric': 0.13977272727272727, 'fraction_numerical': 0.003409090909090909, 'mean_word_length': 2.3688995215311004, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '6625786', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2184, 'n_words': 437}