Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Config.php

function Config::processLongArgument

Processes a long (--example) command-line argument.

Parameters

string $arg The command line argument.:

int $pos The position of the argument on the command line.:

Return value

void

Throws

\PHP_CodeSniffer\Exceptions\DeepExitException

1 call to Config::processLongArgument()
Config::setCommandLineValues in vendor/squizlabs/php_codesniffer/src/Config.php
Set the command line values.

File

vendor/squizlabs/php_codesniffer/src/Config.php, line 749

Class

Config
Stores the configuration used to run PHPCS and PHPCBF.

Namespace

PHP_CodeSniffer

Code

public function processLongArgument($arg, $pos) {
    switch ($arg) {
        case 'help':
            ob_start();
            $this->printUsage();
            $output = ob_get_contents();
            ob_end_clean();
            throw new DeepExitException($output, 0);
        case 'version':
            $output = 'PHP_CodeSniffer version ' . self::VERSION . ' (' . self::STABILITY . ') ';
            $output .= 'by Squiz and PHPCSStandards' . PHP_EOL;
            throw new DeepExitException($output, 0);
        case 'colors':
            if (isset(self::$overriddenDefaults['colors']) === true) {
                break;
            }
            $this->colors = true;
            self::$overriddenDefaults['colors'] = true;
            break;
        case 'no-colors':
            if (isset(self::$overriddenDefaults['colors']) === true) {
                break;
            }
            $this->colors = false;
            self::$overriddenDefaults['colors'] = true;
            break;
        case 'cache':
            if (isset(self::$overriddenDefaults['cache']) === true) {
                break;
            }
            if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
                $this->cache = true;
                self::$overriddenDefaults['cache'] = true;
            }
            break;
        case 'no-cache':
            if (isset(self::$overriddenDefaults['cache']) === true) {
                break;
            }
            $this->cache = false;
            self::$overriddenDefaults['cache'] = true;
            break;
        case 'ignore-annotations':
            if (isset(self::$overriddenDefaults['annotations']) === true) {
                break;
            }
            $this->annotations = false;
            self::$overriddenDefaults['annotations'] = true;
            break;
        case 'config-set':
            if (isset($this->cliArgs[$pos + 1]) === false || isset($this->cliArgs[$pos + 2]) === false) {
                $error = 'ERROR: Setting a config option requires a name and value' . PHP_EOL . PHP_EOL;
                $error .= $this->printShortUsage(true);
                throw new DeepExitException($error, 3);
            }
            $key = $this->cliArgs[$pos + 1];
            $value = $this->cliArgs[$pos + 2];
            $current = self::getConfigData($key);
            try {
                $this->setConfigData($key, $value);
            } catch (Exception $e) {
                throw new DeepExitException($e->getMessage() . PHP_EOL, 3);
            }
            $output = 'Using config file: ' . self::$configDataFile . PHP_EOL . PHP_EOL;
            if ($current === null) {
                $output .= "Config value \"{$key}\" added successfully" . PHP_EOL;
            }
            else {
                $output .= "Config value \"{$key}\" updated successfully; old value was \"{$current}\"" . PHP_EOL;
            }
            throw new DeepExitException($output, 0);
        case 'config-delete':
            if (isset($this->cliArgs[$pos + 1]) === false) {
                $error = 'ERROR: Deleting a config option requires the name of the option' . PHP_EOL . PHP_EOL;
                $error .= $this->printShortUsage(true);
                throw new DeepExitException($error, 3);
            }
            $output = 'Using config file: ' . self::$configDataFile . PHP_EOL . PHP_EOL;
            $key = $this->cliArgs[$pos + 1];
            $current = self::getConfigData($key);
            if ($current === null) {
                $output .= "Config value \"{$key}\" has not been set" . PHP_EOL;
            }
            else {
                try {
                    $this->setConfigData($key, null);
                } catch (Exception $e) {
                    throw new DeepExitException($e->getMessage() . PHP_EOL, 3);
                }
                $output .= "Config value \"{$key}\" removed successfully; old value was \"{$current}\"" . PHP_EOL;
            }
            throw new DeepExitException($output, 0);
        case 'config-show':
            ob_start();
            $data = self::getAllConfigData();
            echo 'Using config file: ' . self::$configDataFile . PHP_EOL . PHP_EOL;
            $this->printConfigData($data);
            $output = ob_get_contents();
            ob_end_clean();
            throw new DeepExitException($output, 0);
        case 'runtime-set':
            if (isset($this->cliArgs[$pos + 1]) === false || isset($this->cliArgs[$pos + 2]) === false) {
                $error = 'ERROR: Setting a runtime config option requires a name and value' . PHP_EOL . PHP_EOL;
                $error .= $this->printShortUsage(true);
                throw new DeepExitException($error, 3);
            }
            $key = $this->cliArgs[$pos + 1];
            $value = $this->cliArgs[$pos + 2];
            $this->cliArgs[$pos + 1] = '';
            $this->cliArgs[$pos + 2] = '';
            self::setConfigData($key, $value, true);
            if (isset(self::$overriddenDefaults['runtime-set']) === false) {
                self::$overriddenDefaults['runtime-set'] = [];
            }
            self::$overriddenDefaults['runtime-set'][$key] = true;
            break;
        default:
            if (substr($arg, 0, 7) === 'sniffs=') {
                if (isset(self::$overriddenDefaults['sniffs']) === true) {
                    break;
                }
                $sniffs = explode(',', substr($arg, 7));
                foreach ($sniffs as $sniff) {
                    if (substr_count($sniff, '.') !== 2) {
                        $error = 'ERROR: The specified sniff code "' . $sniff . '" is invalid' . PHP_EOL . PHP_EOL;
                        $error .= $this->printShortUsage(true);
                        throw new DeepExitException($error, 3);
                    }
                }
                $this->sniffs = $sniffs;
                self::$overriddenDefaults['sniffs'] = true;
            }
            else {
                if (substr($arg, 0, 8) === 'exclude=') {
                    if (isset(self::$overriddenDefaults['exclude']) === true) {
                        break;
                    }
                    $sniffs = explode(',', substr($arg, 8));
                    foreach ($sniffs as $sniff) {
                        if (substr_count($sniff, '.') !== 2) {
                            $error = 'ERROR: The specified sniff code "' . $sniff . '" is invalid' . PHP_EOL . PHP_EOL;
                            $error .= $this->printShortUsage(true);
                            throw new DeepExitException($error, 3);
                        }
                    }
                    $this->exclude = $sniffs;
                    self::$overriddenDefaults['exclude'] = true;
                }
                else {
                    if (defined('PHP_CODESNIFFER_IN_TESTS') === false && substr($arg, 0, 6) === 'cache=') {
                        if (isset(self::$overriddenDefaults['cache']) === true && $this->cache === false || isset(self::$overriddenDefaults['cacheFile']) === true) {
                            break;
                        }
                        // Turn caching on.
                        $this->cache = true;
                        self::$overriddenDefaults['cache'] = true;
                        $this->cacheFile = Common::realpath(substr($arg, 6));
                        // It may not exist and return false instead.
                        if ($this->cacheFile === false) {
                            $this->cacheFile = substr($arg, 6);
                            $dir = dirname($this->cacheFile);
                            if (is_dir($dir) === false) {
                                $error = 'ERROR: The specified cache file path "' . $this->cacheFile . '" points to a non-existent directory' . PHP_EOL . PHP_EOL;
                                $error .= $this->printShortUsage(true);
                                throw new DeepExitException($error, 3);
                            }
                            if ($dir === '.') {
                                // Passed cache file is a file in the current directory.
                                $this->cacheFile = getcwd() . '/' . basename($this->cacheFile);
                            }
                            else {
                                if ($dir[0] === '/') {
                                    // An absolute path.
                                    $dir = Common::realpath($dir);
                                }
                                else {
                                    $dir = Common::realpath(getcwd() . '/' . $dir);
                                }
                                if ($dir !== false) {
                                    // Cache file path is relative.
                                    $this->cacheFile = $dir . '/' . basename($this->cacheFile);
                                }
                            }
                        }
                        
                        //end if
                        self::$overriddenDefaults['cacheFile'] = true;
                        if (is_dir($this->cacheFile) === true) {
                            $error = 'ERROR: The specified cache file path "' . $this->cacheFile . '" is a directory' . PHP_EOL . PHP_EOL;
                            $error .= $this->printShortUsage(true);
                            throw new DeepExitException($error, 3);
                        }
                    }
                    else {
                        if (substr($arg, 0, 10) === 'bootstrap=') {
                            $files = explode(',', substr($arg, 10));
                            $bootstrap = [];
                            foreach ($files as $file) {
                                $path = Common::realpath($file);
                                if ($path === false) {
                                    $error = 'ERROR: The specified bootstrap file "' . $file . '" does not exist' . PHP_EOL . PHP_EOL;
                                    $error .= $this->printShortUsage(true);
                                    throw new DeepExitException($error, 3);
                                }
                                $bootstrap[] = $path;
                            }
                            $this->bootstrap = array_merge($this->bootstrap, $bootstrap);
                            self::$overriddenDefaults['bootstrap'] = true;
                        }
                        else {
                            if (substr($arg, 0, 10) === 'file-list=') {
                                $fileList = substr($arg, 10);
                                $path = Common::realpath($fileList);
                                if ($path === false) {
                                    $error = 'ERROR: The specified file list "' . $fileList . '" does not exist' . PHP_EOL . PHP_EOL;
                                    $error .= $this->printShortUsage(true);
                                    throw new DeepExitException($error, 3);
                                }
                                $files = file($path);
                                foreach ($files as $inputFile) {
                                    $inputFile = trim($inputFile);
                                    // Skip empty lines.
                                    if ($inputFile === '') {
                                        continue;
                                    }
                                    $this->processFilePath($inputFile);
                                }
                            }
                            else {
                                if (substr($arg, 0, 11) === 'stdin-path=') {
                                    if (isset(self::$overriddenDefaults['stdinPath']) === true) {
                                        break;
                                    }
                                    $this->stdinPath = Common::realpath(substr($arg, 11));
                                    // It may not exist and return false instead, so use whatever they gave us.
                                    if ($this->stdinPath === false) {
                                        $this->stdinPath = trim(substr($arg, 11));
                                    }
                                    self::$overriddenDefaults['stdinPath'] = true;
                                }
                                else {
                                    if (PHP_CODESNIFFER_CBF === false && substr($arg, 0, 12) === 'report-file=') {
                                        if (isset(self::$overriddenDefaults['reportFile']) === true) {
                                            break;
                                        }
                                        $this->reportFile = Common::realpath(substr($arg, 12));
                                        // It may not exist and return false instead.
                                        if ($this->reportFile === false) {
                                            $this->reportFile = substr($arg, 12);
                                            $dir = Common::realpath(dirname($this->reportFile));
                                            if (is_dir($dir) === false) {
                                                $error = 'ERROR: The specified report file path "' . $this->reportFile . '" points to a non-existent directory' . PHP_EOL . PHP_EOL;
                                                $error .= $this->printShortUsage(true);
                                                throw new DeepExitException($error, 3);
                                            }
                                            $this->reportFile = $dir . '/' . basename($this->reportFile);
                                        }
                                        
                                        //end if
                                        self::$overriddenDefaults['reportFile'] = true;
                                        if (is_dir($this->reportFile) === true) {
                                            $error = 'ERROR: The specified report file path "' . $this->reportFile . '" is a directory' . PHP_EOL . PHP_EOL;
                                            $error .= $this->printShortUsage(true);
                                            throw new DeepExitException($error, 3);
                                        }
                                    }
                                    else {
                                        if (substr($arg, 0, 13) === 'report-width=') {
                                            if (isset(self::$overriddenDefaults['reportWidth']) === true) {
                                                break;
                                            }
                                            $this->reportWidth = substr($arg, 13);
                                            self::$overriddenDefaults['reportWidth'] = true;
                                        }
                                        else {
                                            if (substr($arg, 0, 9) === 'basepath=') {
                                                if (isset(self::$overriddenDefaults['basepath']) === true) {
                                                    break;
                                                }
                                                self::$overriddenDefaults['basepath'] = true;
                                                if (substr($arg, 9) === '') {
                                                    $this->basepath = null;
                                                    break;
                                                }
                                                $this->basepath = Common::realpath(substr($arg, 9));
                                                // It may not exist and return false instead.
                                                if ($this->basepath === false) {
                                                    $this->basepath = substr($arg, 9);
                                                }
                                                if (is_dir($this->basepath) === false) {
                                                    $error = 'ERROR: The specified basepath "' . $this->basepath . '" points to a non-existent directory' . PHP_EOL . PHP_EOL;
                                                    $error .= $this->printShortUsage(true);
                                                    throw new DeepExitException($error, 3);
                                                }
                                            }
                                            else {
                                                if (substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-') {
                                                    $reports = [];
                                                    if ($arg[6] === '-') {
                                                        // This is a report with file output.
                                                        $split = strpos($arg, '=');
                                                        if ($split === false) {
                                                            $report = substr($arg, 7);
                                                            $output = null;
                                                        }
                                                        else {
                                                            $report = substr($arg, 7, $split - 7);
                                                            $output = substr($arg, $split + 1);
                                                            if ($output === false) {
                                                                $output = null;
                                                            }
                                                            else {
                                                                $dir = Common::realpath(dirname($output));
                                                                if (is_dir($dir) === false) {
                                                                    $error = 'ERROR: The specified ' . $report . ' report file path "' . $output . '" points to a non-existent directory' . PHP_EOL . PHP_EOL;
                                                                    $error .= $this->printShortUsage(true);
                                                                    throw new DeepExitException($error, 3);
                                                                }
                                                                $output = $dir . '/' . basename($output);
                                                                if (is_dir($output) === true) {
                                                                    $error = 'ERROR: The specified ' . $report . ' report file path "' . $output . '" is a directory' . PHP_EOL . PHP_EOL;
                                                                    $error .= $this->printShortUsage(true);
                                                                    throw new DeepExitException($error, 3);
                                                                }
                                                            }
                                                            
                                                            //end if
                                                        }
                                                        
                                                        //end if
                                                        $reports[$report] = $output;
                                                    }
                                                    else {
                                                        // This is a single report.
                                                        if (isset(self::$overriddenDefaults['reports']) === true) {
                                                            break;
                                                        }
                                                        $reportNames = explode(',', substr($arg, 7));
                                                        foreach ($reportNames as $report) {
                                                            $reports[$report] = null;
                                                        }
                                                    }
                                                    
                                                    //end if
                                                    // Remove the default value so the CLI value overrides it.
                                                    if (isset(self::$overriddenDefaults['reports']) === false) {
                                                        $this->reports = $reports;
                                                    }
                                                    else {
                                                        $this->reports = array_merge($this->reports, $reports);
                                                    }
                                                    self::$overriddenDefaults['reports'] = true;
                                                }
                                                else {
                                                    if (substr($arg, 0, 7) === 'filter=') {
                                                        if (isset(self::$overriddenDefaults['filter']) === true) {
                                                            break;
                                                        }
                                                        $this->filter = substr($arg, 7);
                                                        self::$overriddenDefaults['filter'] = true;
                                                    }
                                                    else {
                                                        if (substr($arg, 0, 9) === 'standard=') {
                                                            $standards = trim(substr($arg, 9));
                                                            if ($standards !== '') {
                                                                $this->standards = explode(',', $standards);
                                                            }
                                                            self::$overriddenDefaults['standards'] = true;
                                                        }
                                                        else {
                                                            if (substr($arg, 0, 11) === 'extensions=') {
                                                                if (isset(self::$overriddenDefaults['extensions']) === true) {
                                                                    break;
                                                                }
                                                                $extensions = explode(',', substr($arg, 11));
                                                                $newExtensions = [];
                                                                foreach ($extensions as $ext) {
                                                                    $slash = strpos($ext, '/');
                                                                    if ($slash !== false) {
                                                                        // They specified the tokenizer too.
                                                                        list($ext, $tokenizer) = explode('/', $ext);
                                                                        $newExtensions[$ext] = strtoupper($tokenizer);
                                                                        continue;
                                                                    }
                                                                    if (isset($this->extensions[$ext]) === true) {
                                                                        $newExtensions[$ext] = $this->extensions[$ext];
                                                                    }
                                                                    else {
                                                                        $newExtensions[$ext] = 'PHP';
                                                                    }
                                                                }
                                                                $this->extensions = $newExtensions;
                                                                self::$overriddenDefaults['extensions'] = true;
                                                            }
                                                            else {
                                                                if (substr($arg, 0, 7) === 'suffix=') {
                                                                    if (isset(self::$overriddenDefaults['suffix']) === true) {
                                                                        break;
                                                                    }
                                                                    $this->suffix = substr($arg, 7);
                                                                    self::$overriddenDefaults['suffix'] = true;
                                                                }
                                                                else {
                                                                    if (substr($arg, 0, 9) === 'parallel=') {
                                                                        if (isset(self::$overriddenDefaults['parallel']) === true) {
                                                                            break;
                                                                        }
                                                                        $this->parallel = max((int) substr($arg, 9), 1);
                                                                        self::$overriddenDefaults['parallel'] = true;
                                                                    }
                                                                    else {
                                                                        if (substr($arg, 0, 9) === 'severity=') {
                                                                            $this->errorSeverity = (int) substr($arg, 9);
                                                                            $this->warningSeverity = $this->errorSeverity;
                                                                            if (isset(self::$overriddenDefaults['errorSeverity']) === false) {
                                                                                self::$overriddenDefaults['errorSeverity'] = true;
                                                                            }
                                                                            if (isset(self::$overriddenDefaults['warningSeverity']) === false) {
                                                                                self::$overriddenDefaults['warningSeverity'] = true;
                                                                            }
                                                                        }
                                                                        else {
                                                                            if (substr($arg, 0, 15) === 'error-severity=') {
                                                                                if (isset(self::$overriddenDefaults['errorSeverity']) === true) {
                                                                                    break;
                                                                                }
                                                                                $this->errorSeverity = (int) substr($arg, 15);
                                                                                self::$overriddenDefaults['errorSeverity'] = true;
                                                                            }
                                                                            else {
                                                                                if (substr($arg, 0, 17) === 'warning-severity=') {
                                                                                    if (isset(self::$overriddenDefaults['warningSeverity']) === true) {
                                                                                        break;
                                                                                    }
                                                                                    $this->warningSeverity = (int) substr($arg, 17);
                                                                                    self::$overriddenDefaults['warningSeverity'] = true;
                                                                                }
                                                                                else {
                                                                                    if (substr($arg, 0, 7) === 'ignore=') {
                                                                                        if (isset(self::$overriddenDefaults['ignored']) === true) {
                                                                                            break;
                                                                                        }
                                                                                        // Split the ignore string on commas, unless the comma is escaped
                                                                                        // using 1 or 3 slashes (\, or \\\,).
                                                                                        $patterns = preg_split('/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', substr($arg, 7));
                                                                                        $ignored = [];
                                                                                        foreach ($patterns as $pattern) {
                                                                                            $pattern = trim($pattern);
                                                                                            if ($pattern === '') {
                                                                                                continue;
                                                                                            }
                                                                                            $ignored[$pattern] = 'absolute';
                                                                                        }
                                                                                        $this->ignored = $ignored;
                                                                                        self::$overriddenDefaults['ignored'] = true;
                                                                                    }
                                                                                    else {
                                                                                        if (substr($arg, 0, 10) === 'generator=' && PHP_CODESNIFFER_CBF === false) {
                                                                                            if (isset(self::$overriddenDefaults['generator']) === true) {
                                                                                                break;
                                                                                            }
                                                                                            $this->generator = substr($arg, 10);
                                                                                            self::$overriddenDefaults['generator'] = true;
                                                                                        }
                                                                                        else {
                                                                                            if (substr($arg, 0, 9) === 'encoding=') {
                                                                                                if (isset(self::$overriddenDefaults['encoding']) === true) {
                                                                                                    break;
                                                                                                }
                                                                                                $this->encoding = strtolower(substr($arg, 9));
                                                                                                self::$overriddenDefaults['encoding'] = true;
                                                                                            }
                                                                                            else {
                                                                                                if (substr($arg, 0, 10) === 'tab-width=') {
                                                                                                    if (isset(self::$overriddenDefaults['tabWidth']) === true) {
                                                                                                        break;
                                                                                                    }
                                                                                                    $this->tabWidth = (int) substr($arg, 10);
                                                                                                    self::$overriddenDefaults['tabWidth'] = true;
                                                                                                }
                                                                                                else {
                                                                                                    if ($this->dieOnUnknownArg === false) {
                                                                                                        $eqPos = strpos($arg, '=');
                                                                                                        try {
                                                                                                            $unknown = $this->unknown;
                                                                                                            if ($eqPos === false) {
                                                                                                                $unknown[$arg] = $arg;
                                                                                                            }
                                                                                                            else {
                                                                                                                $value = substr($arg, $eqPos + 1);
                                                                                                                $arg = substr($arg, 0, $eqPos);
                                                                                                                $unknown[$arg] = $value;
                                                                                                            }
                                                                                                            $this->unknown = $unknown;
                                                                                                        } catch (RuntimeException $e) {
                                                                                                            // Value is not valid, so just ignore it.
                                                                                                        }
                                                                                                    }
                                                                                                    else {
                                                                                                        $this->processUnknownArgument('--' . $arg, $pos);
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            //end if
            break;
    }
    
    //end switch
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal