Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndentationSniff.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 
19 class IndentationSniff implements Sniff
20 {
27 
33  public $indent = 4;
34 
40  public $maxIndentLevel = 3;
41 
46  private $styleCodesToSkip = [T_ASPERAND, T_COLON, T_OPEN_PARENTHESIS, T_CLOSE_PARENTHESIS];
47 
51  public function register()
52  {
53  return [T_OPEN_TAG];
54  }
55 
61  public function process(File $phpcsFile, $stackPtr)
62  {
63  $tokens = $phpcsFile->getTokens();
64 
65  $numTokens = (count($tokens) - 2);
66  $indentLevel = 0;
67  for ($i = 1; $i < $numTokens; $i++) {
68  if ($tokens[$i]['code'] === T_COMMENT) {
69  // Don't check the indent of comments.
70  continue;
71  }
72 
73  if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
74  $indentLevel++;
75  } elseif ($tokens[($i + 1)]['code'] === T_CLOSE_CURLY_BRACKET) {
76  $indentLevel--;
77  }
78 
79  if ($tokens[$i]['column'] !== 1) {
80  continue;
81  }
82 
83  // We started a new line, so check indent.
84  if ($tokens[$i]['code'] === T_WHITESPACE) {
85  $content = str_replace($phpcsFile->eolChar, '', $tokens[$i]['content']);
86  $foundIndent = strlen($content);
87  } else {
88  $foundIndent = 0;
89  }
90 
91  $expectedIndent = ($indentLevel * $this->indent);
92  if (!($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false)
93  && ($foundIndent !== $expectedIndent)
94  && (!in_array($tokens[$i + 1]['code'], $this->styleCodesToSkip))
95  ) {
96  $error = 'Line indented incorrectly; expected %s spaces, found %s';
97  $phpcsFile->addError($error, $i, 'Incorrect', [$expectedIndent, $foundIndent]);
98  }
99 
100  if ($indentLevel > $this->maxIndentLevel) {
101  // Will be implemented in MAGETWO-49778
102  // $phpcsFile->addWarning('Avoid using more than three levels of nesting', $i, 'IncorrectNestingLevel');
103  }
104  }
105  }
106 }
process(File $phpcsFile, $stackPtr)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$i
Definition: gallery.phtml:31
$tokens
Definition: cards_list.phtml:9