Skip to content

fix(phpcs): Remove support for CSS and JS files, remove deprecated MySource.Debug.DebugCode sniff, rename deprecated Squiz.WhiteSpace.LanguageConstructSpacing sniff #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

- name: Run Cspell
if: ${{ matrix.extra-tests == '1' }}
uses: streetsidesoftware/cspell-action@v6
uses: streetsidesoftware/cspell-action@v7
with:
incremental_files_only: false

Expand Down
141 changes: 50 additions & 91 deletions coder_sniffer/Drupal/Sniffs/CSS/ClassDefinitionNameSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Ensure there are no blank lines between the names of classes/IDs. Copied from
* \PHP_CodeSniffer\Standards\Squiz\Sniffs\CSS\ClassDefinitionNameSpacingSniff
* because we also check for comma separated selectors on their own line.
* Disabled sniff. Previously ensured that there are no blank lines between the
* names of classes/IDs.
*
* We cannot implement DeprecatedSniff here because that would show deprecation
* messages to Coder users although they cannot fix them.
*
* @deprecated in Coder 8.3.30 and will be removed in Coder 9.0.0. Checking CSS
* coding standards is not supported anymore, use Stylelint instead with the
* Drupal core .stylelintrc.json configuration file.
* @see https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json
*
* @category PHP
* @package PHP_CodeSniffer
Expand All @@ -25,13 +31,6 @@
class ClassDefinitionNameSpacingSniff implements Sniff
{

/**
* A list of tokenizers this sniff supports.
*
* @var array<string>
*/
public $supportedTokenizers = ['CSS'];


/**
* Returns the token types that this sniff is interested in.
Expand All @@ -40,7 +39,7 @@ class ClassDefinitionNameSpacingSniff implements Sniff
*/
public function register()
{
return [T_OPEN_CURLY_BRACKET];
return [T_OPEN_TAG];

}//end register()

Expand All @@ -52,90 +51,50 @@ public function register()
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Do not check nested style definitions as, for example, in @media style rules.
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
if ($nested !== false) {
return;
}

// Find the first blank line before this opening brace, unless we get
// to another style definition, comment or the start of the file.
$endTokens = [
T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
T_OPEN_TAG => T_OPEN_TAG,
];
$endTokens += Tokens::$commentTokens;

$foundContent = false;
$currentLine = $tokens[$stackPtr]['line'];
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
if (isset($endTokens[$tokens[$i]['code']]) === true) {
break;
}

// A comma must be followed by a new line character.
if ($tokens[$i]['code'] === T_COMMA
&& strpos($tokens[($i + 1)]['content'], $phpcsFile->eolChar) === false
) {
$error = 'Multiple selectors should each be on a single line';
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'MultipleSelectors');
if ($fix === true) {
$phpcsFile->fixer->addNewline($i);
}
}

// Selectors must be on the same line.
if ($tokens[$i]['code'] === T_WHITESPACE
&& strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false
&& isset($endTokens[$tokens[($i - 1)]['code']]) === false
&& in_array($tokens[($i - 1)]['code'], [T_WHITESPACE, T_COMMA]) === false
) {
$error = 'Selectors must be on a single line';
// cspell:ignore SeletorSingleLine
$fix = $phpcsFile->addFixableError($error, $i, 'SeletorSingleLine');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($i, str_replace($phpcsFile->eolChar, ' ', $tokens[$i]['content']));
}
}

if ($tokens[$i]['line'] === $currentLine) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$foundContent = true;
}

continue;
}

// We changed lines.
if ($foundContent === false) {
// Before we throw an error, make sure we are not looking
// at a gap before the style definition.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
if ($prev !== false
&& isset($endTokens[$tokens[$prev]['code']]) === false
) {
$error = 'Blank lines are not allowed between class names';
$fix = $phpcsFile->addFixableError($error, ($i + 1), 'BlankLinesFound');
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($i + 1), '');
}
}

break;
}

$foundContent = false;
$currentLine = $tokens[$i]['line'];
}//end for
// This sniff is deprecated and disabled - do nothing.
return ($phpcsFile->numTokens + 1);

}//end process()


/**
* {@inheritdoc}
*
* @return string
*/
public function getDeprecationVersion(): string
{
return 'Coder 8.3.30';

}//end getDeprecationVersion()


/**
* {@inheritdoc}
*
* @return string
*/
public function getRemovalVersion(): string
{
return 'Coder 9.0.0';

}//end getRemovalVersion()


