Skip to content

Commit ef25bb5

Browse files
committed
deprecate sniffs instead of removing
1 parent d15b919 commit ef25bb5

File tree

4 files changed

+358
-3
lines changed

4 files changed

+358
-3
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
/**
3+
* \Drupal\Sniffs\CSS\ClassDefinitionNameSpacingSniff.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
10+
namespace Drupal\Sniffs\CSS;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
14+
use PHP_CodeSniffer\Sniffs\Sniff;
15+
use PHP_CodeSniffer\Util\Tokens;
16+
17+
/**
18+
* Ensure there are no blank lines between the names of classes/IDs. Copied from
19+
* \PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS\ClassDefinitionNameSpacingSniff
20+
* because we also check for comma separated selectors on their own line.
21+
*
22+
* @deprecated in Coder 8.3.30 and will be removed in Coder 9.0.0. Checking CSS
23+
* coding standards is not supported anymore, use Stylelint instead with the
24+
* Drupal core .stylelintrc.json configuration file.
25+
* @see https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json
26+
*
27+
* @category PHP
28+
* @package PHP_CodeSniffer
29+
* @link http://pear.php.net/package/PHP_CodeSniffer
30+
*/
31+
class ClassDefinitionNameSpacingSniff implements Sniff, DeprecatedSniff
32+
{
33+
34+
/**
35+
* A list of tokenizers this sniff supports.
36+
*
37+
* @var array<string>
38+
*/
39+
public $supportedTokenizers = ['CSS'];
40+
41+
42+
/**
43+
* Returns the token types that this sniff is interested in.
44+
*
45+
* @return array<int, int|string>
46+
*/
47+
public function register()
48+
{
49+
return [T_OPEN_CURLY_BRACKET];
50+
51+
}//end register()
52+
53+
54+
/**
55+
* Processes the tokens that this sniff is interested in.
56+
*
57+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
58+
* @param int $stackPtr The position in the stack where
59+
* the token was found.
60+
*
61+
* @return void
62+
*/
63+
public function process(File $phpcsFile, $stackPtr)
64+
{
65+
$tokens = $phpcsFile->getTokens();
66+
67+
// Do not check nested style definitions as, for example, in @media style rules.
68+
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
69+
if ($nested !== false) {
70+
return;
71+
}
72+
73+
// Find the first blank line before this opening brace, unless we get
74+
// to another style definition, comment or the start of the file.
75+
$endTokens = [
76+
T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
77+
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
78+
T_OPEN_TAG => T_OPEN_TAG,
79+
];
80+
$endTokens += Tokens::$commentTokens;
81+
82+
$foundContent = false;
83+
$currentLine = $tokens[$stackPtr]['line'];
84+
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
85+
if (isset($endTokens[$tokens[$i]['code']]) === true) {
86+
break;
87+
}
88+
89+
// A comma must be followed by a new line character.
90+
if ($tokens[$i]['code'] === T_COMMA
91+
&& strpos($tokens[($i + 1)]['content'], $phpcsFile->eolChar) === false
92+
) {
93+
$error = 'Multiple selectors should each be on a single line';
94+
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'MultipleSelectors');
95+
if ($fix === true) {
96+
$phpcsFile->fixer->addNewline($i);
97+
}
98+
}
99+
100+
// Selectors must be on the same line.
101+
if ($tokens[$i]['code'] === T_WHITESPACE
102+
&& strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false
103+
&& isset($endTokens[$tokens[($i - 1)]['code']]) === false
104+
&& in_array($tokens[($i - 1)]['code'], [T_WHITESPACE, T_COMMA]) === false
105+
) {
106+
$error = 'Selectors must be on a single line';
107+
// cspell:ignore SeletorSingleLine
108+
$fix = $phpcsFile->addFixableError($error, $i, 'SeletorSingleLine');
109+
if ($fix === true) {
110+
$phpcsFile->fixer->replaceToken($i, str_replace($phpcsFile->eolChar, ' ', $tokens[$i]['content']));
111+
}
112+
}
113+
114+
if ($tokens[$i]['line'] === $currentLine) {
115+
if ($tokens[$i]['code'] !== T_WHITESPACE) {
116+
$foundContent = true;
117+
}
118+
119+
continue;
120+
}
121+
122+
// We changed lines.
123+
if ($foundContent === false) {
124+
// Before we throw an error, make sure we are not looking
125+
// at a gap before the style definition.
126+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
127+
if ($prev !== false
128+
&& isset($endTokens[$tokens[$prev]['code']]) === false
129+
) {
130+
$error = 'Blank lines are not allowed between class names';
131+
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'BlankLinesFound');
132+
if ($fix === true) {
133+
$phpcsFile->fixer->replaceToken(($i + 1), '');
134+
}
135+
}
136+
137+
break;
138+
}
139+
140+
$foundContent = false;
141+
$currentLine = $tokens[$i]['line'];
142+
}//end for
143+
144+
}//end process()
145+
146+
147+
/**
148+
* {@inheritdoc}
149+
*
150+
* @return string
151+
*/
152+
public function getDeprecationVersion(): string
153+
{
154+
return 'Coder 8.3.30';
155+
156+
}//end getDeprecationVersion()
157+
158+
159+
/**
160+
* {@inheritdoc}
161+
*
162+
* @return string
163+
*/
164+
public function getRemovalVersion(): string
165+
{
166+
return 'Coder 9.0.0';
167+
168+
}//end getRemovalVersion()
169+
170+
171+
/**
172+
* {@inheritdoc}
173+
*
174+
* @return string
175+
*/
176+
public function getDeprecationMessage(): string
177+
{
178+
return 'Checking CSS coding standards is not supported anymore, use Stylelint instead with the Drupal core .stylelintrc.json configuration file. https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json';
179+
180+
}//end getDeprecationMessage()
181+
182+
183+
}//end class
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* \Drupal\Sniffs\CSS\ColourDefinitionSniff.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
10+
namespace Drupal\Sniffs\CSS;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
14+
use PHP_CodeSniffer\Sniffs\Sniff;
15+
16+
/**
17+
* \Drupal\Sniffs\CSS\ColourDefinitionSniff.
18+
*
19+
* Ensure colors are defined in lower-case.
20+
*
21+
* @deprecated in Coder 8.3.30 and will be removed in Coder 9.0.0. Checking CSS
22+
* coding standards is not supported anymore, use Stylelint instead with the
23+
* Drupal core .stylelintrc.json configuration file.
24+
* @see https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json
25+
*
26+
* @category PHP
27+
* @package PHP_CodeSniffer
28+
* @link http://pear.php.net/package/PHP_CodeSniffer
29+
*/
30+
class ColourDefinitionSniff implements Sniff, DeprecatedSniff
31+
{
32+
33+
/**
34+
* A list of tokenizers this sniff supports.
35+
*
36+
* @var array<string>
37+
*/
38+
public $supportedTokenizers = ['CSS'];
39+
40+
41+
/**
42+
* Returns the token types that this sniff is interested in.
43+
*
44+
* @return array<int|string>
45+
*/
46+
public function register()
47+
{
48+
return [T_COLOUR];
49+
50+
}//end register()
51+
52+
53+
/**
54+
* Processes the tokens that this sniff is interested in.
55+
*
56+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
57+
* @param int $stackPtr The position in the stack where
58+
* the token was found.
59+
*
60+
* @return void
61+
*/
62+
public function process(File $phpcsFile, $stackPtr)
63+
{
64+
$tokens = $phpcsFile->getTokens();
65+
$color = $tokens[$stackPtr]['content'];
66+
67+
$expected = strtolower($color);
68+
if ($color !== $expected) {
69+
$error = 'CSS colors must be defined in lowercase; expected %s but found %s';
70+
$data = [
71+
$expected,
72+
$color,
73+
];
74+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotLower', $data);
75+
if ($fix === true) {
76+
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
77+
}
78+
}
79+
80+
}//end process()
81+
82+
83+
/**
84+
* {@inheritdoc}
85+
*
86+
* @return string
87+
*/
88+
public function getDeprecationVersion(): string
89+
{
90+
return 'Coder 8.3.30';
91+
92+
}//end getDeprecationVersion()
93+
94+
95+
/**
96+
* {@inheritdoc}
97+
*
98+
* @return string
99+
*/
100+
public function getRemovalVersion(): string
101+
{
102+
return 'Coder 9.0.0';
103+
104+
}//end getRemovalVersion()
105+
106+
107+
/**
108+
* {@inheritdoc}
109+
*
110+
* @return string
111+
*/
112+
public function getDeprecationMessage(): string
113+
{
114+
return 'Checking CSS coding standards is not supported anymore, use Stylelint instead with the Drupal core .stylelintrc.json configuration file. https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json';
115+
116+
}//end getDeprecationMessage()
117+
118+
119+
}//end class

