|
| 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 | +} |
0 commit comments