6 declare(strict_types=1);
9 use PHP_CodeSniffer\Sniffs\Sniff;
10 use PHP_CodeSniffer\Files\File;
20 private $annotationFormatValidator;
25 public function register()
47 private function validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
49 int $previousCommentClosePtr,
52 $tokens = $phpcsFile->getTokens();
53 if (
$tokens[$stackPtr][
'type'] ===
'T_CLASS') {
54 if (
$tokens[$stackPtr - 2][
'type'] ===
'T_ABSTRACT' &&
55 $tokens[$stackPtr - 4][
'content'] !=
$tokens[$previousCommentClosePtr][
'content']
57 $error =
'Interface or abstract class is missing annotation block';
58 $phpcsFile->addFixableError($error, $stackPtr,
'ClassAnnotation');
60 if (
$tokens[$stackPtr - 2][
'type'] ===
'T_FINAL' &&
61 $tokens[$stackPtr - 4][
'content'] !=
$tokens[$previousCommentClosePtr][
'content']
63 $error =
'Final class is missing annotation block';
64 $phpcsFile->addFixableError($error, $stackPtr,
'ClassAnnotation');
76 private function validateAnnotationBlockExists(File $phpcsFile,
int $previousCommentClosePtr,
int $stackPtr) : void
78 $tokens = $phpcsFile->getTokens();
79 $this->validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
81 $previousCommentClosePtr,
84 if (
$tokens[$stackPtr - 2][
'content'] !=
'class' &&
$tokens[$stackPtr - 2][
'content'] !=
'abstract' 85 &&
$tokens[$stackPtr - 2][
'content'] !=
'final' 86 &&
$tokens[$stackPtr - 2][
'content'] !==
$tokens[$previousCommentClosePtr][
'content']
88 $error =
'Class is missing annotation block';
89 $phpcsFile->addFixableError($error, $stackPtr,
'ClassAnnotation');
96 public function process(File $phpcsFile, $stackPtr)
98 $tokens = $phpcsFile->getTokens();
99 $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0);
100 $this->validateAnnotationBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr);
101 $commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
102 $commentCloserPtr =
$tokens[$commentStartPtr][
'comment_closer'];
104 T_DOC_COMMENT_WHITESPACE,
107 $shortPtr = $phpcsFile->findNext($emptyTypeTokens, $commentStartPtr +1, $commentCloserPtr,
true);
108 if ($shortPtr ===
false) {
109 $error =
'Annotation block is empty';
110 $phpcsFile->addError($error, $commentStartPtr,
'MethodAnnotation');
112 $this->annotationFormatValidator->validateDescriptionFormatStructure(
116 $previousCommentClosePtr,
process(File $phpcsFile, $stackPtr)