coder_sniffer/Drupal/Sniffs/Strings/UnnecessaryStringConcatSniff.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99

1010
namespace Drupal\Sniffs\Strings;
1111

12+
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
1213
use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff as GenericUnnecessaryStringConcatSniff;
1314

1415
/**
1516
* Checks that two strings are not concatenated together; suggests using one string instead.
1617
*
17-
* @todo Remove in Coder 9.0.0 and replace with GenericUnnecessaryStringConcatSniff.
18+
* @deprecated in Coder 8.3.30 and will be removed in Coder 9.0.0. Use
19+
* Generic.Strings.UnnecessaryStringConcat instead.
1820
*
1921
* @category PHP
2022
* @package PHP_CodeSniffer
2123
* @link http://pear.php.net/package/PHP_CodeSniffer
2224
*/
23-
class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff
25+
class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff implements DeprecatedSniff
2426
{
2527

2628

@@ -35,4 +37,40 @@ class UnnecessaryStringConcatSniff extends GenericUnnecessaryStringConcatSniff
3537
public $allowMultiline = true;
3638

3739

40+
/**
41+
* {@inheritdoc}
42+
*
43+
* @return string
44+
*/
45+
public function getDeprecationVersion(): string
46+
{
47+
return 'Coder 8.3.30';
48+
49+
}//end getDeprecationVersion()
50+
51+
52+
/**
53+
* {@inheritdoc}
54+
*
55+
* @return string
56+
*/
57+
public function getRemovalVersion(): string
58+
{
59+
return 'Coder 9.0.0';
60+
61+
}//end getRemovalVersion()
62+
63+
64+
/**
65+
* {@inheritdoc}
66+
*
67+
* @return string
68+
*/
69+
public function getDeprecationMessage(): string
70+
{
71+
return 'The custom UnnecessaryStringConcatSniff is deprecated and will be removed in Coder 9.0.0. Use Generic.Strings.UnnecessaryStringConcat instead.';
72+
73+
}//end getDeprecationMessage()
74+
75+
3876
}//end class

0 commit comments

Comments
 (0)