Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassAnnotationStructureSniff.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
8 
9 use PHP_CodeSniffer\Sniffs\Sniff;
10 use PHP_CodeSniffer\Files\File;
11 
15 class ClassAnnotationStructureSniff implements Sniff
16 {
20  private $annotationFormatValidator;
21 
25  public function register()
26  {
27  return [
28  T_CLASS
29  ];
30  }
31 
35  public function __construct()
36  {
37  $this->annotationFormatValidator = new AnnotationFormatValidator();
38  }
39 
47  private function validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
48  File $phpcsFile,
49  int $previousCommentClosePtr,
50  int $stackPtr
51  ) : void {
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']
56  ) {
57  $error = 'Interface or abstract class is missing annotation block';
58  $phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
59  }
60  if ($tokens[$stackPtr - 2]['type'] === 'T_FINAL' &&
61  $tokens[$stackPtr - 4]['content'] != $tokens[$previousCommentClosePtr]['content']
62  ) {
63  $error = 'Final class is missing annotation block';
64  $phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
65  }
66  }
67  }
68 
76  private function validateAnnotationBlockExists(File $phpcsFile, int $previousCommentClosePtr, int $stackPtr) : void
77  {
78  $tokens = $phpcsFile->getTokens();
79  $this->validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists(
80  $phpcsFile,
81  $previousCommentClosePtr,
82  $stackPtr
83  );
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']
87  ) {
88  $error = 'Class is missing annotation block';
89  $phpcsFile->addFixableError($error, $stackPtr, 'ClassAnnotation');
90  }
91  }
92 
96  public function process(File $phpcsFile, $stackPtr)
97  {
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'];
103  $emptyTypeTokens = [
104  T_DOC_COMMENT_WHITESPACE,
105  T_DOC_COMMENT_STAR
106  ];
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');
111  } else {
112  $this->annotationFormatValidator->validateDescriptionFormatStructure(
113  $phpcsFile,
114  $commentStartPtr,
115  (int) $shortPtr,
116  $previousCommentClosePtr,
117  $emptyTypeTokens
118  );
119  }
120  }
121 }
$tokens
Definition: cards_list.phtml:9