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

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