config` property should work * in PHPCS 3.x and higher. * * @return bool Whether the setting of the data was successfull. */ public static function setConfigData($key, $value, $temp = false, $config = null) { if (isset($config) === true) { // PHPCS 3.x and 4.x. return $config->setConfigData($key, $value, $temp); } if (\version_compare(self::getVersion(), '3.99.99', '>') === true) { throw new RuntimeException('Passing the $config parameter is required in PHPCS 4.x'); } // PHPCS 3.x. return Config::setConfigData($key, $value, $temp); } /** * Get the value of a single PHP_CodeSniffer config key. * * @see Helper::getCommandLineData() Alternative for the same which is more reliable * if the `$phpcsFile` object is available. * * @since 1.0.0 * * @param string $key The name of the config value. * * @return string|null */ public static function getConfigData($key) { return Config::getConfigData($key); } /** * Get the value of a CLI overrulable single PHP_CodeSniffer config key. * * Use this for config keys which can be set in the `CodeSniffer.conf` file, * on the command-line or in a ruleset. * * @since 1.0.0 * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. * @param string $key The name of the config value. * * @return string|null */ public static function getCommandLineData(File $phpcsFile, $key) { if (isset($phpcsFile->config->{$key})) { return $phpcsFile->config->{$key}; } return null; } /** * Get the applicable tab width as passed to PHP_CodeSniffer from the * command-line or the ruleset. * * @since 1.0.0 * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. * * @return int Tab width. Defaults to the PHPCS native default of 4. */ public static function getTabWidth(File $phpcsFile) { $tabWidth = self::getCommandLineData($phpcsFile, 'tabWidth'); if ($tabWidth > 0) { return (int) $tabWidth; } return self::DEFAULT_TABWIDTH; } /** * Get the applicable (file) encoding as passed to PHP_CodeSniffer from the * command-line or the ruleset. * * @since 1.0.0 * * @param \PHP_CodeSniffer\Files\File|null $phpcsFile Optional. The current file being processed. * * @return string Encoding. Defaults to the PHPCS native default, which is 'utf-8' * for PHPCS 3.x. */ public static function getEncoding(File $phpcsFile = null) { $default = 'utf-8'; if ($phpcsFile instanceof File) { // Most reliable. $encoding = self::getCommandLineData($phpcsFile, 'encoding'); if ($encoding === null) { $encoding = $default; } return $encoding; } // Less reliable. $encoding = self::getConfigData('encoding'); if ($encoding === null) { $encoding = $default; } return $encoding; } /** * Check whether the "--ignore-annotations" option is in effect. * * @since 1.0.0 * * @param \PHP_CodeSniffer\Files\File|null $phpcsFile Optional. The current file being processed. * * @return bool `TRUE` if annotations should be ignored, `FALSE` otherwise. */ public static function ignoreAnnotations(File $phpcsFile = null) { if (isset($phpcsFile, $phpcsFile->config->annotations)) { return ! $phpcsFile->config->annotations; } $annotations = Config::getConfigData('annotations'); if (isset($annotations)) { return ! $annotations; } return false; } }