Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ColonSpacingSniff.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 ColonSpacingSniff implements Sniff
21 {
28 
32  public function register()
33  {
34  return [T_COLON];
35  }
36 
40  public function process(File $phpcsFile, $stackPtr)
41  {
42  $tokens = $phpcsFile->getTokens();
43 
44  if ($this->needValidateSpaces($phpcsFile, $stackPtr, $tokens)) {
45  $this->validateSpaces($phpcsFile, $stackPtr, $tokens);
46  }
47  }
48 
58  private function needValidateSpaces(File $phpcsFile, $stackPtr, $tokens)
59  {
60  $nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
61 
62  if (false === $nextSemicolon
63  || ($tokens[$nextSemicolon]['line'] !== $tokens[$stackPtr]['line'])
64  || TokenizerSymbolsInterface::BITWISE_AND === $tokens[$stackPtr - 1]['content']
65  ) {
66  return false;
67  }
68 
69  $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
70  if ($tokens[$prev]['code'] !== T_STYLE) {
71  // The colon is not part of a style definition.
72  return false;
73  }
74 
75  if ($tokens[$prev]['content'] === 'progid') {
76  // Special case for IE filters.
77  return false;
78  }
79 
80  return true;
81  }
82 
92  private function validateSpaces(File $phpcsFile, $stackPtr, array $tokens)
93  {
94  if (T_WHITESPACE === $tokens[($stackPtr - 1)]['code']) {
95  $phpcsFile->addError('There must be no space before a colon in a style definition', $stackPtr, 'Before');
96  }
97 
98  if (T_WHITESPACE !== $tokens[($stackPtr + 1)]['code']) {
99  $phpcsFile->addError('Expected 1 space after colon in style definition; 0 found', $stackPtr, 'NoneAfter');
100  } else {
101  $content = $tokens[($stackPtr + 1)]['content'];
102  if (false === strpos($content, $phpcsFile->eolChar)) {
103  $length = strlen($content);
104  if ($length !== 1) {
105  $error = 'Expected 1 space after colon in style definition; %s found';
106  $phpcsFile->addError($error, $stackPtr, 'After');
107  }
108  } else {
109  $error = 'Expected 1 space after colon in style definition; newline found';
110  $phpcsFile->addError($error, $stackPtr, 'AfterNewline');
111  }
112  }
113  }
114 }
process(File $phpcsFile, $stackPtr)
$tokens
Definition: cards_list.phtml:9