IndentationSniff.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\Less;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. /**
  10. * Class IndentationSniff
  11. *
  12. * Ensures styles are indented 4 spaces.
  13. *
  14. * @see Squiz_Sniffs_CSS_IndentationSniff
  15. * @link https://devdocs.magento.com/guides/v2.0/coding-standards/code-standard-less.html#indentation
  16. */
  17. class IndentationSniff implements Sniff
  18. {
  19. /**
  20. * A list of tokenizers this sniff supports.
  21. *
  22. * @var array
  23. */
  24. public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
  25. /**
  26. * The number of spaces code should be indented.
  27. *
  28. * @var int
  29. */
  30. public $indent = 4;
  31. /**
  32. * A nesting level than this value will throw a warning.
  33. *
  34. * @var int
  35. */
  36. public $maxIndentLevel = 3;
  37. /** Skip codes that can be detected by sniffer incorrectly
  38. *
  39. * @var array
  40. */
  41. private $styleCodesToSkip = [T_ASPERAND, T_COLON, T_OPEN_PARENTHESIS, T_CLOSE_PARENTHESIS];
  42. /**
  43. * @inheritdoc
  44. */
  45. public function register()
  46. {
  47. return [T_OPEN_TAG];
  48. }
  49. /**
  50. * @inheritdoc
  51. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  52. * @SuppressWarnings(PHPMD.NPathComplexity)
  53. */
  54. public function process(File $phpcsFile, $stackPtr)
  55. {
  56. $tokens = $phpcsFile->getTokens();
  57. $numTokens = (count($tokens) - 2);
  58. $indentLevel = 0;
  59. for ($i = 1; $i < $numTokens; $i++) {
  60. if ($tokens[$i]['code'] === T_COMMENT) {
  61. // Don't check the indent of comments.
  62. continue;
  63. }
  64. if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
  65. $indentLevel++;
  66. } elseif ($tokens[($i + 1)]['code'] === T_CLOSE_CURLY_BRACKET) {
  67. $indentLevel--;
  68. }
  69. if ($tokens[$i]['column'] !== 1) {
  70. continue;
  71. }
  72. // We started a new line, so check indent.
  73. if ($tokens[$i]['code'] === T_WHITESPACE) {
  74. $content = str_replace($phpcsFile->eolChar, '', $tokens[$i]['content']);
  75. $foundIndent = strlen($content);
  76. } else {
  77. $foundIndent = 0;
  78. }
  79. $expectedIndent = ($indentLevel * $this->indent);
  80. if (!($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false)
  81. && ($foundIndent !== $expectedIndent)
  82. && (!in_array($tokens[$i + 1]['code'], $this->styleCodesToSkip))
  83. ) {
  84. $error = 'Line indented incorrectly; expected %s spaces, found %s';
  85. $phpcsFile->addError($error, $i, 'Incorrect', [$expectedIndent, $foundIndent]);
  86. }
  87. if ($indentLevel > $this->maxIndentLevel) {
  88. // Will be implemented in MAGETWO-49778
  89. // $phpcsFile->addWarning('Avoid using more than three levels of nesting', $i, 'IncorrectNestingLevel');
  90. }
  91. }
  92. }
  93. }