ColonSpacingSniff.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use PHP_CodeSniffer\Util\Tokens;
  10. /**
  11. * Class ColonSpacingSniff
  12. *
  13. * Ensure that single quotes are used
  14. *
  15. * @link https://devdocs.magento.com/guides/v2.0/coding-standards/code-standard-less.html#properties-colon-indents
  16. */
  17. class ColonSpacingSniff 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. * @inheritdoc
  27. */
  28. public function register()
  29. {
  30. return [T_COLON];
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function process(File $phpcsFile, $stackPtr)
  36. {
  37. $tokens = $phpcsFile->getTokens();
  38. if ($this->needValidateSpaces($phpcsFile, $stackPtr, $tokens)) {
  39. $this->validateSpaces($phpcsFile, $stackPtr, $tokens);
  40. }
  41. }
  42. /**
  43. * Check is it need to check spaces
  44. *
  45. * @param File $phpcsFile
  46. * @param int $stackPtr
  47. * @param array $tokens
  48. *
  49. * @return bool
  50. */
  51. private function needValidateSpaces(File $phpcsFile, $stackPtr, $tokens)
  52. {
  53. $nextSemicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
  54. if (false === $nextSemicolon
  55. || ($tokens[$nextSemicolon]['line'] !== $tokens[$stackPtr]['line'])
  56. || TokenizerSymbolsInterface::BITWISE_AND === $tokens[$stackPtr - 1]['content']
  57. ) {
  58. return false;
  59. }
  60. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  61. if ($tokens[$prev]['code'] !== T_STYLE) {
  62. // The colon is not part of a style definition.
  63. return false;
  64. }
  65. if ($tokens[$prev]['content'] === 'progid') {
  66. // Special case for IE filters.
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Validate Colon Spacing according to requirements
  73. *
  74. * @param File $phpcsFile
  75. * @param int $stackPtr
  76. * @param array $tokens
  77. *
  78. * @return void
  79. */
  80. private function validateSpaces(File $phpcsFile, $stackPtr, array $tokens)
  81. {
  82. if (T_WHITESPACE === $tokens[($stackPtr - 1)]['code']) {
  83. $phpcsFile->addError('There must be no space before a colon in a style definition', $stackPtr, 'Before');
  84. }
  85. if (T_WHITESPACE !== $tokens[($stackPtr + 1)]['code']) {
  86. $phpcsFile->addError('Expected 1 space after colon in style definition; 0 found', $stackPtr, 'NoneAfter');
  87. } else {
  88. $content = $tokens[($stackPtr + 1)]['content'];
  89. if (false === strpos($content, $phpcsFile->eolChar)) {
  90. $length = strlen($content);
  91. if ($length !== 1) {
  92. $error = 'Expected 1 space after colon in style definition; %s found';
  93. $phpcsFile->addError($error, $stackPtr, 'After');
  94. }
  95. } else {
  96. $error = 'Expected 1 space after colon in style definition; newline found';
  97. $phpcsFile->addError($error, $stackPtr, 'AfterNewline');
  98. }
  99. }
  100. }
  101. }