Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VariablesSniff.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 
23 class VariablesSniff implements Sniff
24 {
31 
35  public function register()
36  {
37  return [T_ASPERAND];
38  }
39 
43  public function process(File $phpcsFile, $stackPtr)
44  {
45  $tokens = $phpcsFile->getTokens();
46  $currentToken = $tokens[$stackPtr];
47 
48  $nextColon = $phpcsFile->findNext(T_COLON, $stackPtr);
49  $nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
50  if ((false === $nextColon) || (false === $nextSemicolon)) {
51  return;
52  }
53 
54  $isVariableDeclaration = ($currentToken['line'] === $tokens[$nextColon]['line'])
55  && ($currentToken['line'] === $tokens[$nextSemicolon]['line'])
56  && (T_STRING === $tokens[$stackPtr + 1]['code'])
57  && (T_COLON === $tokens[$stackPtr + 2]['code']);
58 
59  if (!$isVariableDeclaration) {
60  return;
61  }
62 
63  $classBefore = $phpcsFile->findPrevious(T_STYLE, $stackPtr);
64  if (false !== $classBefore) {
65  $phpcsFile->addError(
66  'Variable declaration located not in the beginning of general comments',
67  $stackPtr,
68  'VariableLocation'
69  );
70  }
71 
72  $variableName = $tokens[$stackPtr + 1]['content'];
73  if (preg_match('/[A-Z]/', $variableName)) {
74  $phpcsFile->addError(
75  'Variable declaration contains uppercase symbols',
76  $stackPtr,
77  'VariableUppercase'
78  );
79  }
80  }
81 }
process(File $phpcsFile, $stackPtr)
$tokens
Definition: cards_list.phtml:9