Skip to content

Commit f4a8132

Browse files
[Imported] AC-1902: Remove PHPCSUtils dependency from coding standard (magento#135)
* AC-1902: Remove PHPCSUtils dependency from coding standard
1 parent 68eadc2 commit f4a8132

29 files changed

+3414
-171
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\Util\Tokens;
14+
15+
class BCTokens
16+
{
17+
/**
18+
* Tokens that open class and object scopes.
19+
*
20+
* @since 1.0.0
21+
* @since 1.0.0-alpha3 Visibility changed from `protected` to `private`.
22+
*
23+
* @var array <int|string> => <int|string>
24+
*/
25+
private static $ooScopeTokens = [
26+
\T_CLASS => \T_CLASS,
27+
\T_ANON_CLASS => \T_ANON_CLASS,
28+
\T_INTERFACE => \T_INTERFACE,
29+
\T_TRAIT => \T_TRAIT,
30+
];
31+
32+
/**
33+
* Tokens that open class and object scopes.
34+
*
35+
* Retrieve the OO scope tokens array in a cross-version compatible manner.
36+
*
37+
* Changelog for the PHPCS native array:
38+
* - Introduced in PHPCS 3.1.0.
39+
*
40+
* @see \PHP_CodeSniffer\Util\Tokens::$ooScopeTokens Original array.
41+
*
42+
* @since 1.0.0
43+
*
44+
* @return array <int|string> => <int|string> Token array.
45+
*/
46+
public static function ooScopeTokens()
47+
{
48+
return self::$ooScopeTokens;
49+
}
50+
51+
/**
52+
* Tokens that represent arithmetic operators.
53+
*
54+
* Retrieve the PHPCS arithmetic tokens array in a cross-version compatible manner.
55+
*
56+
* Changelog for the PHPCS native array:
57+
* - Introduced in PHPCS 0.5.0.
58+
* - PHPCS 2.9.0: The PHP 5.6 `T_POW` token was added to the array.
59+
* The `T_POW` token was introduced in PHPCS 2.4.0.
60+
*
61+
* @see \PHP_CodeSniffer\Util\Tokens::$arithmeticTokens Original array.
62+
*
63+
* @since 1.0.0
64+
*
65+
* @return array <int|string> => <int|string> Token array or an empty array for PHPCS versions in
66+
* which the PHPCS native comment tokens did not exist yet.
67+
*/
68+
public static function arithmeticTokens()
69+
{
70+
return Tokens::$arithmeticTokens + [\T_POW => \T_POW];
71+
}
72+
73+
/**
74+
* Tokens that represent assignment operators.
75+
*
76+
* Retrieve the PHPCS assignment tokens array in a cross-version compatible manner.
77+
*
78+
* Changelog for the PHPCS native array:
79+
* - Introduced in PHPCS 0.0.5.
80+
* - PHPCS 2.9.0: The PHP 7.4 `T_COALESCE_EQUAL` token was added to the array.
81+
* The `T_COALESCE_EQUAL` token was introduced in PHPCS 2.8.1.
82+
* - PHPCS 3.2.0: The JS `T_ZSR_EQUAL` token was added to the array.
83+
* The `T_ZSR_EQUAL` token was introduced in PHPCS 2.8.0.
84+
*
85+
* @see \PHP_CodeSniffer\Util\Tokens::$assignmentTokens Original array.
86+
*
87+
* @since 1.0.0
88+
*
89+
* @return array <int|string> => <int|string> Token array.
90+
*/
91+
public static function assignmentTokens()
92+
{
93+
$tokens = Tokens::$assignmentTokens;
94+
95+
/*
96+
* The `T_COALESCE_EQUAL` token may be available pre-PHPCS 2.8.1 depending on
97+
* the PHP version used to run PHPCS.
98+
*/
99+
if (\defined('T_COALESCE_EQUAL')) {
100+
$tokens[\T_COALESCE_EQUAL] = \T_COALESCE_EQUAL;
101+
}
102+
103+
if (\defined('T_ZSR_EQUAL')) {
104+
$tokens[\T_ZSR_EQUAL] = \T_ZSR_EQUAL;
105+
}
106+
107+
return $tokens;
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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

Comments
 (0)