1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sniffs\Annotation;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Sniff to validate structure of class, interface annotations
*/
class ClassAnnotationStructureSniff implements Sniff
{
/**
* @var AnnotationFormatValidator
*/
private $annotationFormatValidator;
/**
* @inheritdoc
*/
public function register()
{
return [
T_CLASS
];
}
/**
* AnnotationStructureSniff constructor.
*/
public function __construct()
{
$this->annotationFormatValidator = new AnnotationFormatValidator();
}
/**
* Validates whether annotation block exists for interface, abstract or final classes
*
* @param File $phpcsFile
* @param int $previousCommentClosePtr
* @param int $stackPtr
*/
private function validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
File $phpcsFile,
int $previousCommentClosePtr,
int $stackPtr
) : void {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['type'] === 'T_CLASS') {
if ($tokens[$stackPtr - 2]['type'] === 'T_ABSTRACT' &&
$tokens[$stackPtr - 4]['content'] != $tokens[$previousCommentClosePtr]['content']
) {
$error = 'Interface or abstract class is missing annotation block';
$phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
}
if ($tokens[$stackPtr - 2]['type'] === 'T_FINAL' &&
$tokens[$stackPtr - 4]['content'] != $tokens[$previousCommentClosePtr]['content']
) {
$error = 'Final class is missing annotation block';
$phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
}
}
}
/**
* Validates whether annotation block exists
*
* @param File $phpcsFile
* @param int $previousCommentClosePtr
* @param int $stackPtr
*/
private function validateAnnotationBlockExists(File $phpcsFile, int $previousCommentClosePtr, int $stackPtr) : void
{
$tokens = $phpcsFile->getTokens();
$this->validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
$phpcsFile,
$previousCommentClosePtr,
$stackPtr
);
if ($tokens[$stackPtr - 2]['content'] != 'class' && $tokens[$stackPtr - 2]['content'] != 'abstract'
&& $tokens[$stackPtr - 2]['content'] != 'final'
&& $tokens[$stackPtr - 2]['content'] !== $tokens[$previousCommentClosePtr]['content']
) {
$error = 'Class is missing annotation block';
$phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
}
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0);
$this->validateAnnotationBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr);
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
$emptyTypeTokens = [
T_DOC_COMMENT_WHITESPACE,
T_DOC_COMMENT_STAR
];
$shortPtr = $phpcsFile->findNext($emptyTypeTokens, $commentStartPtr +1, $commentCloserPtr, true);
if ($shortPtr === false) {
$error = 'Annotation block is empty';
$phpcsFile->addError($error, $commentStartPtr, 'MethodAnnotation');
} else {
$this->annotationFormatValidator->validateDescriptionFormatStructure(
$phpcsFile,
$commentStartPtr,
(int) $shortPtr,
$previousCommentClosePtr,
$emptyTypeTokens
);
}
}
}