Skip to content

Commit 1d08b05

Browse files
committed
Allow ignore statements in doc comments
1 parent bbd9a76 commit 1d08b05

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

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';

tests/Drupal/good/good.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,3 +1954,35 @@ public function test(array $options = ['pretty-format' => TRUE]): void {
19541954
function phpcs_ignore_comment() {
19551955

19561956
}
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)