Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CommentLevelsSniff.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sniffs\Less;
7 
8 use PHP_CodeSniffer\Sniffs\Sniff;
9 use PHP_CodeSniffer\Files\File;
10 
21 class CommentLevelsSniff implements Sniff
22 {
23  const COMMENT_STRING = '//';
24 
25  const FIRST_LEVEL_COMMENT = '_____________________________________________';
26 
27  const SECOND_LEVEL_COMMENT = '--';
28 
32  protected $levelComments = [
33  self::FIRST_LEVEL_COMMENT => T_STRING,
34  self::SECOND_LEVEL_COMMENT => T_DEC,
35  ];
36 
43 
47  public function register()
48  {
49  return [T_STRING];
50  }
51 
55  public function process(File $phpcsFile, $stackPtr)
56  {
57  $tokens = $phpcsFile->getTokens();
58 
59  if ((T_STRING !== $tokens[$stackPtr]['code'])
60  || (self::COMMENT_STRING !== $tokens[$stackPtr]['content'])
61  || (1 === $tokens[$stackPtr]['line'])
62  ) {
63  return;
64  }
65 
66  $textInSameLine = $phpcsFile->findPrevious([T_STRING, T_STYLE], $stackPtr - 1);
67 
68  // is inline comment
69  if ((false !== $textInSameLine)
70  && ($tokens[$textInSameLine]['line'] === $tokens[$stackPtr]['line'])
71  ) {
72  $this->validateInlineComment($phpcsFile, $stackPtr, $tokens);
73  return;
74  }
75 
76  // validation of levels comments
77  if (!in_array($tokens[$stackPtr + 1]['content'], [
80  ])
81  ) {
82  $phpcsFile->addError('Level\'s comment does not have 2 spaces after "//"', $stackPtr, 'SpacesMissed');
83  }
84 
85  if (!$this->isNthLevelComment($phpcsFile, $stackPtr, $tokens)) {
86  return;
87  }
88 
89  if (!$this->checkNthLevelComment($phpcsFile, $stackPtr, $tokens)) {
90  $phpcsFile->addError(
91  'First and second level comments must be surrounded by empty lines',
92  $stackPtr,
93  'SpaceMissed'
94  );
95  }
96  }
97 
106  private function validateInlineComment(File $phpcsFile, $stackPtr, array $tokens)
107  {
108  if ($tokens[$stackPtr + 1]['content'] !== TokenizerSymbolsInterface::WHITESPACE) {
109  $phpcsFile->addError('Inline comment should have 1 space after "//"', $stackPtr, 'SpaceMissedAfter');
110  }
111  if ($tokens[$stackPtr - 1]['content'] !== TokenizerSymbolsInterface::WHITESPACE) {
112  $phpcsFile->addError('Inline comment should have 1 space before "//"', $stackPtr, 'SpaceMissedBefore');
113  }
114  }
115 
124  private function isNthLevelComment(File $phpcsFile, $stackPtr, array $tokens)
125  {
126  $nthLevelCommentFound = false;
127  $levelComment = 0;
128 
129  foreach ($this->levelComments as $code => $comment) {
130  $levelComment = $phpcsFile->findNext($comment, $stackPtr, null, false, $code);
131  if (false !== $levelComment) {
132  $nthLevelCommentFound = true;
133  break;
134  }
135  }
136 
137  if (false === $nthLevelCommentFound) {
138  return false;
139  }
140 
141  $currentLine = $tokens[$stackPtr]['line'];
142  $levelCommentLine = $tokens[$levelComment]['line'];
143 
144  if ($currentLine !== $levelCommentLine) {
145  return false;
146  }
147 
148  return true;
149  }
150 
159  private function checkNthLevelComment(File $phpcsFile, $stackPtr, array $tokens)
160  {
161  $correct = false;
162 
163  $nextLine = $phpcsFile->findNext(
164  T_WHITESPACE,
165  $stackPtr,
166  null,
167  false,
169  );
170 
171  if (false === $nextLine) {
172  return $correct;
173  }
174 
175  if (($tokens[$nextLine]['content'] !== TokenizerSymbolsInterface::NEW_LINE)
176  || ($tokens[$nextLine + 1]['content'] !== TokenizerSymbolsInterface::NEW_LINE)
177  ) {
178  return $correct;
179  }
180 
181  $commentLinePtr = $stackPtr;
182  while ($tokens[$commentLinePtr - 2]['line'] > 1) {
183  $commentLinePtr = $phpcsFile->findPrevious(T_STRING, $commentLinePtr - 1, null, false, '//');
184 
185  if (false === $commentLinePtr) {
186  continue;
187  }
188 
189  if (($tokens[$commentLinePtr - 1]['content'] === TokenizerSymbolsInterface::NEW_LINE)
190  && ($tokens[$commentLinePtr - 2]['content'] === TokenizerSymbolsInterface::NEW_LINE)
191  ) {
192  $correct = true;
193  break;
194  }
195  }
196 
197  return $correct;
198  }
199 }
$tokens
Definition: cards_list.phtml:9
$code
Definition: info.phtml:12