PHP_CodeSniffer_CLI::getDefaults PHP Method

getDefaults() public method

Get a list of default values for all possible command line arguments.
public getDefaults ( ) : array
return array
    public function getDefaults()
    {
        if (defined('PHP_CODESNIFFER_IN_TESTS') === true) {
            return array();
        }
        // The default values for config settings.
        $defaults['files'] = array();
        $defaults['standard'] = null;
        $defaults['verbosity'] = 0;
        $defaults['interactive'] = false;
        $defaults['colors'] = false;
        $defaults['explain'] = false;
        $defaults['local'] = false;
        $defaults['showSources'] = false;
        $defaults['extensions'] = array();
        $defaults['sniffs'] = array();
        $defaults['exclude'] = array();
        $defaults['ignored'] = array();
        $defaults['reportFile'] = null;
        $defaults['generator'] = '';
        $defaults['reports'] = array();
        $defaults['bootstrap'] = array();
        $defaults['errorSeverity'] = null;
        $defaults['warningSeverity'] = null;
        $defaults['stdin'] = null;
        $defaults['stdinPath'] = '';
        $reportFormat = PHP_CodeSniffer::getConfigData('report_format');
        if ($reportFormat !== null) {
            $defaults['reports'][$reportFormat] = null;
        }
        $tabWidth = PHP_CodeSniffer::getConfigData('tab_width');
        if ($tabWidth === null) {
            $defaults['tabWidth'] = 0;
        } else {
            $defaults['tabWidth'] = (int) $tabWidth;
        }
        $encoding = PHP_CodeSniffer::getConfigData('encoding');
        if ($encoding === null) {
            $defaults['encoding'] = 'iso-8859-1';
        } else {
            $defaults['encoding'] = strtolower($encoding);
        }
        $severity = PHP_CodeSniffer::getConfigData('severity');
        if ($severity !== null) {
            $defaults['errorSeverity'] = (int) $severity;
            $defaults['warningSeverity'] = (int) $severity;
        }
        $severity = PHP_CodeSniffer::getConfigData('error_severity');
        if ($severity !== null) {
            $defaults['errorSeverity'] = (int) $severity;
        }
        $severity = PHP_CodeSniffer::getConfigData('warning_severity');
        if ($severity !== null) {
            $defaults['warningSeverity'] = (int) $severity;
        }
        $showWarnings = PHP_CodeSniffer::getConfigData('show_warnings');
        if ($showWarnings !== null) {
            $showWarnings = (bool) $showWarnings;
            if ($showWarnings === false) {
                $defaults['warningSeverity'] = 0;
            }
        }
        $reportWidth = PHP_CodeSniffer::getConfigData('report_width');
        if ($reportWidth !== null) {
            $defaults['reportWidth'] = $this->_validateReportWidth($reportWidth);
        } else {
            // Use function defaults.
            $defaults['reportWidth'] = null;
        }
        $showProgress = PHP_CodeSniffer::getConfigData('show_progress');
        if ($showProgress === null) {
            $defaults['showProgress'] = false;
        } else {
            $defaults['showProgress'] = (bool) $showProgress;
        }
        $quiet = PHP_CodeSniffer::getConfigData('quiet');
        if ($quiet === null) {
            $defaults['quiet'] = false;
        } else {
            $defaults['quiet'] = (bool) $quiet;
        }
        $colors = PHP_CodeSniffer::getConfigData('colors');
        if ($colors === null) {
            $defaults['colors'] = false;
        } else {
            $defaults['colors'] = (bool) $colors;
        }
        if (PHP_CodeSniffer::isPharFile(dirname(dirname(__FILE__))) === true) {
            // If this is a phar file, check for the standard in the config.
            $standard = PHP_CodeSniffer::getConfigData('standard');
            if ($standard !== null) {
                $defaults['standard'] = $standard;
            }
        }
        return $defaults;
    }

Usage Example

 /**
  * Run PHPCS on a file.
  *
  * Returns a string representation of the output that would normally
  * be printed to the console. Artifically sets the `-s` (showSources)
  * command line switch to make it possible to parse which rules failed
  * for a given sample file.
  *
  * @param string $file to run.
  * @return string The output from phpcs.
  */
 public function runPhpCs($file)
 {
     $defaults = $this->_phpcs->getDefaults();
     $standard = $this->_rootDir . '/ruleset.xml';
     if (defined('PHP_CodeSniffer::VERSION') && version_compare(PHP_CodeSniffer::VERSION, '1.5.0') != -1) {
         $standard = [$standard];
     }
     $options = ['encoding' => 'utf-8', 'files' => [$file], 'standard' => $standard, 'showSources' => true] + $defaults;
     // New PHPCS has a strange issue where the method arguments
     // are not stored on the instance causing weird errors.
     $reflection = new ReflectionProperty($this->_phpcs, 'values');
     $reflection->setAccessible(true);
     $reflection->setValue($this->_phpcs, $options);
     ob_start();
     $this->_phpcs->process($options);
     $result = ob_get_contents();
     ob_end_clean();
     return $result;
 }
All Usage Examples Of PHP_CodeSniffer_CLI::getDefaults