PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file Config.php

Documentation is available at Config.php

  1. <?php
  2. /**
  3.  * Stores the configuration used to run PHPCS and PHPCBF.
  4.  *
  5.  * Parses the command line to determine user supplied values
  6.  * and provides functions to access data stored in config files.
  7.  *
  8.  * @author    Greg Sherwood <gsherwood@squiz.net>
  9.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  10.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  11.  */
  12.  
  13. namespace PHP_CodeSniffer;
  14.  
  15. use PHP_CodeSniffer\Exceptions\RuntimeException;
  16.  
  17. class Config
  18. {
  19.  
  20.     /**
  21.      * The current version.
  22.      *
  23.      * @var string 
  24.      */
  25.     const VERSION = '3.0.0';
  26.  
  27.     /**
  28.      * Package stability; either stable, beta or alpha.
  29.      *
  30.      * @var string 
  31.      */
  32.     const STABILITY = 'stable';
  33.  
  34.     /**
  35.      * An array of settings that PHPCS and PHPCBF accept.
  36.      *
  37.      * This array is not meant to be accessed directly. Instead, use the settings
  38.      * as if they are class member vars so the __get() and __set() magic methods
  39.      * can be used to validate the values. For example, to set the verbosity level to
  40.      * level 2, use $this->verbosity = 2; instead of accessing this property directly.
  41.      *
  42.      * The list of settings are:
  43.      *
  44.      * string[] files           The files and directories to check.
  45.      * string[] standards       The standards being used for checking.
  46.      * int      verbosity       How verbose the output should be.
  47.      *                          0: no unnecessary output
  48.      *                          1: basic output for files being checked
  49.      *                          2: ruleset and file parsing output
  50.      *                          3: sniff execution output
  51.      * bool     interactive     Enable interactive checking mode.
  52.      * bool     parallel        Check files in parallel.
  53.      * bool     cache           Enable the use of the file cache.
  54.      * bool     cacheFile       A file where the cache data should be written
  55.      * bool     colors          Display colours in output.
  56.      * bool     explain         Explain the coding standards.
  57.      * bool     local           Process local files in directories only (no recursion).
  58.      * bool     showSources     Show sniff source codes in report output.
  59.      * bool     showProgress    Show basic progress information while running.
  60.      * bool     quiet           Quiet mode; disables progress and verbose output.
  61.      * bool     annotations     Process @codingStandard annotations.
  62.      * int      tabWidth        How many spaces each tab is worth.
  63.      * string   encoding        The encoding of the files being checked.
  64.      * string[] sniffs          The sniffs that should be used for checking.
  65.      *                          If empty, all sniffs in the supplied standards will be used.
  66.      * string[] exclude         The sniffs that should be excluded from checking.
  67.      *                          If empty, all sniffs in the supplied standards will be used.
  68.      * string[] ignored         Regular expressions used to ignore files and folders during checking.
  69.      * string   reportFile      A file where the report output should be written.
  70.      * string   generator       The documentation generator to use.
  71.      * string   filter          The filter to use for the run.
  72.      * string[] bootstrap       One of more files to include before the run begins.
  73.      * int      reportWidth     The maximum number of columns that reports should use for output.
  74.      *                          Set to "auto" for have this value changed to the width of the terminal.
  75.      * int      errorSeverity   The minimum severity an error must have to be displayed.
  76.      * int      warningSeverity The minimum severity a warning must have to be displayed.
  77.      * bool     recordErrors    Record the content of error messages as well as error counts.
  78.      * string   suffix          A suffix to add to fixed files.
  79.      * string   basepath        A file system location to strip from the paths of files shown in reports.
  80.      * bool     stdin           Read content from STDIN instead of supplied files.
  81.      * string   stdinContent    Content passed directly to PHPCS on STDIN.
  82.      * string   stdinPath       The path to use for content passed on STDIN.
  83.      *
  84.      * array<string, string>      extensions File extensions that should be checked, and what tokenizer to use.
  85.      *                                       E.g., array('inc' => 'PHP');
  86.      * array<string, string|null> reports    The reports to use for printing output after the run.
  87.      *                                       The format of the array is:
  88.      *                                           array(
  89.      *                                            'reportName1' => 'outputFile',
  90.      *                                            'reportName2' => null,
  91.      *                                           );
  92.      *                                       If the array value is NULL, the report will be written to the screen.
  93.      *
  94.      * @var array<string, mixed>
  95.      */
  96.     private $settings = array(
  97.                          'files'           => null,
  98.                          'standards'       => null,
  99.                          'verbosity'       => null,
  100.                          'interactive'     => null,
  101.                          'parallel'        => null,
  102.                          'cache'           => null,
  103.                          'cacheFile'       => null,
  104.                          'colors'          => null,
  105.                          'explain'         => null,
  106.                          'local'           => null,
  107.                          'showSources'     => null,
  108.                          'showProgress'    => null,
  109.                          'quiet'           => null,
  110.                          'annotations'     => null,
  111.                          'tabWidth'        => null,
  112.                          'encoding'        => null,
  113.                          'extensions'      => null,
  114.                          'sniffs'          => null,
  115.                          'exclude'         => null,
  116.                          'ignored'         => null,
  117.                          'reportFile'      => null,
  118.                          'generator'       => null,
  119.                          'filter'          => null,
  120.                          'bootstrap'       => null,
  121.                          'reports'         => null,
  122.                          'basepath'        => null,
  123.                          'reportWidth'     => null,
  124.                          'errorSeverity'   => null,
  125.                          'warningSeverity' => null,
  126.                          'recordErrors'    => null,
  127.                          'suffix'          => null,
  128.                          'stdin'           => null,
  129.                          'stdinContent'    => null,
  130.                          'stdinPath'       => null,
  131.                         );
  132.  
  133.     /**
  134.      * Whether or not to kill the process when an unknown command line arg is found.
  135.      *
  136.      * If FALSE, arguments that are not command line options or file/directory paths
  137.      * will be ignored and execution will continue.
  138.      *
  139.      * @var boolean 
  140.      */
  141.     public $dieOnUnknownArg;
  142.  
  143.     /**
  144.      * The current command line arguments we are processing.
  145.      *
  146.      * @var string[] 
  147.      */
  148.     private $cliArgs = array();
  149.  
  150.     /**
  151.      * Command line values that the user has supplied directly.
  152.      *
  153.      * @var array<string, TRUE>
  154.      */
  155.     private $overriddenDefaults = array();
  156.  
  157.     /**
  158.      * Config file data that has been loaded for the run.
  159.      *
  160.      * @var array<string, string>
  161.      */
  162.     private static $configData = null;
  163.  
  164.     /**
  165.      * Automatically discovered executable utility paths.
  166.      *
  167.      * @var array<string, string>
  168.      */
  169.     private static $executablePaths = array();
  170.  
  171.  
  172.     /**
  173.      * Get the value of an inaccessible property.
  174.      *
  175.      * @param string $name The name of the property.
  176.      *
  177.      * @return mixed 
  178.      * @throws RuntimeException If the setting name is invalid.
  179.      */
  180.     public function __get($name)
  181.     {
  182.         if (array_key_exists($name$this->settings=== false{
  183.             throw new RuntimeException("ERROR: unable to get value of property \"$name\"");
  184.         }
  185.  
  186.         return $this->settings[$name];
  187.  
  188.     }//end __get()
  189.  
  190.  
  191.     /**
  192.      * Set the value of an inaccessible property.
  193.      *
  194.      * @param string $name  The name of the property.
  195.      * @param mixed  $value The value of the property.
  196.      *
  197.      * @return void 
  198.      * @throws RuntimeException If the setting name is invalid.
  199.      */
  200.     public function __set($name$value)
  201.     {
  202.         if (array_key_exists($name$this->settings=== false{
  203.             throw new RuntimeException("Can't __set() $name; setting doesn't exist");
  204.         }
  205.  
  206.         switch ($name{
  207.         case 'reportWidth' :
  208.             // Support auto terminal width.
  209.             if ($value === 'auto' && preg_match('|\d+ (\d+)|'shell_exec('stty size 2>&1')$matches=== 1{
  210.                 $value = (int) $matches[1];
  211.             else {
  212.                 $value = (int) $value;
  213.             }
  214.             break;
  215.         case 'standards' :
  216.             $cleaned = array();
  217.  
  218.             // Check if the standard name is valid, or if the case is invalid.
  219.             $installedStandards = Util\Standards::getInstalledStandards();
  220.             foreach ($value as $standard{
  221.                 foreach ($installedStandards as $validStandard{
  222.                     if (strtolower($standard=== strtolower($validStandard)) {
  223.                         $standard $validStandard;
  224.                         break;
  225.                     }
  226.                 }
  227.  
  228.                 $cleaned[$standard;
  229.             }
  230.  
  231.             $value $cleaned;
  232.             break;
  233.         default :
  234.             // No validation required.
  235.             break;
  236.         }//end switch
  237.  
  238.         $this->settings[$name$value;
  239.  
  240.     }//end __set()
  241.  
  242.  
  243.     /**
  244.      * Check if the value of an inaccessible property is set.
  245.      *
  246.      * @param string $name The name of the property.
  247.      *
  248.      * @return bool 
  249.      */
  250.     public function __isset($name)
  251.     {
  252.         return isset($this->settings[$name]);
  253.  
  254.     }//end __isset()
  255.  
  256.  
  257.     /**
  258.      * Unset the value of an inaccessible property.
  259.      *
  260.      * @param string $name The name of the property.
  261.      *
  262.      * @return void 
  263.      */
  264.     public function __unset($name)
  265.     {
  266.         $this->settings[$name= null;
  267.  
  268.     }//end __unset()
  269.  
  270.  
  271.     /**
  272.      * Get the array of all config settings.
  273.      *
  274.      * @return array<string, mixed>
  275.      */
  276.     public function getSettings()
  277.     {
  278.         return $this->settings;
  279.  
  280.     }//end getSettings()
  281.  
  282.  
  283.     /**
  284.      * Set the array of all config settings.
  285.      *
  286.      * @param array<string, mixed> $settings The array of config settings.
  287.      *
  288.      * @return void 
  289.      */
  290.     public function setSettings($settings)
  291.     {
  292.         return $this->settings $settings;
  293.  
  294.     }//end setSettings()
  295.  
  296.  
  297.     /**
  298.      * Creates a Config object and populates it with command line values.
  299.      *
  300.      * @param array $cliArgs         An array of values gathered from CLI args.
  301.      * @param bool  $dieOnUnknownArg Whether or not to kill the process when an
  302.      *                                unknown command line arg is found.
  303.      *
  304.      * @return void 
  305.      */
  306.     public function __construct(array $cliArgs=array()$dieOnUnknownArg=true)
  307.     {
  308.         if (defined('PHP_CODESNIFFER_IN_TESTS'=== true{
  309.             // Let everything through during testing so that we can
  310.             // make use of PHPUnit command line arguments as well.
  311.             $this->dieOnUnknownArg = false;
  312.         else {
  313.             $this->dieOnUnknownArg $dieOnUnknownArg;
  314.         }
  315.  
  316.         $checkStdin = false;
  317.         if (empty($cliArgs=== true{
  318.             $cliArgs $_SERVER['argv'];
  319.             array_shift($cliArgs);
  320.             $checkStdin = true;
  321.         }
  322.  
  323.         $this->restoreDefaults();
  324.         $this->setCommandLineValues($cliArgs);
  325.  
  326.         if (isset($this->overriddenDefaults['standards']=== false
  327.             && Config::getConfigData('default_standard'=== null
  328.         {
  329.             // They did not supply a standard to use.
  330.             // Look for a default ruleset in the current directory or higher.
  331.             $currentDir getcwd();
  332.  
  333.             do {
  334.                 $default $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml';
  335.                 if (is_file($default=== true{
  336.                     $this->standards = array($default);
  337.                 else {
  338.                     $default $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml.dist';
  339.                     if (is_file($default=== true{
  340.                         $this->standards = array($default);
  341.                     }
  342.                 }
  343.  
  344.                 $lastDir    $currentDir;
  345.                 $currentDir dirname($currentDir);
  346.             while ($currentDir !== '.' && $currentDir !== $lastDir);
  347.         }
  348.  
  349.         // Check for content on STDIN.
  350.         if ($checkStdin === true{
  351.             $handle fopen('php://stdin''r');
  352.             if (stream_set_blocking($handlefalse=== true{
  353.                 $fileContents '';
  354.                 while (($line fgets($handle)) !== false{
  355.                     $fileContents .= $line;
  356.                     usleep(10);
  357.                 }
  358.  
  359.                 stream_set_blocking($handletrue);
  360.                 fclose($handle);
  361.                 if (trim($fileContents!== ''{
  362.                     $this->stdin        = true;
  363.                     $this->stdinContent $fileContents;
  364.                     $this->overriddenDefaults['stdin']        = true;
  365.                     $this->overriddenDefaults['stdinContent'= true;
  366.                 }
  367.             }
  368.         }
  369.  
  370.     }//end __construct()
  371.  
  372.  
  373.     /**
  374.      * Set the command line values.
  375.      *
  376.      * @param array $args An array of command line arguments to set.
  377.      *
  378.      * @return void 
  379.      */
  380.     public function setCommandLineValues($args)
  381.     {
  382.         $this->cliArgs $args;
  383.         $numArgs       count($args);
  384.  
  385.         for ($i = 0; $i $numArgs$i++{
  386.             $arg $this->cliArgs[$i];
  387.             if ($arg === ''{
  388.                 continue;
  389.             }
  390.  
  391.             if ($arg{0=== '-'{
  392.                 if ($arg === '-'{
  393.                     // Asking to read from STDIN.
  394.                     $this->stdin = true;
  395.                     $this->overriddenDefaults['stdin'= true;
  396.                     continue;
  397.                 }
  398.  
  399.                 if ($arg === '--'{
  400.                     // Empty argument, ignore it.
  401.                     continue;
  402.                 }
  403.  
  404.                 if ($arg{1=== '-'{
  405.                     $this->processLongArgument(substr($arg2)$i);
  406.                 else {
  407.                     $switches str_split($arg);
  408.                     foreach ($switches as $switch{
  409.                         if ($switch === '-'{
  410.                             continue;
  411.                         }
  412.  
  413.                         $this->processShortArgument($switch$i);
  414.                     }
  415.                 }
  416.             else {
  417.                 $this->processUnknownArgument($arg$i);
  418.             }//end if
  419.         }//end for
  420.  
  421.     }//end setCommandLineValues()
  422.  
  423.  
  424.     /**
  425.      * Restore default values for all possible command line arguments.
  426.      *
  427.      * @return array 
  428.      */
  429.     public function restoreDefaults()
  430.     {
  431.         $this->files           = array();
  432.         $this->standards       = array('PEAR');
  433.         $this->verbosity       = 0;
  434.         $this->interactive     = false;
  435.         $this->cache           = false;
  436.         $this->cacheFile       = null;
  437.         $this->colors          = false;
  438.         $this->explain         = false;
  439.         $this->local           = false;
  440.         $this->showSources     = false;
  441.         $this->showProgress    = false;
  442.         $this->quiet           = false;
  443.         $this->annotations     = true;
  444.         $this->parallel        = 1;
  445.         $this->tabWidth        = 0;
  446.         $this->encoding        'utf-8';
  447.         $this->extensions      = array(
  448.                                   'php' => 'PHP',
  449.                                   'inc' => 'PHP',
  450.                                   'js'  => 'JS',
  451.                                   'css' => 'CSS',
  452.                                  );
  453.         $this->sniffs          = array();
  454.         $this->exclude         = array();
  455.         $this->ignored         = array();
  456.         $this->reportFile      = null;
  457.         $this->generator       = null;
  458.         $this->filter          = null;
  459.         $this->bootstrap       = array();
  460.         $this->reports         = array('full' => null);
  461.         $this->reportWidth     'auto';
  462.         $this->errorSeverity   = 5;
  463.         $this->warningSeverity = 5;
  464.         $this->recordErrors    = true;
  465.         $this->suffix          '';
  466.         $this->stdin           = false;
  467.         $this->stdinContent    = null;
  468.         $this->stdinPath       = null;
  469.  
  470.         $standard = self::getConfigData('default_standard');
  471.         if ($standard !== null{
  472.             $this->standards explode(','$standard);
  473.         }
  474.  
  475.         $reportFormat = self::getConfigData('report_format');
  476.         if ($reportFormat !== null{
  477.             $this->reports = array($reportFormat => null);
  478.         }
  479.  
  480.         $tabWidth = self::getConfigData('tab_width');
  481.         if ($tabWidth !== null{
  482.             $this->tabWidth = (int) $tabWidth;
  483.         }
  484.  
  485.         $encoding = self::getConfigData('encoding');
  486.         if ($encoding !== null{
  487.             $this->encoding strtolower($encoding);
  488.         }
  489.  
  490.         $severity = self::getConfigData('severity');
  491.         if ($severity !== null{
  492.             $this->errorSeverity   = (int) $severity;
  493.             $this->warningSeverity = (int) $severity;
  494.         }
  495.  
  496.         $severity = self::getConfigData('error_severity');
  497.         if ($severity !== null{
  498.             $this->errorSeverity = (int) $severity;
  499.         }
  500.  
  501.         $severity = self::getConfigData('warning_severity');
  502.         if ($severity !== null{
  503.             $this->warningSeverity = (int) $severity;
  504.         }
  505.  
  506.         $showWarnings = self::getConfigData('show_warnings');
  507.         if ($showWarnings !== null{
  508.             $showWarnings = (bool) $showWarnings;
  509.             if ($showWarnings === false{
  510.                 $this->warningSeverity = 0;
  511.             }
  512.         }
  513.  
  514.         $reportWidth = self::getConfigData('report_width');
  515.         if ($reportWidth !== null{
  516.             $this->reportWidth $reportWidth;
  517.         }
  518.  
  519.         $showProgress = self::getConfigData('show_progress');
  520.         if ($showProgress !== null{
  521.             $this->showProgress = (bool) $showProgress;
  522.         }
  523.  
  524.         $quiet = self::getConfigData('quiet');
  525.         if ($quiet !== null{
  526.             $this->quiet = (bool) $quiet;
  527.         }
  528.  
  529.         $colors = self::getConfigData('colors');
  530.         if ($colors !== null{
  531.             $this->colors = (bool) $colors;
  532.         }
  533.  
  534.         if (defined('PHP_CODESNIFFER_IN_TESTS'=== false{
  535.             $cache = self::getConfigData('cache');
  536.             if ($cache !== null{
  537.                 $this->cache = (bool) $cache;
  538.             }
  539.  
  540.             $parallel = self::getConfigData('parallel');
  541.             if ($parallel !== null{
  542.                 $this->parallel max((int) $parallel1);
  543.             }
  544.         }
  545.  
  546.     }//end restoreDefaults()
  547.  
  548.  
  549.     /**
  550.      * Processes a short (-e) command line argument.
  551.      *
  552.      * @param string $arg The command line argument.
  553.      * @param int    $pos The position of the argument on the command line.
  554.      *
  555.      * @return void 
  556.      */
  557.     public function processShortArgument($arg$pos)
  558.     {
  559.         switch ($arg{
  560.         case 'h':
  561.         case '?':
  562.             $this->printUsage();
  563.             exit(0);
  564.         case 'i' :
  565.             Util\Standards::printInstalledStandards();
  566.             exit(0);
  567.         case 'v' :
  568.             if ($this->quiet === true{
  569.                 // Ignore when quiet mode is enabled.
  570.                 break;
  571.             }
  572.  
  573.             $this->verbosity++;
  574.             $this->overriddenDefaults['verbosity'= true;
  575.             break;
  576.         case 'l' :
  577.             $this->local = true;
  578.             $this->overriddenDefaults['local'= true;
  579.             break;
  580.         case 's' :
  581.             $this->showSources = true;
  582.             $this->overriddenDefaults['showSources'= true;
  583.             break;
  584.         case 'a' :
  585.             $this->interactive = true;
  586.             $this->overriddenDefaults['interactive'= true;
  587.             break;
  588.         case 'e':
  589.             $this->explain = true;
  590.             $this->overriddenDefaults['explain'= true;
  591.             break;
  592.         case 'p' :
  593.             if ($this->quiet === true{
  594.                 // Ignore when quiet mode is enabled.
  595.                 break;
  596.             }
  597.  
  598.             $this->showProgress = true;
  599.             $this->overriddenDefaults['showProgress'= true;
  600.             break;
  601.         case 'q' :
  602.             // Quiet mode disables a few other settings as well.
  603.             $this->quiet        = true;
  604.             $this->showProgress = false;
  605.             $this->verbosity    = 0;
  606.  
  607.             $this->overriddenDefaults['quiet'= true;
  608.             break;
  609.         case 'm' :
  610.             $this->recordErrors = false;
  611.             $this->overriddenDefaults['recordErrors'= true;
  612.             break;
  613.         case 'd' :
  614.             $ini explode('='$this->cliArgs[($pos + 1)]);
  615.             $this->cliArgs[($pos + 1)'';
  616.             if (isset($ini[1]=== true{
  617.                 ini_set($ini[0]$ini[1]);
  618.             else {
  619.                 ini_set($ini[0]true);
  620.             }
  621.             break;
  622.         case 'n' :
  623.             if (isset($this->overriddenDefaults['warningSeverity']=== false{
  624.                 $this->warningSeverity = 0;
  625.                 $this->overriddenDefaults['warningSeverity'= true;
  626.             }
  627.             break;
  628.         case 'w' :
  629.             if (isset($this->overriddenDefaults['warningSeverity']=== false{
  630.                 $this->warningSeverity $this->errorSeverity;
  631.                 $this->overriddenDefaults['warningSeverity'= true;
  632.             }
  633.             break;
  634.         default:
  635.             if ($this->dieOnUnknownArg === false{
  636.                 $this->values[$arg$arg;
  637.             else {
  638.                 $this->processUnknownArgument('-'.$arg$pos);
  639.             }
  640.         }//end switch
  641.  
  642.     }//end processShortArgument()
  643.  
  644.  
  645.     /**
  646.      * Processes a long (--example) command line argument.
  647.      *
  648.      * @param string $arg The command line argument.
  649.      * @param int    $pos The position of the argument on the command line.
  650.      *
  651.      * @return void 
  652.      */
  653.     public function processLongArgument($arg$pos)
  654.     {
  655.         switch ($arg{
  656.         case 'help':
  657.             $this->printUsage();
  658.             exit(0);
  659.         case 'version':
  660.             echo 'PHP_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') ';
  661.             echo 'by Squiz (http://www.squiz.net)'.PHP_EOL;
  662.             exit(0);
  663.         case 'colors':
  664.             if (isset($this->overriddenDefaults['colors']=== true{
  665.                 break;
  666.             }
  667.  
  668.             $this->colors = true;
  669.             $this->overriddenDefaults['colors'= true;
  670.             break;
  671.         case 'no-colors':
  672.             if (isset($this->overriddenDefaults['colors']=== true{
  673.                 break;
  674.             }
  675.  
  676.             $this->colors = false;
  677.             $this->overriddenDefaults['colors'= true;
  678.             break;
  679.         case 'cache':
  680.             if (isset($this->overriddenDefaults['cache']=== true{
  681.                 break;
  682.             }
  683.  
  684.             if (defined('PHP_CODESNIFFER_IN_TESTS'=== false{
  685.                 $this->cache = true;
  686.                 $this->overriddenDefaults['cache'= true;
  687.             }
  688.             break;
  689.         case 'no-cache':
  690.             if (isset($this->overriddenDefaults['cache']=== true{
  691.                 break;
  692.             }
  693.  
  694.             $this->cache = false;
  695.             $this->overriddenDefaults['cache'= true;
  696.             break;
  697.         case 'ignore-annotations':
  698.             if (isset($this->overriddenDefaults['annotations']=== true{
  699.                 break;
  700.             }
  701.  
  702.             $this->annotations = false;
  703.             $this->overriddenDefaults['annotations'= true;
  704.             break;
  705.         case 'config-set':
  706.             if (isset($this->cliArgs[($pos + 1)]=== false
  707.                 || isset($this->cliArgs[($pos + 2)]=== false
  708.             {
  709.                 echo 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL;
  710.                 $this->printShortUsage();
  711.                 exit(0);
  712.             }
  713.  
  714.             $key     $this->cliArgs[($pos + 1)];
  715.             $value   $this->cliArgs[($pos + 2)];
  716.             $current = self::getConfigData($key);
  717.  
  718.             try {
  719.                 $this->setConfigData($key$value);
  720.             catch (Exception $e{
  721.                 echo $e->getMessage().PHP_EOL;
  722.                 exit(3);
  723.             }
  724.  
  725.             if ($current === null{
  726.                 echo "Config value \"$key\" added successfully".PHP_EOL;
  727.             else {
  728.                 echo "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL;
  729.             }
  730.             exit(0);
  731.         case 'config-delete':
  732.             if (isset($this->cliArgs[($pos + 1)]=== false{
  733.                 echo 'ERROR: Deleting a config option requires the name of the option'.PHP_EOL.PHP_EOL;
  734.                 $this->printShortUsage();
  735.                 exit(0);
  736.             }
  737.  
  738.             $key     $this->cliArgs[($pos + 1)];
  739.             $current = self::getConfigData($key);
  740.             if ($current === null{
  741.                 echo "Config value \"$key\" has not been set".PHP_EOL;
  742.             else {
  743.                 try {
  744.                     $this->setConfigData($keynull);
  745.                 catch (Exception $e{
  746.                     echo $e->getMessage().PHP_EOL;
  747.                     exit(3);
  748.                 }
  749.  
  750.                 echo "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
  751.             }
  752.             exit(0);
  753.         case 'config-show':
  754.             $data = self::getAllConfigData();
  755.             $this->printConfigData($data);
  756.             exit(0);
  757.         case 'runtime-set':
  758.             if (isset($this->cliArgs[($pos + 1)]=== false
  759.                 || isset($this->cliArgs[($pos + 2)]=== false
  760.             {
  761.                 echo 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL;
  762.                 $this->printShortUsage();
  763.                 exit(0);
  764.             }
  765.  
  766.             $key   $this->cliArgs[($pos + 1)];
  767.             $value $this->cliArgs[($pos + 2)];
  768.             $this->cliArgs[($pos + 1)'';
  769.             $this->cliArgs[($pos + 2)'';
  770.             self::setConfigData($key$valuetrue);
  771.             break;
  772.         default:
  773.             if (substr($arg07=== 'sniffs='{
  774.                 $sniffs explode(','substr($arg7));
  775.                 foreach ($sniffs as $sniff{
  776.                     if (substr_count($sniff'.'!== 2{
  777.                         echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
  778.                         $this->printShortUsage();
  779.                         exit(3);
  780.                     }
  781.                 }
  782.  
  783.                 $this->sniffs $sniffs;
  784.                 $this->overriddenDefaults['sniffs'= true;
  785.             else if (substr($arg08=== 'exclude='{
  786.                 $sniffs explode(','substr($arg8));
  787.                 foreach ($sniffs as $sniff{
  788.                     if (substr_count($sniff'.'!== 2{
  789.                         echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
  790.                         $this->printShortUsage();
  791.                         exit(3);
  792.                     }
  793.                 }
  794.  
  795.                 $this->exclude $sniffs;
  796.                 $this->overriddenDefaults['exclude'= true;
  797.             else if (defined('PHP_CODESNIFFER_IN_TESTS'=== false
  798.                 && substr($arg06=== 'cache='
  799.             {
  800.                 // Turn caching on.
  801.                 $this->cache = true;
  802.                 $this->overriddenDefaults['cache'= true;
  803.  
  804.                 $this->cacheFile = Util\Common::realpath(substr($arg6));
  805.  
  806.                 // It may not exist and return false instead.
  807.                 if ($this->cacheFile === false{
  808.                     $this->cacheFile substr($arg6);
  809.  
  810.                     $dir dirname($this->cacheFile);
  811.                     if (is_dir($dir=== false{
  812.                         echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
  813.                         $this->printShortUsage();
  814.                         exit(3);
  815.                     }
  816.  
  817.                     if ($dir === '.'{
  818.                         // Passed cache file is a file in the current directory.
  819.                         $this->cacheFile getcwd().'/'.basename($this->cacheFile);
  820.                     else {
  821.                         if ($dir{0=== '/'{
  822.                             // An absolute path.
  823.                             $dir = Util\Common::realpath($dir);
  824.                         else {
  825.                             $dir = Util\Common::realpath(getcwd().'/'.$dir);
  826.                         }
  827.  
  828.                         if ($dir !== false{
  829.                             // Cache file path is relative.
  830.                             $this->cacheFile $dir.'/'.basename($this->cacheFile);
  831.                         }
  832.                     }
  833.                 }//end if
  834.  
  835.                 $this->overriddenDefaults['cacheFile'= true;
  836.  
  837.                 if (is_dir($this->cacheFile=== true{
  838.                     echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL;
  839.                     $this->printShortUsage();
  840.                     exit(3);
  841.                 }
  842.             else if (substr($arg010=== 'bootstrap='{
  843.                 $files     explode(','substr($arg10));
  844.                 $bootstrap = array();
  845.                 foreach ($files as $file{
  846.                     $path = Util\Common::realpath($file);
  847.                     if ($path === false{
  848.                         echo 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
  849.                         $this->printShortUsage();
  850.                         exit(3);
  851.                     }
  852.  
  853.                     $bootstrap[$path;
  854.                 }
  855.  
  856.                 $this->bootstrap array_merge($this->bootstrap$bootstrap);
  857.                 $this->overriddenDefaults['bootstrap'= true;
  858.             else if (substr($arg010=== 'file-list='{
  859.                 $fileList substr($arg10);
  860.                 $path     = Util\Common::realpath($fileList);
  861.                 if ($path === false{
  862.                     echo 'ERROR: The specified file list "'.$fileList.'" does not exist'.PHP_EOL.PHP_EOL;
  863.                     $this->printShortUsage();
  864.                     exit(3);
  865.                 }
  866.  
  867.                 $files file($path);
  868.                 foreach ($files as $inputFile{
  869.                     $inputFile trim($inputFile);
  870.  
  871.                     // Skip empty lines.
  872.                     if ($inputFile === ''{
  873.                         continue;
  874.                     }
  875.  
  876.                     $this->processFilePath($inputFile);
  877.                 }
  878.             else if (substr($arg011=== 'stdin-path='{
  879.                 $this->stdinPath = Util\Common::realpath(substr($arg11));
  880.  
  881.                 // It may not exist and return false instead, so use whatever they gave us.
  882.                 if ($this->stdinPath === false{
  883.                     $this->stdinPath trim(substr($arg11));
  884.                 }
  885.  
  886.                 $this->overriddenDefaults['stdinPath'= true;
  887.             else if (PHP_CODESNIFFER_CBF === false && substr($arg012=== 'report-file='{
  888.                 $this->reportFile = Util\Common::realpath(substr($arg12));
  889.  
  890.                 // It may not exist and return false instead.
  891.                 if ($this->reportFile === false{
  892.                     $this->reportFile substr($arg12);
  893.  
  894.                     $dir dirname($this->reportFile);
  895.                     if (is_dir($dir=== false{
  896.                         echo 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
  897.                         $this->printShortUsage();
  898.                         exit(3);
  899.                     }
  900.  
  901.                     if ($dir === '.'{
  902.                         // Passed report file is a file in the current directory.
  903.                         $this->reportFile getcwd().'/'.basename($this->reportFile);
  904.                     else {
  905.                         if ($dir{0=== '/'{
  906.                             // An absolute path.
  907.                             $dir = Util\Common::realpath($dir);
  908.                         else {
  909.                             $dir = Util\Common::realpath(getcwd().'/'.$dir);
  910.                         }
  911.  
  912.                         if ($dir !== false{
  913.                             // Report file path is relative.
  914.                             $this->reportFile $dir.'/'.basename($this->reportFile);
  915.                         }
  916.                     }
  917.                 }//end if
  918.  
  919.                 $this->overriddenDefaults['reportFile'= true;
  920.  
  921.                 if (is_dir($this->reportFile=== true{
  922.                     echo 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL;
  923.                     $this->printShortUsage();
  924.                     exit(3);
  925.                 }
  926.             else if (substr($arg013=== 'report-width='{
  927.                 if (isset($this->overriddenDefaults['reportWidth']=== true{
  928.                     break;
  929.                 }
  930.  
  931.                 $this->reportWidth substr($arg13);
  932.                 $this->overriddenDefaults['reportWidth'= true;
  933.             else if (substr($arg09=== 'basepath='{
  934.                 if (isset($this->overriddenDefaults['basepath']=== true{
  935.                     break;
  936.                 }
  937.  
  938.                 $this->basepath = Util\Common::realpath(substr($arg9));
  939.  
  940.                 // It may not exist and return false instead.
  941.                 if ($this->basepath === false{
  942.                     $this->basepath substr($arg9);
  943.                 }
  944.  
  945.                 $this->overriddenDefaults['basepath'= true;
  946.  
  947.                 if (is_dir($this->basepath=== false{
  948.                     echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
  949.                     $this->printShortUsage();
  950.                     exit(3);
  951.                 }
  952.             else if ((substr($arg07=== 'report=' || substr($arg07=== 'report-')) {
  953.                 $reports = array();
  954.  
  955.                 if ($arg[6=== '-'{
  956.                     // This is a report with file output.
  957.                     $split strpos($arg'=');
  958.                     if ($split === false{
  959.                         $report substr($arg7);
  960.                         $output = null;
  961.                     else {
  962.                         $report substr($arg7($split - 7));
  963.                         $output substr($arg($split + 1));
  964.                         if ($output === false{
  965.                             $output = null;
  966.                         else {
  967.                             $dir dirname($output);
  968.                             if ($dir === '.'{
  969.                                 // Passed report file is a filename in the current directory.
  970.                                 $output getcwd().'/'.basename($output);
  971.                             else {
  972.                                 if ($dir{0=== '/'{
  973.                                     // An absolute path.
  974.                                     $dir = Util\Common::realpath($dir);
  975.                                 else {
  976.                                     $dir = Util\Common::realpath(getcwd().'/'.$dir);
  977.                                 }
  978.  
  979.                                 if ($dir !== false{
  980.                                     // Report file path is relative.
  981.                                     $output $dir.'/'.basename($output);
  982.                                 }
  983.                             }
  984.                         }//end if
  985.                     }//end if
  986.  
  987.                     $reports[$report$output;
  988.                 else {
  989.                     // This is a single report.
  990.                     if (isset($this->overriddenDefaults['reports']=== true{
  991.                         break;
  992.                     }
  993.  
  994.                     $reportNames explode(','substr($arg7));
  995.                     foreach ($reportNames as $report{
  996.                         $reports[$report= null;
  997.                     }
  998.                 }//end if
  999.  
  1000.                 // Remove the default value so the CLI value overrides it.
  1001.                 if (isset($this->overriddenDefaults['reports']=== false{
  1002.                     $this->reports $reports;
  1003.                 else {
  1004.                     $this->reports array_merge($this->reports$reports);
  1005.                 }
  1006.  
  1007.                 $this->overriddenDefaults['reports'= true;
  1008.             else if (substr($arg07=== 'filter='{
  1009.                 if (isset($this->overriddenDefaults['filter']=== true{
  1010.                     break;
  1011.                 }
  1012.  
  1013.                 $this->filter substr($arg7);
  1014.                 $this->overriddenDefaults['filter'= true;
  1015.             else if (substr($arg09=== 'standard='{
  1016.                 $standards trim(substr($arg9));
  1017.                 if ($standards !== ''{
  1018.                     $this->standards explode(','$standards);
  1019.                 }
  1020.  
  1021.                 $this->overriddenDefaults['standards'= true;
  1022.             else if (substr($arg011=== 'extensions='{
  1023.                 $extensions    explode(','substr($arg11));
  1024.                 $newExtensions = array();
  1025.                 foreach ($extensions as $ext{
  1026.                     $slash strpos($ext'/');
  1027.                     if ($slash !== false{
  1028.                         // They specified the tokenizer too.
  1029.                         list($ext$tokenizerexplode('/'$ext);
  1030.                         $newExtensions[$ext]   strtoupper($tokenizer);
  1031.                         continue;
  1032.                     }
  1033.  
  1034.                     if (isset($this->extensions[$ext]=== true{
  1035.                         $newExtensions[$ext$this->extensions[$ext];
  1036.                     else {
  1037.                         $newExtensions[$ext'PHP';
  1038.                     }
  1039.                 }
  1040.  
  1041.                 $this->extensions $newExtensions;
  1042.                 $this->overriddenDefaults['extensions'= true;
  1043.             else if (substr($arg07=== 'suffix='{
  1044.                 $this->suffix explode(','substr($arg7));
  1045.                 $this->overriddenDefaults['suffix'= true;
  1046.             else if (substr($arg09=== 'parallel='{
  1047.                 if (isset($this->overriddenDefaults['parallel']=== true{
  1048.                     break;
  1049.                 }
  1050.  
  1051.                 $this->parallel max((int) substr($arg9)1);
  1052.                 $this->overriddenDefaults['parallel'= true;
  1053.             else if (substr($arg09=== 'severity='{
  1054.                 $this->errorSeverity   = (int) substr($arg9);
  1055.                 $this->warningSeverity $this->errorSeverity;
  1056.                 $this->overriddenDefaults['errorSeverity']   = true;
  1057.                 $this->overriddenDefaults['warningSeverity'= true;
  1058.             else if (substr($arg015=== 'error-severity='{
  1059.                 $this->errorSeverity = (int) substr($arg15);
  1060.                 $this->overriddenDefaults['errorSeverity'= true;
  1061.             else if (substr($arg017=== 'warning-severity='{
  1062.                 $this->warningSeverity = (int) substr($arg17);
  1063.                 $this->overriddenDefaults['warningSeverity'= true;
  1064.             else if (substr($arg07=== 'ignore='{
  1065.                 // Split the ignore string on commas, unless the comma is escaped
  1066.                 // using 1 or 3 slashes (\, or \\\,).
  1067.                 $patterns preg_split(
  1068.                     '/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/',
  1069.                     substr($arg7)
  1070.                 );
  1071.  
  1072.                 $ignored = array();
  1073.                 foreach ($patterns as $pattern{
  1074.                     $pattern trim($pattern);
  1075.                     if ($pattern === ''{
  1076.                         continue;
  1077.                     }
  1078.  
  1079.                     $ignored[$pattern'absolute';
  1080.                 }
  1081.  
  1082.                 $this->ignored $ignored;
  1083.                 $this->overriddenDefaults['ignored'= true;
  1084.             else if (substr($arg010=== 'generator='
  1085.                 && PHP_CODESNIFFER_CBF === false
  1086.             {
  1087.                 $this->generator substr($arg10);
  1088.                 $this->overriddenDefaults['generator'= true;
  1089.             else if (substr($arg09=== 'encoding='{
  1090.                 $this->encoding strtolower(substr($arg9));
  1091.                 $this->overriddenDefaults['encoding'= true;
  1092.             else if (substr($arg010=== 'tab-width='{
  1093.                 $this->tabWidth = (int) substr($arg10);
  1094.                 $this->overriddenDefaults['tabWidth'= true;
  1095.             else {
  1096.                 if ($this->dieOnUnknownArg === false{
  1097.                     $eqPos strpos($arg'=');
  1098.                     try {
  1099.                         if ($eqPos === false{
  1100.                             $this->values[$arg$arg;
  1101.                         else {
  1102.                             $value substr($arg($eqPos + 1));
  1103.                             $arg   substr($arg0$eqPos);
  1104.                             $this->values[$arg$value;
  1105.                         }
  1106.                     catch (RuntimeException $e{
  1107.                         // Value is not valid, so just ignore it.
  1108.                     }
  1109.                 else {
  1110.                     $this->processUnknownArgument('--'.$arg$pos);
  1111.                 }
  1112.             }//end if
  1113.  
  1114.             break;
  1115.         }//end switch
  1116.  
  1117.     }//end processLongArgument()
  1118.  
  1119.  
  1120.     /**
  1121.      * Processes an unknown command line argument.
  1122.      *
  1123.      * Assumes all unknown arguments are files and folders to check.
  1124.      *
  1125.      * @param string $arg The command line argument.
  1126.      * @param int    $pos The position of the argument on the command line.
  1127.      *
  1128.      * @return void 
  1129.      */
  1130.     public function processUnknownArgument($arg$pos)
  1131.     {
  1132.         // We don't know about any additional switches; just files.
  1133.         if ($arg{0=== '-'{
  1134.             if ($this->dieOnUnknownArg === false{
  1135.                 return;
  1136.             }
  1137.  
  1138.             echo "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL;
  1139.             $this->printShortUsage();
  1140.             exit(3);
  1141.         }
  1142.  
  1143.         $this->processFilePath($arg);
  1144.  
  1145.     }//end processUnknownArgument()
  1146.  
  1147.  
  1148.     /**
  1149.      * Processes a file path and add it to the file list.
  1150.      *
  1151.      * @param string $path The path to the file to add.
  1152.      *
  1153.      * @return void 
  1154.      */
  1155.     public function processFilePath($path)
  1156.     {
  1157.         // If we are processing STDIN, don't record any files to check.
  1158.         if ($this->stdin === true{
  1159.             return;
  1160.         }
  1161.  
  1162.         $file = Util\Common::realpath($path);
  1163.         if (file_exists($file=== false{
  1164.             if ($this->dieOnUnknownArg === false{
  1165.                 return;
  1166.             }
  1167.  
  1168.             echo 'ERROR: The file "'.$path.'" does not exist.'.PHP_EOL.PHP_EOL;
  1169.             $this->printShortUsage();
  1170.             exit(3);
  1171.         else {
  1172.             $files       $this->files;
  1173.             $files[]     $file;
  1174.             $this->files $files;
  1175.             $this->overriddenDefaults['files'= true;
  1176.         }
  1177.  
  1178.     }//end processFilePath()
  1179.  
  1180.  
  1181.     /**
  1182.      * Prints out the usage information for this script.
  1183.      *
  1184.      * @return void 
  1185.      */
  1186.     public function printUsage()
  1187.     {
  1188.         echo PHP_EOL;
  1189.  
  1190.         if (PHP_CODESNIFFER_CBF === true{
  1191.             $this->printPHPCBFUsage();
  1192.         else {
  1193.             $this->printPHPCSUsage();
  1194.         }
  1195.  
  1196.         echo PHP_EOL;
  1197.  
  1198.     }//end printUsage()
  1199.  
  1200.  
  1201.     /**
  1202.      * Prints out the short usage information for this script.
  1203.      *
  1204.      * @return void 
  1205.      */
  1206.     public function printShortUsage()
  1207.     {
  1208.         if (PHP_CODESNIFFER_CBF === true{
  1209.             echo 'Run "phpcbf --help" for usage information';
  1210.         else {
  1211.             echo 'Run "phpcs --help" for usage information';
  1212.         }
  1213.  
  1214.         echo PHP_EOL.PHP_EOL;
  1215.  
  1216.     }//end printShortUsage()
  1217.  
  1218.  
  1219.     /**
  1220.      * Prints out the usage information for PHPCS.
  1221.      *
  1222.      * @return void 
  1223.      */
  1224.     public function printPHPCSUsage()
  1225.     {
  1226.         echo 'Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--cache[=<cacheFile>]] [--no-cache] [--colors] [--no-colors]'.PHP_EOL;
  1227.         echo '  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL;
  1228.         echo '  [--report-width=<reportWidth>] [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL;
  1229.         echo '  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
  1230.         echo '  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]'.PHP_EOL;
  1231.         echo '  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>] '.PHP_EOL;
  1232.         echo '  [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]'.PHP_EOL;
  1233.         echo '  [--extensions=<extensions>] [--ignore=<patterns>] [--ignore-annotations] <file> - ...'.PHP_EOL;
  1234.         echo PHP_EOL;
  1235.         echo ' -     Check STDIN instead of local files and directories'.PHP_EOL;
  1236.         echo ' -n    Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL;
  1237.         echo ' -w    Print both warnings and errors (this is the default)'.PHP_EOL;
  1238.         echo ' -l    Local directory only, no recursion'.PHP_EOL;
  1239.         echo ' -s    Show sniff codes in all reports'.PHP_EOL;
  1240.         echo ' -a    Run interactively'.PHP_EOL;
  1241.         echo ' -e    Explain a standard by showing the sniffs it includes'.PHP_EOL;
  1242.         echo ' -p    Show progress of the run'.PHP_EOL;
  1243.         echo ' -q    Quiet mode; disables progress and verbose output'.PHP_EOL;
  1244.         echo ' -m    Stop error messages from being recorded'.PHP_EOL;
  1245.         echo '       (saves a lot of memory, but stops many reports from being used)'.PHP_EOL;
  1246.         echo ' -v    Print processed files'.PHP_EOL;
  1247.         echo ' -vv   Print ruleset and token output'.PHP_EOL;
  1248.         echo ' -vvv  Print sniff processing information'.PHP_EOL;
  1249.         echo ' -i    Show a list of installed coding standards'.PHP_EOL;
  1250.         echo ' -d    Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL;
  1251.         echo PHP_EOL;
  1252.         echo ' --help                Print this help message'.PHP_EOL;
  1253.         echo ' --version             Print version information'.PHP_EOL;
  1254.         echo ' --colors              Use colors in output'.PHP_EOL;
  1255.         echo ' --no-colors           Do not use colors in output (this is the default)'.PHP_EOL;
  1256.         echo ' --cache               Cache results between runs'.PHP_EOL;
  1257.         echo ' --no-cache            Do not cache results between runs (this is the default)'.PHP_EOL;
  1258.         echo ' --ignore-annotations  Ignore all @codingStandard annotations in code comments'.PHP_EOL;
  1259.         echo PHP_EOL;
  1260.         echo ' <cacheFile>    Use a specific file for caching (uses a temporary file by default)'.PHP_EOL;
  1261.         echo ' <basepath>     A path to strip from the front of file paths inside reports'.PHP_EOL;
  1262.         echo ' <file>         One or more files and/or directories to check'.PHP_EOL;
  1263.         echo ' <encoding>     The encoding of the files being checked (default is utf-8)'.PHP_EOL;
  1264.         echo ' <extensions>   A comma separated list of file extensions to check'.PHP_EOL;
  1265.         echo '                (extension filtering only valid when checking a directory)'.PHP_EOL;
  1266.         echo '                The type of the file can be specified using: ext/type'.PHP_EOL;
  1267.         echo '                e.g., module/php,es/js'.PHP_EOL;
  1268.         echo ' <generator>    Uses either the "HTML", "Markdown" or "Text" generator'.PHP_EOL;
  1269.         echo '                (forces documentation generation instead of checking)'.PHP_EOL;
  1270.         echo ' <patterns>     A comma separated list of patterns to ignore files and directories'.PHP_EOL;
  1271.         echo ' <processes>    How many files should be checked simultaneously (default is 1)'.PHP_EOL;
  1272.         echo ' <report>       Print either the "full", "xml", "checkstyle", "csv"'.PHP_EOL;
  1273.         echo '                "json", "junit", "emacs", "source", "summary", "diff"'.PHP_EOL;
  1274.         echo '                "svnblame", "gitblame", "hgblame" or "notifysend" report'.PHP_EOL;
  1275.         echo '                (the "full" report is printed by default)'.PHP_EOL;
  1276.         echo ' <reportFile>   Write the report to the specified file path'.PHP_EOL;
  1277.         echo ' <reportWidth>  How many columns wide screen reports should be printed'.PHP_EOL;
  1278.         echo '                or set to "auto" to use current screen width, where supported'.PHP_EOL;
  1279.         echo ' <sniffs>       A comma separated list of sniff codes to include or exclude from checking'.PHP_EOL;
  1280.         echo '                (all sniffs must be part of the specified standard)'.PHP_EOL;
  1281.         echo ' <severity>     The minimum severity required to display an error or warning'.PHP_EOL;
  1282.         echo ' <standard>     The name or path of the coding standard to use'.PHP_EOL;
  1283.         echo ' <tabWidth>     The number of spaces each tab represents'.PHP_EOL;
  1284.  
  1285.     }//end printPHPCSUsage()
  1286.  
  1287.  
  1288.     /**
  1289.      * Prints out the usage information for PHPCBF.
  1290.      *
  1291.      * @return void 
  1292.      */
  1293.     public function printPHPCBFUsage()
  1294.     {
  1295.         echo 'Usage: phpcbf [-nwli] [-d key[=value]] [--ignore-annotations]'.PHP_EOL;
  1296.         echo '  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>] [--suffix=<suffix>]'.PHP_EOL;
  1297.         echo '  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
  1298.         echo '  [--tab-width=<tabWidth>] [--encoding=<encoding>] [--parallel=<processes>]'.PHP_EOL;
  1299.         echo '  [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL;
  1300.         echo PHP_EOL;
  1301.         echo ' -     Fix STDIN instead of local files and directories'.PHP_EOL;
  1302.         echo ' -n    Do not fix warnings (shortcut for --warning-severity=0)'.PHP_EOL;
  1303.         echo ' -w    Fix both warnings and errors (on by default)'.PHP_EOL;
  1304.         echo ' -l    Local directory only, no recursion'.PHP_EOL;
  1305.         echo ' -p    Show progress of the run'.PHP_EOL;
  1306.         echo ' -q    Quiet mode; disables progress and verbose output'.PHP_EOL;
  1307.         echo ' -v    Print processed files'.PHP_EOL;
  1308.         echo ' -vv   Print ruleset and token output'.PHP_EOL;
  1309.         echo ' -vvv  Print sniff processing information'.PHP_EOL;
  1310.         echo ' -i    Show a list of installed coding standards'.PHP_EOL;
  1311.         echo ' -d    Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL;
  1312.         echo PHP_EOL;
  1313.         echo ' --help                Print this help message'.PHP_EOL;
  1314.         echo ' --version             Print version information'.PHP_EOL;
  1315.         echo ' --ignore-annotations  Ignore all @codingStandard annotations in code comments'.PHP_EOL;
  1316.         echo PHP_EOL;
  1317.         echo ' <basepath>    A path to strip from the front of file paths inside reports'.PHP_EOL;
  1318.         echo ' <file>        One or more files and/or directories to fix'.PHP_EOL;
  1319.         echo ' <encoding>    The encoding of the files being fixed (default is utf-8)'.PHP_EOL;
  1320.         echo ' <extensions>  A comma separated list of file extensions to fix'.PHP_EOL;
  1321.         echo '               (extension filtering only valid when checking a directory)'.PHP_EOL;
  1322.         echo '               The type of the file can be specified using: ext/type'.PHP_EOL;
  1323.         echo '               e.g., module/php,es/js'.PHP_EOL;
  1324.         echo ' <patterns>    A comma separated list of patterns to ignore files and directories'.PHP_EOL;
  1325.         echo ' <processes>   How many files should be fixed simultaneously (default is 1)'.PHP_EOL;
  1326.         echo ' <sniffs>      A comma separated list of sniff codes to include or exclude from fixing'.PHP_EOL;
  1327.         echo '               (all sniffs must be part of the specified standard)'.PHP_EOL;
  1328.         echo ' <severity>    The minimum severity required to fix an error or warning'.PHP_EOL;
  1329.         echo ' <standard>    The name or path of the coding standard to use'.PHP_EOL;
  1330.         echo ' <suffix>      Write modified files to a filename using this suffix'.PHP_EOL;
  1331.         echo '               ("diff" and "patch" are not used in this mode)'.PHP_EOL;
  1332.         echo ' <tabWidth>    The number of spaces each tab represents'.PHP_EOL;
  1333.  
  1334.     }//end printPHPCBFUsage()
  1335.  
  1336.  
  1337.     /**
  1338.      * Get a single config value.
  1339.      *
  1340.      * @param string $key The name of the config value.
  1341.      *
  1342.      * @return string|null
  1343.      * @see    setConfigData()
  1344.      * @see    getAllConfigData()
  1345.      */
  1346.     public static function getConfigData($key)
  1347.     {
  1348.         $phpCodeSnifferConfig = self::getAllConfigData();
  1349.  
  1350.         if ($phpCodeSnifferConfig === null{
  1351.             return null;
  1352.         }
  1353.  
  1354.         if (isset($phpCodeSnifferConfig[$key]=== false{
  1355.             return null;
  1356.         }
  1357.  
  1358.         return $phpCodeSnifferConfig[$key];
  1359.  
  1360.     }//end getConfigData()
  1361.  
  1362.  
  1363.     /**
  1364.      * Get the path to an executable utility.
  1365.      *
  1366.      * @param string $name The name of the executable utility.
  1367.      *
  1368.      * @return string|null
  1369.      * @see    getConfigData()
  1370.      */
  1371.     public static function getExecutablePath($name)
  1372.     {
  1373.         $data = self::getConfigData($name.'_path');
  1374.         if ($data !== null{
  1375.             return $data;
  1376.         }
  1377.  
  1378.         if (array_key_exists($nameself::$executablePaths=== true{
  1379.             return self::$executablePaths[$name];
  1380.         }
  1381.  
  1382.         if (strtoupper(substr(PHP_OS03)) === 'WIN'{
  1383.             $cmd 'where '.escapeshellarg($name).' 2> nul';
  1384.         else {
  1385.             $cmd 'which '.escapeshellarg($name).' 2> /dev/null';
  1386.         }
  1387.  
  1388.         $result exec($cmd$output$retVal);
  1389.         if ($retVal !== 0{
  1390.             $result = null;
  1391.         }
  1392.  
  1393.         self::$executablePaths[$name$result;
  1394.         return $result;
  1395.  
  1396.     }//end getExecutablePath()
  1397.  
  1398.  
  1399.     /**
  1400.      * Set a single config value.
  1401.      *
  1402.      * @param string      $key   The name of the config value.
  1403.      * @param string|null$value The value to set. If null, the config
  1404.      *                            entry is deleted, reverting it to the
  1405.      *                            default value.
  1406.      * @param boolean     $temp  Set this config data temporarily for this
  1407.      *                            script run. This will not write the config
  1408.      *                            data to the config file.
  1409.      *
  1410.      * @return bool 
  1411.      * @see    getConfigData()
  1412.      * @throws RuntimeException If the config file can not be written.
  1413.      */
  1414.     public static function setConfigData($key$value$temp=false)
  1415.     {
  1416.         if ($temp === false{
  1417.             $path '';
  1418.             if (is_callable('\Phar::running'=== true{
  1419.                 $path = \Phar::running(false);
  1420.             }
  1421.  
  1422.             if ($path !== ''{
  1423.                 $configFile dirname($path).'/CodeSniffer.conf';
  1424.             else {
  1425.                 $configFile dirname(__DIR__).'/CodeSniffer.conf';
  1426.                 if (is_file($configFile=== false
  1427.                     && strpos('@data_dir@''@data_dir'=== false
  1428.                 {
  1429.                     // If data_dir was replaced, this is a PEAR install and we can
  1430.                     // use the PEAR data dir to store the conf file.
  1431.                     $configFile '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
  1432.                 }
  1433.             }
  1434.  
  1435.             if (is_file($configFile=== true
  1436.                 && is_writable($configFile=== false
  1437.             {
  1438.                 $error 'Config file '.$configFile.' is not writable';
  1439.                 throw new RuntimeException($error);
  1440.             }
  1441.         }//end if
  1442.  
  1443.         $phpCodeSnifferConfig = self::getAllConfigData();
  1444.  
  1445.         if ($value === null{
  1446.             if (isset($phpCodeSnifferConfig[$key]=== true{
  1447.                 unset($phpCodeSnifferConfig[$key]);
  1448.             }
  1449.         else {
  1450.             $phpCodeSnifferConfig[$key$value;
  1451.         }
  1452.  
  1453.         if ($temp === false{
  1454.             $output  '<'.'?php'."\n".' $phpCodeSnifferConfig = ';
  1455.             $output .= var_export($phpCodeSnifferConfigtrue);
  1456.             $output .= "\n?".'>';
  1457.  
  1458.             if (file_put_contents($configFile$output=== false{
  1459.                 return false;
  1460.             }
  1461.         }
  1462.  
  1463.         self::$configData $phpCodeSnifferConfig;
  1464.  
  1465.         return true;
  1466.  
  1467.     }//end setConfigData()
  1468.  
  1469.  
  1470.     /**
  1471.      * Get all config data.
  1472.      *
  1473.      * @return array<string, string>
  1474.      * @see    getConfigData()
  1475.      */
  1476.     public static function getAllConfigData()
  1477.     {
  1478.         if (self::$configData !== null{
  1479.             return self::$configData;
  1480.         }
  1481.  
  1482.         $path '';
  1483.         if (is_callable('\Phar::running'=== true{
  1484.             $path = \Phar::running(false);
  1485.         }
  1486.  
  1487.         if ($path !== ''{
  1488.             $configFile dirname($path).'/CodeSniffer.conf';
  1489.         else {
  1490.             $configFile dirname(__DIR__).'/CodeSniffer.conf';
  1491.             if (is_file($configFile=== false
  1492.                 && strpos('@data_dir@''@data_dir'=== false
  1493.             {
  1494.                 $configFile '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
  1495.             }
  1496.         }
  1497.  
  1498.         if (is_file($configFile=== false{
  1499.             self::$configData = array();
  1500.             return array();
  1501.         }
  1502.  
  1503.         include $configFile;
  1504.         self::$configData $phpCodeSnifferConfig;
  1505.         return self::$configData;
  1506.  
  1507.     }//end getAllConfigData()
  1508.  
  1509.  
  1510.     /**
  1511.      * Prints out the gathered config data.
  1512.      *
  1513.      * @param array $data The config data to print.
  1514.      *
  1515.      * @return void 
  1516.      */
  1517.     public function printConfigData($data)
  1518.     {
  1519.         $max  = 0;
  1520.         $keys = array_keys($data);
  1521.         foreach ($keys as $key{
  1522.             $len strlen($key);
  1523.             if (strlen($key$max{
  1524.                 $max $len;
  1525.             }
  1526.         }
  1527.  
  1528.         if ($max === 0{
  1529.             return;
  1530.         }
  1531.  
  1532.         $max += 2;
  1533.         ksort($data);
  1534.         foreach ($data as $name => $value{
  1535.             echo str_pad($name.': '$max).$value.PHP_EOL;
  1536.         }
  1537.  
  1538.     }//end printConfigData()
  1539.  
  1540.  
  1541. }//end class

Documentation generated on Mon, 11 Mar 2019 14:23:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.