PHP_CodeSniffer::setConfigData PHP Method

setConfigData() public static method

Config data is stored in the data dir, in a file called CodeSniffer.conf. It is a simple PHP array.
See also: getConfigData()
public static setConfigData ( string $key, string | null $value, boolean $temp = false ) : boolean
$key string The name of the config value.
$value string | null The value to set. If null, the config entry is deleted, reverting it to the default value.
$temp boolean Set this config data temporarily for this script run. This will not write the config data to the config file.
return boolean
    public static function setConfigData($key, $value, $temp = false)
    {
        if ($temp === false) {
            $path = '';
            if (is_callable('Phar::running') === true) {
                $path = Phar::running(false);
            }
            if ($path !== '') {
                $configFile = dirname($path) . '/CodeSniffer.conf';
            } else {
                $configFile = dirname(__FILE__) . '/CodeSniffer.conf';
                if (is_file($configFile) === false && strpos('@data_dir@', '@data_dir') === false) {
                    // If data_dir was replaced, this is a PEAR install and we can
                    // use the PEAR data dir to store the conf file.
                    $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
                }
            }
            if (is_file($configFile) === true && is_writable($configFile) === false) {
                $error = 'Config file ' . $configFile . ' is not writable';
                throw new PHP_CodeSniffer_Exception($error);
            }
        }
        //end if
        $phpCodeSnifferConfig = self::getAllConfigData();
        if ($value === null) {
            if (isset($phpCodeSnifferConfig[$key]) === true) {
                unset($phpCodeSnifferConfig[$key]);
            }
        } else {
            $phpCodeSnifferConfig[$key] = $value;
        }
        if ($temp === false) {
            $output = '<' . '?php' . "\n" . ' $phpCodeSnifferConfig = ';
            $output .= var_export($phpCodeSnifferConfig, true);
            $output .= "\n?" . '>';
            if (file_put_contents($configFile, $output) === false) {
                return false;
            }
        }
        $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig;
        return true;
    }

Usage Example

 /**
  * Sniff a file and return resulting file object
  *
  * @param string $filename Filename to sniff
  * @param string $targetPhpVersion Value of 'testVersion' to set on PHPCS object
  * @return PHP_CodeSniffer_File File object
  */
 public function sniffFile($filename, $targetPhpVersion = null)
 {
     if (null !== $targetPhpVersion) {
         PHP_CodeSniffer::setConfigData('testVersion', $targetPhpVersion, true);
     }
     $filename = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . $filename;
     try {
         $phpcsFile = self::$phpcs->processFile($filename);
     } catch (Exception $e) {
         $this->fail('An unexpected exception has been caught: ' . $e->getMessage());
         return false;
     }
     return $phpcsFile;
 }
All Usage Examples Of PHP_CodeSniffer::setConfigData