|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSUtils |
| 6 | + * @copyright 2019-2020 PHPCSUtils Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Magento2\Helpers\PHPCSUtils\BackCompat; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Exceptions\RuntimeException; |
| 14 | +use PHP_CodeSniffer\Files\File; |
| 15 | + |
| 16 | +/** |
| 17 | + * Utility methods to retrieve (configuration) information from PHP_CodeSniffer. |
| 18 | + * |
| 19 | + * PHP_CodeSniffer cross-version compatibility helper for PHPCS 2.x vs PHPCS 3.x. |
| 20 | + * |
| 21 | + * A number of PHPCS classes were split up into several classes in PHPCS 3.x |
| 22 | + * Those classes cannot be aliased as they don't represent the same object. |
| 23 | + * This class provides helper methods for functions which were contained in |
| 24 | + * one of these classes and which are commonly used by external standards. |
| 25 | + * |
| 26 | + * @since 1.0.0 The initial methods in this class have been ported over from |
| 27 | + * the external PHPCompatibility & WPCS standards. |
| 28 | + */ |
| 29 | +class Helper |
| 30 | +{ |
| 31 | + |
| 32 | + /** |
| 33 | + * The default tab width used by PHP_CodeSniffer. |
| 34 | + * |
| 35 | + * @since 1.0.0 |
| 36 | + * |
| 37 | + * @var int |
| 38 | + */ |
| 39 | + const DEFAULT_TABWIDTH = 4; |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the PHP_CodeSniffer version number. |
| 43 | + * |
| 44 | + * @since 1.0.0 |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + public static function getVersion() |
| 49 | + { |
| 50 | + if (\defined('\PHP_CodeSniffer\Config::VERSION') === false) { |
| 51 | + // PHPCS 2.x. |
| 52 | + return \PHP_CodeSniffer::VERSION; |
| 53 | + } |
| 54 | + |
| 55 | + // PHPCS 3.x. |
| 56 | + return \PHP_CodeSniffer\Config::VERSION; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Pass config data to PHP_CodeSniffer. |
| 61 | + * |
| 62 | + * @since 1.0.0 |
| 63 | + * |
| 64 | + * @param string $key The name of the config value. |
| 65 | + * @param string|null $value The value to set. If `null`, the config entry |
| 66 | + * is deleted, reverting it to the default value. |
| 67 | + * @param bool $temp Set this config data temporarily for this script run. |
| 68 | + * This will not write the config data to the config file. |
| 69 | + * @param \PHP_CodeSniffer\Config $config The PHPCS config object. |
| 70 | + * This parameter is required for PHPCS 4.x, optional |
| 71 | + * for PHPCS 3.x and not possible to pass for PHPCS 2.x. |
| 72 | + * Passing the `$phpcsFile->config` property should work |
| 73 | + * in PHPCS 3.x and higher. |
| 74 | + * |
| 75 | + * @return bool Whether the setting of the data was successfull. |
| 76 | + */ |
| 77 | + public static function setConfigData($key, $value, $temp = false, $config = null) |
| 78 | + { |
| 79 | + if (\method_exists('\PHP_CodeSniffer\Config', 'setConfigData') === false) { |
| 80 | + // PHPCS 2.x. |
| 81 | + return \PHP_CodeSniffer::setConfigData($key, $value, $temp); |
| 82 | + } |
| 83 | + |
| 84 | + if (isset($config) === true) { |
| 85 | + // PHPCS 3.x and 4.x. |
| 86 | + return $config->setConfigData($key, $value, $temp); |
| 87 | + } |
| 88 | + |
| 89 | + if (\version_compare(self::getVersion(), '3.99.99', '>') === true) { |
| 90 | + throw new RuntimeException('Passing the $config parameter is required in PHPCS 4.x'); |
| 91 | + } |
| 92 | + |
| 93 | + // PHPCS 3.x. |
| 94 | + return \PHP_CodeSniffer\Config::setConfigData($key, $value, $temp); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the value of a single PHP_CodeSniffer config key. |
| 99 | + * |
| 100 | + * @see Helper::getCommandLineData() Alternative for the same which is more reliable |
| 101 | + * if the `$phpcsFile` object is available. |
| 102 | + * |
| 103 | + * @since 1.0.0 |
| 104 | + * |
| 105 | + * @param string $key The name of the config value. |
| 106 | + * |
| 107 | + * @return string|null |
| 108 | + */ |
| 109 | + public static function getConfigData($key) |
| 110 | + { |
| 111 | + if (\method_exists('\PHP_CodeSniffer\Config', 'getConfigData') === false) { |
| 112 | + // PHPCS 2.x. |
| 113 | + return \PHP_CodeSniffer::getConfigData($key); |
| 114 | + } |
| 115 | + |
| 116 | + // PHPCS 3.x. |
| 117 | + return \PHP_CodeSniffer\Config::getConfigData($key); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the value of a CLI overrulable single PHP_CodeSniffer config key. |
| 122 | + * |
| 123 | + * Use this for config keys which can be set in the `CodeSniffer.conf` file, |
| 124 | + * on the command-line or in a ruleset. |
| 125 | + * |
| 126 | + * @since 1.0.0 |
| 127 | + * |
| 128 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. |
| 129 | + * @param string $key The name of the config value. |
| 130 | + * |
| 131 | + * @return string|null |
| 132 | + */ |
| 133 | + public static function getCommandLineData(File $phpcsFile, $key) |
| 134 | + { |
| 135 | + if (\class_exists('\PHP_CodeSniffer\Config') === false) { |
| 136 | + // PHPCS 2.x. |
| 137 | + $config = $phpcsFile->phpcs->cli->getCommandLineValues(); |
| 138 | + if (isset($config[$key])) { |
| 139 | + return $config[$key]; |
| 140 | + } |
| 141 | + } else { |
| 142 | + // PHPCS 3.x. |
| 143 | + $config = $phpcsFile->config; |
| 144 | + if (isset($config->{$key})) { |
| 145 | + return $config->{$key}; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the applicable tab width as passed to PHP_CodeSniffer from the |
| 154 | + * command-line or the ruleset. |
| 155 | + * |
| 156 | + * @since 1.0.0 |
| 157 | + * |
| 158 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed. |
| 159 | + * |
| 160 | + * @return int Tab width. Defaults to the PHPCS native default of 4. |
| 161 | + */ |
| 162 | + public static function getTabWidth(File $phpcsFile) |
| 163 | + { |
| 164 | + $tabWidth = self::getCommandLineData($phpcsFile, 'tabWidth'); |
| 165 | + if ($tabWidth > 0) { |
| 166 | + return (int) $tabWidth; |
| 167 | + } |
| 168 | + |
| 169 | + return self::DEFAULT_TABWIDTH; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the applicable (file) encoding as passed to PHP_CodeSniffer from the |
| 174 | + * command-line or the ruleset. |
| 175 | + * |
| 176 | + * @since 1.0.0-alpha3 |
| 177 | + * |
| 178 | + * @param \PHP_CodeSniffer\Files\File|null $phpcsFile Optional. The current file being processed. |
| 179 | + * |
| 180 | + * @return string Encoding. Defaults to the PHPCS native default, which is 'utf-8' |
| 181 | + * for PHPCS 3.x and was 'iso-8859-1' for PHPCS 2.x. |
| 182 | + */ |
| 183 | + public static function getEncoding(File $phpcsFile = null) |
| 184 | + { |
| 185 | + static $default; |
| 186 | + |
| 187 | + if (isset($default) === false) { |
| 188 | + $default = 'utf-8'; |
| 189 | + if (\version_compare(self::getVersion(), '2.99.99', '<=') === true) { |
| 190 | + // In PHPCS 2.x, the default encoding is `iso-8859-1`. |
| 191 | + $default = 'iso-8859-1'; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + if ($phpcsFile instanceof File) { |
| 196 | + // Most reliable. |
| 197 | + $encoding = self::getCommandLineData($phpcsFile, 'encoding'); |
| 198 | + if ($encoding === null) { |
| 199 | + $encoding = $default; |
| 200 | + } |
| 201 | + |
| 202 | + return $encoding; |
| 203 | + } |
| 204 | + |
| 205 | + // Less reliable. |
| 206 | + $encoding = self::getConfigData('encoding'); |
| 207 | + if ($encoding === null) { |
| 208 | + $encoding = $default; |
| 209 | + } |
| 210 | + |
| 211 | + return $encoding; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Check whether the "--ignore-annotations" option is in effect. |
| 216 | + * |
| 217 | + * @since 1.0.0 |
| 218 | + * |
| 219 | + * @param \PHP_CodeSniffer\Files\File|null $phpcsFile Optional. The current file being processed. |
| 220 | + * |
| 221 | + * @return bool `TRUE` if annotations should be ignored, `FALSE` otherwise. |
| 222 | + */ |
| 223 | + public static function ignoreAnnotations(File $phpcsFile = null) |
| 224 | + { |
| 225 | + if (\class_exists('\PHP_CodeSniffer\Config') === false) { |
| 226 | + // PHPCS 2.x does not support `--ignore-annotations`. |
| 227 | + return false; |
| 228 | + } |
| 229 | + |
| 230 | + // PHPCS 3.x. |
| 231 | + if (isset($phpcsFile, $phpcsFile->config->annotations)) { |
| 232 | + return ! $phpcsFile->config->annotations; |
| 233 | + } |
| 234 | + |
| 235 | + $annotations = \PHP_CodeSniffer\Config::getConfigData('annotations'); |
| 236 | + if (isset($annotations)) { |
| 237 | + return ! $annotations; |
| 238 | + } |
| 239 | + |
| 240 | + return false; |
| 241 | + } |
| 242 | +} |
0 commit comments