Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MethodAnnotationStructureSniff.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 MethodAnnotationStructureSniff implements Sniff
16 {
20  private $annotationFormatValidator;
21 
25  public function __construct()
26  {
27  $this->annotationFormatValidator = new AnnotationFormatValidator();
28  }
29 
33  public function register()
34  {
35  return [
36  T_FUNCTION
37  ];
38  }
39 
43  public function process(File $phpcsFile, $stackPtr)
44  {
45  $tokens = $phpcsFile->getTokens();
46  $commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($stackPtr), 0);
47  $commentEndPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr), 0);
48  $commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
49  $functionPtrContent = $tokens[$stackPtr+2]['content'] ;
50  if (preg_match('/(?i)__construct/', $functionPtrContent)) {
51  return;
52  }
53  $emptyTypeTokens = [
54  T_DOC_COMMENT_WHITESPACE,
55  T_DOC_COMMENT_STAR
56  ];
57  $shortPtr = $phpcsFile->findNext($emptyTypeTokens, $commentStartPtr + 1, $commentCloserPtr, true);
58  if ($shortPtr === false) {
59  $error = 'Annotation block is empty';
60  $phpcsFile->addError($error, $commentStartPtr, 'MethodAnnotation');
61  } else {
62  $this->annotationFormatValidator->validateDescriptionFormatStructure(
63  $phpcsFile,
64  $commentStartPtr,
65  (int) $shortPtr,
66  $commentEndPtr,
67  $emptyTypeTokens
68  );
69  if (empty($tokens[$commentStartPtr]['comment_tags'])) {
70  return;
71  }
72  $this->annotationFormatValidator->validateTagsSpacingFormat(
73  $phpcsFile,
74  $commentStartPtr,
75  $emptyTypeTokens
76  );
77  $this->annotationFormatValidator->validateTagGroupingFormat($phpcsFile, $commentStartPtr);
78  }
79  }
80 }
$tokens
Definition: cards_list.phtml:9