Skip to content

Commit 78034f0

Browse files
authored
fix(FunctionComment): Allow PHPCS ignore directives before functions and in doc blocks (#3511862)
1 parent 99b280c commit 78034f0

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ jobs:
8484
# core is updated to that version.
8585
run: |
8686
cd drupal/core
87-
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php,modules/system/tests/modules/theme_test/src/EventSubscriber/ThemeTestSubscriber.php
87+
../../vendor/bin/phpcs -p -s --exclude=Drupal.Commenting.FunctionComment --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php,modules/system/tests/modules/theme_test/src/EventSubscriber/ThemeTestSubscriber.php,modules/views/src/Plugin/views/pager/PagerPluginBase.php,lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.php

coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function process(File $phpcsFile, $stackPtr)
6363
$name = $tokens[$stackPtr]['content'];
6464
$classCodeStart = $stackPtr;
6565

66-
$previousContent = null;
6766
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
6867
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
6968
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
@@ -73,10 +72,6 @@ public function process(File $phpcsFile, $stackPtr)
7372
continue;
7473
}
7574

76-
if ($previousContent === null) {
77-
$previousContent = $commentEnd;
78-
}
79-
8075
if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
8176
&& isset($tokens[$commentEnd]['attribute_opener']) === true
8277
) {

coder_sniffer/Drupal/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
/**
1617
* Ensures doc blocks follow basic formatting.
@@ -50,9 +51,18 @@ public function register()
5051
*/
5152
public function process(File $phpcsFile, $stackPtr)
5253
{
53-
$tokens = $phpcsFile->getTokens();
54-
$commentEnd = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr + 1));
55-
$commentStart = $tokens[$commentEnd]['comment_opener'];
54+
$tokens = $phpcsFile->getTokens();
55+
56+
if (isset($tokens[$stackPtr]['comment_closer']) === false
57+
|| ($tokens[$tokens[$stackPtr]['comment_closer']]['content'] === ''
58+
&& $tokens[$stackPtr]['comment_closer'] === ($phpcsFile->numTokens - 1))
59+
) {
60+
// Don't process an unfinished comment during live coding.
61+
return;
62+
}
63+
64+
$commentStart = $stackPtr;
65+
$commentEnd = $tokens[$stackPtr]['comment_closer'];
5666

5767
$empty = [
5868
T_DOC_COMMENT_WHITESPACE,
@@ -463,15 +473,21 @@ public function process(File $phpcsFile, $stackPtr)
463473
// Check for a value. No value means no padding needed.
464474
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
465475
if ($string !== false && $tokens[$string]['line'] === $tokens[$tag]['line']) {
466-
$paddings[$tag] = strlen($tokens[($tag + 1)]['content']);
476+
$paddings[$tag] = $tokens[($tag + 1)]['length'];
467477
}
468478
}
469479

470480
// Check that there was single blank line after the tag block
471-
// but account for a multi-line tag comments.
481+
// but account for multi-line tag comments.
482+
$find = Tokens::$phpcsCommentTokens;
483+
$find[T_DOC_COMMENT_TAG] = T_DOC_COMMENT_TAG;
484+
472485
$lastTag = $group[$pos];
473-
$next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd);
474-
if ($next !== false && $tokens[$next]['column'] === $tokens[$firstTag]['column']) {
486+
$next = $phpcsFile->findNext($find, ($lastTag + 3), $commentEnd);
487+
if ($next !== false
488+
&& $tokens[$next]['column'] === $tokens[$firstTag]['column']
489+
&& in_array($tokens[$lastTag]['content'], $checkTags) === true
490+
) {
475491
$prev = $phpcsFile->findPrevious([T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING], ($next - 1), $commentStart);
476492
if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) {
477493
$error = 'There must be a single blank line after a tag group';

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ public function register()
7575
public function process(File $phpcsFile, $stackPtr)
7676
{
7777
$tokens = $phpcsFile->getTokens();
78-
$ignore = Tokens::$methodPrefixes;
78+
$ignore = (Tokens::$methodPrefixes + Tokens::$phpcsCommentTokens);
7979
$ignore[T_WHITESPACE] = T_WHITESPACE;
8080
$functionCodeStart = $stackPtr;
8181

8282
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
8383
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
84+
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
85+
$functionCodeStart = $commentEnd;
86+
}
87+
8488
continue;
8589
}
8690

tests/Drupal/good/good.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,3 +1946,43 @@ public function test(array $options = ['pretty-format' => TRUE]): void {
19461946
}
19471947

19481948
}
1949+
1950+
/**
1951+
* Doc block is here and an ignore directive is ok.
1952+
*/
1953+
// phpcs:ignore Drupal.NamingConventions.ValidClassName
1954+
function phpcs_ignore_comment() {
1955+
1956+
}
1957+
1958+
/**
1959+
* Test class.
1960+
*/
1961+
class TestPlugin {
1962+
1963+
/**
1964+
* Gets a fallback id for a missing plugin.
1965+
*
1966+
* This method should be implemented in extending classes that also implement
1967+
* FallbackPluginManagerInterface. It is called by
1968+
* PluginManagerBase::handlePluginNotFound on the abstract class, and
1969+
* therefore should be defined as well on the abstract class to prevent static
1970+
* analysis errors.
1971+
*
1972+
* @param string $plugin_id
1973+
* The ID of the missing requested plugin.
1974+
* @param array $configuration
1975+
* An array of configuration relevant to the plugin instance.
1976+
*
1977+
* phpcs:ignore Drupal.Commenting.FunctionComment.InvalidNoReturn
1978+
* @return string
1979+
* The id of an existing plugin to use when the plugin does not exist.
1980+
*
1981+
* @throws \BadMethodCallException
1982+
* If the method is not implemented in the concrete plugin manager class.
1983+
*/
1984+
protected function getFallbackPluginId($plugin_id, array $configuration = []) {
1985+
throw new \BadMethodCallException(static::class . '::getFallbackPluginId() not implemented.');
1986+
}
1987+
1988+
}

0 commit comments

Comments
 (0)