Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AvoidIdSniff.php
Go to the documentation of this file.
1 <?php
7 
8 use PHP_CodeSniffer\Sniffs\Sniff;
9 use PHP_CodeSniffer\Files\File;
10 
18 class AvoidIdSniff implements Sniff
19 {
26 
32  private $selectorTokens = [
33  T_HASH,
34  T_WHITESPACE,
35  T_STRING_CONCAT,
36  T_OPEN_PARENTHESIS,
37  T_CLOSE_PARENTHESIS,
38  T_OPEN_SQUARE_BRACKET,
39  T_CLOSE_SQUARE_BRACKET,
40  T_DOUBLE_QUOTED_STRING,
41  T_CONSTANT_ENCAPSED_STRING,
42  T_DOUBLE_COLON,
43  T_COLON,
44  T_EQUAL,
45  T_MUL_EQUAL,
46  T_OR_EQUAL,
47  T_STRING,
48  T_NONE,
49  T_DOLLAR,
50  T_GREATER_THAN,
51  T_PLUS,
52  T_NS_SEPARATOR,
53  T_LNUMBER,
54  ];
55 
59  public function register()
60  {
61  return [T_HASH];
62  }
63 
88  public function process(File $phpcsFile, $stackPtr)
89  {
90  $tokens = $phpcsFile->getTokens();
91 
92  // Find the next non-selector token
93  $nextToken = $phpcsFile->findNext($this->selectorTokens, $stackPtr + 1, null, true);
94 
95  // Anything except a { or a , means this is not a selector
96  if ($nextToken !== false && in_array($tokens[$nextToken]['code'], [T_OPEN_CURLY_BRACKET, T_COMMA])) {
97  $phpcsFile->addError('Id selector is used', $stackPtr, 'IdSelectorUsage');
98  }
99  }
100 }
process(File $phpcsFile, $stackPtr)
$tokens
Definition: cards_list.phtml:9