Skip to content

Commit 5e2f42d

Browse files
authored
Feat - Custom sniff for checking spaces after @since & @version doc tags. (#23)
* This sniff makes sure that `@since` and `@version` doc tags always have only 1 space after them. ![Screenshot 2025-02-15 at 4 34 45 AM](https://github.com/user-attachments/assets/64485aa4-66b0-4b34-bad0-2661f6df530b)
2 parents bdf05c1 + 0ce0d88 commit 5e2f42d

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace StellarWP\Sniffs\Whitespace;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File;
7+
8+
/**
9+
* Class DocCommentSpacingSniff
10+
*
11+
* @package StellarWP\Sniffs\Whitespace
12+
*/
13+
final class DocCommentSpacingSniff implements Sniff {
14+
/**
15+
* Register the sniff.
16+
*
17+
* @return array<string>
18+
*/
19+
public function register() {
20+
return [ T_DOC_COMMENT_TAG ];
21+
}
22+
23+
/**
24+
* Process the doc comment.
25+
*
26+
* @param File $phpcsFile The file being scanned.
27+
* @param int $stackPtr The position of the current token in the stack.
28+
*/
29+
public function process( File $phpcsFile, $stackPtr ) {
30+
$tokens = $phpcsFile->getTokens();
31+
$full_tag = $tokens[ $stackPtr ]['content'];
32+
33+
// Check if @since or @version is missing space.
34+
if ( preg_match( '/@(?:since|version)(\S+)/', $full_tag, $matches, PREG_OFFSET_CAPTURE ) ) {
35+
$error = 'There should be exactly one space after the %s tag.';
36+
$version = $matches[1][0];
37+
$tag = strstr( $full_tag, $version, true );
38+
$data = [ $tag ];
39+
$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'MissingSpace', $data );
40+
41+
if ( $fix ) {
42+
$this->fixSpacing( $phpcsFile, $stackPtr, $tag, $version );
43+
}
44+
}
45+
46+
// Check for the extra whitespace after @since or @version.
47+
$next_token = $tokens[ $stackPtr + 1 ];
48+
49+
if ( $next_token["type"] !== "T_DOC_COMMENT_WHITESPACE" ) {
50+
return;
51+
}
52+
53+
$whitespace = $next_token['content'];
54+
55+
// Check if @since or @version is followed by more than one space
56+
if ( strlen( $whitespace ) > 1 ) {
57+
$error = 'There should be exactly one space after the %s tag, not multiple.';
58+
$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ExtraSpaces', $full_tag );
59+
60+
if ( $fix ) {
61+
$this->fixMultipleSpaces( $phpcsFile, $stackPtr + 1 );
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Fix the spacing issue in the doc comment.
68+
*
69+
* @param File $phpcsFile The file being scanned.
70+
* @param int $stackPtr The position of the current token in the stack.
71+
* @param string $tag The tag name.
72+
* @param string $version The version number.
73+
*/
74+
private function fixSpacing( File $phpcsFile, $stackPtr, $tag, $version ) {
75+
$correctedComment = sprintf( '%s %s', $tag, $version );
76+
77+
$phpcsFile->fixer->beginChangeset();
78+
$phpcsFile->fixer->replaceToken( $stackPtr, $correctedComment );
79+
$phpcsFile->fixer->endChangeset();
80+
}
81+
82+
/**
83+
* Fix the multiple space trails in the doc comment.
84+
*
85+
* @param File $phpcsFile The file being scanned.
86+
* @param int $stackPtr The position of the current token in the stack.
87+
*/
88+
private function fixMultipleSpaces( File $phpcsFile, $stackPtr ) {
89+
$phpcsFile->fixer->beginChangeset();
90+
$phpcsFile->fixer->replaceToken( $stackPtr, " " );
91+
$phpcsFile->fixer->endChangeset();
92+
}
93+
}

StellarWP/ruleset.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
9292
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>
9393

94+
<!-- Check for proper spacing in comment tags -->
95+
<rule ref="StellarWP.Whitespace.DocCommentSpacing"/>
96+
9497
<!-- Don't check test-generated files. -->
9598
<exclude-pattern>*/tests/_support/_generated/*</exclude-pattern>
9699

0 commit comments

Comments
 (0)