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

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