/**
* {@inheritdoc}
*
* @return string
*/
public function getDeprecationMessage(): string
{
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';

}//end getDeprecationMessage()


}//end class
74 changes: 48 additions & 26 deletions coder_sniffer/Drupal/Sniffs/CSS/ColourDefinitionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* \Drupal\Sniffs\CSS\ColourDefinitionSniff.
* Disabled sniff. Previously ensured that colors are defined in lower-case.
*
* We cannot implement DeprecatedSniff here because that would show deprecation
* messages to Coder users although they cannot fix them.
*
* Ensure colors are defined in lower-case.
* @deprecated in Coder 8.3.30 and will be removed in Coder 9.0.0. Checking CSS
* coding standards is not supported anymore, use Stylelint instead with the
* Drupal core .stylelintrc.json configuration file.
* @see https://git.drupalcode.org/project/drupal/-/blob/11.x/core/.stylelintrc.json
*
* @category PHP
* @package PHP_CodeSniffer
Expand All @@ -24,13 +30,6 @@
class ColourDefinitionSniff implements Sniff
{

/**
* A list of tokenizers this sniff supports.
*
* @var array<string>
*/
public $supportedTokenizers = ['CSS'];


/**
* Returns the token types that this sniff is interested in.
Expand All @@ -39,7 +38,7 @@ class ColourDefinitionSniff implements Sniff
*/
public function register()
{
return [T_COLOUR];
return [T_OPEN_TAG];

}//end register()

Expand All @@ -51,27 +50,50 @@ public function register()
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$color = $tokens[$stackPtr]['content'];

$expected = strtolower($color);
if ($color !== $expected) {
$error = 'CSS colors must be defined in lowercase; expected %s but found %s';
$data = [
$expected,
$color,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotLower', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
}
// This sniff is deprecated and disabled - do nothing.
return ($phpcsFile->numTokens + 1);

}//end process()


/**
* {@inheritdoc}
*
* @return string
*/
public function getDeprecationVersion(): string
{
return 'Coder 8.3.30';

}//end getDeprecationVersion()


/**
* {@inheritdoc}
*
* @return string
*/
public function getRemovalVersion(): string
{
return 'Coder 9.0.0';

}//end getRemovalVersion()


/**
* {@inheritdoc}
*
* @return string
*/
public function getDeprecationMessage(): string
{
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';

}//end getDeprecationMessage()


}//end class
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public function process(File $phpcsFile, $stackPtr)

// We are only interested in function/class/interface doc block comments.
$ignore = Tokens::$emptyTokens;
if ($phpcsFile->tokenizerType === 'JS') {
$ignore[] = T_EQUAL;
$ignore[] = T_STRING;
$ignore[] = T_OBJECT_OPERATOR;
}

$nextToken = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true);
$ignore = [
Expand Down
12 changes: 1 addition & 11 deletions coder_sniffer/Drupal/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public function process(File $phpcsFile, $stackPtr)
}

// The first line of the comment should just be the /** code.
// In JSDoc there are cases with @lends that are on the same line as code.
if ($tokens[$short]['line'] === $tokens[$stackPtr]['line'] && $phpcsFile->tokenizerType !== 'JS') {
if ($tokens[$short]['line'] === $tokens[$stackPtr]['line']) {
$error = 'The open comment tag must be the only content on the line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen');
if ($fix === true) {
Expand Down Expand Up @@ -137,12 +136,6 @@ public function process(File $phpcsFile, $stackPtr)

// Check for a comment description.
if ($tokens[$short]['code'] !== T_DOC_COMMENT_STRING) {
// JSDoc has many cases of @type declaration that don't have a
// description.
if ($phpcsFile->tokenizerType === 'JS') {
return;
}

// PHPUnit test methods are allowed to skip the short description and
// only provide an @covers annotation.
if ($tokens[$short]['content'] === '@covers') {
Expand Down Expand Up @@ -528,9 +521,6 @@ public function process(File $phpcsFile, $stackPtr)
// of @code, @todo and link tags.
if ($paramGroupid !== null && $paramGroupid !== 0
&& in_array($tokens[$tokens[$commentStart]['comment_tags'][0]]['content'], ['@code', '@todo', '@link', '@endlink', '@codingStandardsIgnoreStart']) === false
// In JSDoc we can have many other valid tags like @function or
// tags like @constructor before the param tags.
&& $phpcsFile->tokenizerType !== 'JS'
) {
$error = 'Parameter tags must be defined first in a doc comment';
$phpcsFile->addError($error, $tagGroups[$paramGroupid][0], 'ParamNotFirst');
Expand Down
30 changes: 1 addition & 29 deletions coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,6 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($phpcsFile->tokenizerType === 'JS') {
// We allow block comments if a function or object
// is being assigned to a variable.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_EQUAL;
$ignore[] = T_STRING;
$ignore[] = T_OBJECT_OPERATOR;
$nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
if ($tokens[$nextToken]['code'] === T_FUNCTION
|| $tokens[$nextToken]['code'] === T_CLOSURE
|| $tokens[$nextToken]['code'] === T_OBJECT
|| $tokens[$nextToken]['code'] === T_PROTOTYPE
) {
return;
}
}

$prevToken = $phpcsFile->findPrevious(
Tokens::$emptyTokens,
($stackPtr - 1),
Expand All @@ -127,8 +110,7 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

// Inline doc blocks are allowed in JSDoc.
if ($tokens[$stackPtr]['content'] === '/**' && $phpcsFile->tokenizerType !== 'JS') {
if ($tokens[$stackPtr]['content'] === '/**') {
// The only exception to inline doc blocks is the /** @var */
// declaration. Allow that in any form.
$varTag = $phpcsFile->findNext([T_DOC_COMMENT_TAG], ($stackPtr + 1), $tokens[$stackPtr]['comment_closer'], false, '@var');
Expand All @@ -155,16 +137,6 @@ public function process(File $phpcsFile, $stackPtr)
if ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}

// Special case for JS files.
if ($tokens[$previousContent]['code'] === T_COMMA
|| $tokens[$previousContent]['code'] === T_SEMICOLON
) {
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true);
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
}
}

// Only want inline comments.
Expand Down
Loading