Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BracesFormattingSniff.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 use PHP_CodeSniffer\Util\Tokens;
11 
20 class BracesFormattingSniff implements Sniff
21 {
28 
32  public function register()
33  {
34  return [T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET];
35  }
36 
40  public function process(File $phpcsFile, $stackPtr)
41  {
42  $tokens = $phpcsFile->getTokens();
43 
44  if (T_OPEN_CURLY_BRACKET === $tokens[$stackPtr]['code']) {
45  if (TokenizerSymbolsInterface::WHITESPACE !== $tokens[$stackPtr - 1]['content']) {
46  $phpcsFile->addError('Space before opening brace is missing', $stackPtr, 'SpacingBeforeOpen');
47  }
48 
49  return;
50  }
51 
52  $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
53  if ($next === false) {
54  return;
55  }
56 
57  if (!in_array($tokens[$next]['code'], [T_CLOSE_TAG, T_CLOSE_CURLY_BRACKET])) {
58  $found = (($tokens[$next]['line'] - $tokens[$stackPtr]['line']) - 1);
59  if ($found !== 1) {
60  $error = 'Expected one blank line after closing brace of class definition; %s found';
61  $data = [$found];
62  // Will be implemented in MAGETWO-49778
63  //$phpcsFile->addWarning($error, $stackPtr, 'SpacingAfterClose', $data);
64  }
65  }
66 
67  // Ignore nested style definitions from here on. The spacing before the closing brace
68  // (a single blank line) will be enforced by the above check, which ensures there is a
69  // blank line after the last nested class.
70  $found = $phpcsFile->findPrevious(
71  T_CLOSE_CURLY_BRACKET,
72  ($stackPtr - 1),
73  $tokens[$stackPtr]['bracket_opener']
74  );
75 
76  if ($found !== false) {
77  return;
78  }
79 
80  $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
81  if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
82  $num = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1);
83  $error = 'Expected 0 blank lines before closing brace of class definition; %s found';
84  $data = [$num];
85  $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeClose', $data);
86  }
87  }
88 }
$tokens
Definition: cards_list.phtml:9