PHP_CodeSniffer_CLI::processLongArgument PHP Method

processLongArgument() public method

Processes a long (--example) command line argument.
public processLongArgument ( string $arg, integer $pos ) : void
$arg string The command line argument.
$pos integer The position of the argument on the command line.
return void
    public function processLongArgument($arg, $pos)
    {
        switch ($arg) {
            case 'help':
                $this->printUsage();
                exit(0);
            case 'version':
                echo 'PHP_CodeSniffer version ' . PHP_CodeSniffer::VERSION . ' (' . PHP_CodeSniffer::STABILITY . ') ';
                echo 'by Squiz (http://www.squiz.net)' . PHP_EOL;
                exit(0);
            case 'colors':
                $this->values['colors'] = true;
                break;
            case 'no-colors':
                $this->values['colors'] = false;
                break;
            case 'config-set':
                if (isset($this->_cliArgs[$pos + 1]) === false || isset($this->_cliArgs[$pos + 2]) === false) {
                    echo 'ERROR: Setting a config option requires a name and value' . PHP_EOL . PHP_EOL;
                    $this->printUsage();
                    exit(0);
                }
                $key = $this->_cliArgs[$pos + 1];
                $value = $this->_cliArgs[$pos + 2];
                $current = PHP_CodeSniffer::getConfigData($key);
                try {
                    PHP_CodeSniffer::setConfigData($key, $value);
                } catch (Exception $e) {
                    echo $e->getMessage() . PHP_EOL;
                    exit(2);
                }
                if ($current === null) {
                    echo "Config value \"{$key}\" added successfully" . PHP_EOL;
                } else {
                    echo "Config value \"{$key}\" updated successfully; old value was \"{$current}\"" . PHP_EOL;
                }
                exit(0);
            case 'config-delete':
                if (isset($this->_cliArgs[$pos + 1]) === false) {
                    echo 'ERROR: Deleting a config option requires the name of the option' . PHP_EOL . PHP_EOL;
                    $this->printUsage();
                    exit(0);
                }
                $key = $this->_cliArgs[$pos + 1];
                $current = PHP_CodeSniffer::getConfigData($key);
                if ($current === null) {
                    echo "Config value \"{$key}\" has not been set" . PHP_EOL;
                } else {
                    try {
                        PHP_CodeSniffer::setConfigData($key, null);
                    } catch (Exception $e) {
                        echo $e->getMessage() . PHP_EOL;
                        exit(2);
                    }
                    echo "Config value \"{$key}\" removed successfully; old value was \"{$current}\"" . PHP_EOL;
                }
                exit(0);
            case 'config-show':
                $data = PHP_CodeSniffer::getAllConfigData();
                $this->printConfigData($data);
                exit(0);
            case 'runtime-set':
                if (isset($this->_cliArgs[$pos + 1]) === false || isset($this->_cliArgs[$pos + 2]) === false) {
                    echo 'ERROR: Setting a runtime config option requires a name and value' . PHP_EOL . PHP_EOL;
                    $this->printUsage();
                    exit(0);
                }
                $key = $this->_cliArgs[$pos + 1];
                $value = $this->_cliArgs[$pos + 2];
                $this->_cliArgs[$pos + 1] = '';
                $this->_cliArgs[$pos + 2] = '';
                PHP_CodeSniffer::setConfigData($key, $value, true);
                break;
            default:
                if (substr($arg, 0, 7) === 'sniffs=') {
                    $sniffs = explode(',', substr($arg, 7));
                    foreach ($sniffs as $sniff) {
                        if (substr_count($sniff, '.') !== 2) {
                            echo 'ERROR: The specified sniff code "' . $sniff . '" is invalid' . PHP_EOL . PHP_EOL;
                            $this->printUsage();
                            exit(2);
                        }
                    }
                    $this->values['sniffs'] = $sniffs;
                } else {
                    if (substr($arg, 0, 8) === 'exclude=') {
                        $sniffs = explode(',', substr($arg, 8));
                        foreach ($sniffs as $sniff) {
                            if (substr_count($sniff, '.') !== 2) {
                                echo 'ERROR: The specified sniff code "' . $sniff . '" is invalid' . PHP_EOL . PHP_EOL;
                                $this->printUsage();
                                exit(2);
                            }
                        }
                        $this->values['exclude'] = $sniffs;
                    } else {
                        if (substr($arg, 0, 10) === 'bootstrap=') {
                            $files = explode(',', substr($arg, 10));
                            foreach ($files as $file) {
                                $path = PHP_CodeSniffer::realpath($file);
                                if ($path === false) {
                                    echo 'ERROR: The specified bootstrap file "' . $file . '" does not exist' . PHP_EOL . PHP_EOL;
                                    $this->printUsage();
                                    exit(2);
                                }
                                $this->values['bootstrap'][] = $path;
                            }
                        } else {
                            if (substr($arg, 0, 10) === 'file-list=') {
                                $fileList = substr($arg, 10);
                                $path = PHP_CodeSniffer::realpath($fileList);
                                if ($path === false) {
                                    echo 'ERROR: The specified file list "' . $file . '" does not exist' . PHP_EOL . PHP_EOL;
                                    $this->printUsage();
                                    exit(2);
                                }
                                $files = file($path);
                                foreach ($files as $inputFile) {
                                    $inputFile = trim($inputFile);
                                    // Skip empty lines.
                                    if ($inputFile === '') {
                                        continue;
                                    }
                                    $realFile = PHP_CodeSniffer::realpath($inputFile);
                                    if ($realFile === false) {
                                        echo 'ERROR: The specified file "' . $inputFile . '" does not exist' . PHP_EOL . PHP_EOL;
                                        $this->printUsage();
                                        exit(2);
                                    }
                                    $this->values['files'][] = $realFile;
                                }
                            } else {
                                if (substr($arg, 0, 11) === 'stdin-path=') {
                                    $this->values['stdinPath'] = PHP_CodeSniffer::realpath(substr($arg, 11));
                                    // It may not exist and return false instead, so just use whatever they gave us.
                                    if ($this->values['stdinPath'] === false) {
                                        $this->values['stdinPath'] = trim(substr($arg, 11));
                                    }
                                } else {
                                    if (substr($arg, 0, 12) === 'report-file=') {
                                        $this->values['reportFile'] = PHP_CodeSniffer::realpath(substr($arg, 12));
                                        // It may not exist and return false instead.
                                        if ($this->values['reportFile'] === false) {
                                            $this->values['reportFile'] = substr($arg, 12);
                                            $dir = dirname($this->values['reportFile']);
                                            if (is_dir($dir) === false) {
                                                echo 'ERROR: The specified report file path "' . $this->values['reportFile'] . '" points to a non-existent directory' . PHP_EOL . PHP_EOL;
                                                $this->printUsage();
                                                exit(2);
                                            }
                                            if ($dir === '.') {
                                                // Passed report file is a file in the current directory.
                                                $this->values['reportFile'] = getcwd() . '/' . basename($this->values['reportFile']);
                                            } else {
                                                if ($dir[0] === '/') {
                                                    // An absolute path.
                                                    $dir = PHP_CodeSniffer::realpath($dir);
                                                } else {
                                                    $dir = PHP_CodeSniffer::realpath(getcwd() . '/' . $dir);
                                                }
                                                if ($dir !== false) {
                                                    // Report file path is relative.
                                                    $this->values['reportFile'] = $dir . '/' . basename($this->values['reportFile']);
                                                }
                                            }
                                        }
                                        //end if
                                        if (is_dir($this->values['reportFile']) === true) {
                                            echo 'ERROR: The specified report file path "' . $this->values['reportFile'] . '" is a directory' . PHP_EOL . PHP_EOL;
                                            $this->printUsage();
                                            exit(2);
                                        }
                                    } else {
                                        if (substr($arg, 0, 13) === 'report-width=') {
                                            $this->values['reportWidth'] = $this->_validateReportWidth(substr($arg, 13));
                                        } else {
                                            if (substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-') {
                                                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 = dirname($output);
                                                            if ($dir === '.') {
                                                                // Passed report file is a filename in the current directory.
                                                                $output = getcwd() . '/' . basename($output);
                                                            } else {
                                                                if ($dir[0] === '/') {
                                                                    // An absolute path.
                                                                    $dir = PHP_CodeSniffer::realpath($dir);
                                                                } else {
                                                                    $dir = PHP_CodeSniffer::realpath(getcwd() . '/' . $dir);
                                                                }
                                                                if ($dir !== false) {
                                                                    // Report file path is relative.
                                                                    $output = $dir . '/' . basename($output);
                                                                }
                                                            }
                                                        }
                                                        //end if
                                                    }
                                                    //end if
                                                } else {
                                                    // This is a single report.
                                                    $report = substr($arg, 7);
                                                    $output = null;
                                                }
                                                //end if
                                                $this->values['reports'][$report] = $output;
                                            } else {
                                                if (substr($arg, 0, 9) === 'standard=') {
                                                    $standards = trim(substr($arg, 9));
                                                    if ($standards !== '') {
                                                        $this->values['standard'] = explode(',', $standards);
                                                    }
                                                } else {
                                                    if (substr($arg, 0, 11) === 'extensions=') {
                                                        if (isset($this->values['extensions']) === false) {
                                                            $this->values['extensions'] = array();
                                                        }
                                                        $this->values['extensions'] = array_merge($this->values['extensions'], explode(',', substr($arg, 11)));
                                                    } else {
                                                        if (substr($arg, 0, 9) === 'severity=') {
                                                            $this->values['errorSeverity'] = (int) substr($arg, 9);
                                                            $this->values['warningSeverity'] = $this->values['errorSeverity'];
                                                        } else {
                                                            if (substr($arg, 0, 15) === 'error-severity=') {
                                                                $this->values['errorSeverity'] = (int) substr($arg, 15);
                                                            } else {
                                                                if (substr($arg, 0, 17) === 'warning-severity=') {
                                                                    $this->values['warningSeverity'] = (int) substr($arg, 17);
                                                                } else {
                                                                    if (substr($arg, 0, 7) === 'ignore=') {
                                                                        // Split the ignore string on commas, unless the comma is escaped
                                                                        // using 1 or 3 slashes (\, or \\\,).
                                                                        $ignored = preg_split('/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', substr($arg, 7));
                                                                        foreach ($ignored as $pattern) {
                                                                            $pattern = trim($pattern);
                                                                            if ($pattern === '') {
                                                                                continue;
                                                                            }
                                                                            $this->values['ignored'][$pattern] = 'absolute';
                                                                        }
                                                                    } else {
                                                                        if (substr($arg, 0, 10) === 'generator=') {
                                                                            $this->values['generator'] = substr($arg, 10);
                                                                        } else {
                                                                            if (substr($arg, 0, 9) === 'encoding=') {
                                                                                $this->values['encoding'] = strtolower(substr($arg, 9));
                                                                            } else {
                                                                                if (substr($arg, 0, 10) === 'tab-width=') {
                                                                                    $this->values['tabWidth'] = (int) substr($arg, 10);
                                                                                } else {
                                                                                    if ($this->dieOnUnknownArg === false) {
                                                                                        $eqPos = strpos($arg, '=');
                                                                                        if ($eqPos === false) {
                                                                                            $this->values[$arg] = $arg;
                                                                                        } else {
                                                                                            $value = substr($arg, $eqPos + 1);
                                                                                            $arg = substr($arg, 0, $eqPos);
                                                                                            $this->values[$arg] = $value;
                                                                                        }
                                                                                    } else {
                                                                                        $this->processUnknownArgument('--' . $arg, $pos);
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //end if
                break;
        }
        //end switch
    }

Usage Example

 public function processLongArgument($arg, $pos)
 {
     if (substr($arg, 0, 10) === 'namespace=') {
         $this->namespace = substr($arg, 10);
         return;
     }
     parent::processLongArgument($arg, $pos);
 }
All Usage Examples Of PHP_CodeSniffer_CLI::processLongArgument