BracesFormattingSniff.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 BracesFormattingSniff
  12. *
  13. * Ensure there is a single blank line after the closing brace of a class definition
  14. *
  15. * @see Squiz_Sniffs_CSS_ClassDefinitionClosingBraceSpaceSniff
  16. * @link https://devdocs.magento.com/guides/v2.0/coding-standards/code-standard-less.html#braces
  17. */
  18. class BracesFormattingSniff implements Sniff
  19. {
  20. /**
  21. * A list of tokenizers this sniff supports.
  22. *
  23. * @var array
  24. */
  25. public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
  26. /**
  27. * @inheritdoc
  28. */
  29. public function register()
  30. {
  31. return [T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function process(File $phpcsFile, $stackPtr)
  37. {
  38. $tokens = $phpcsFile->getTokens();
  39. if (T_OPEN_CURLY_BRACKET === $tokens[$stackPtr]['code']) {
  40. if (TokenizerSymbolsInterface::WHITESPACE !== $tokens[$stackPtr - 1]['content']) {
  41. $phpcsFile->addError('Space before opening brace is missing', $stackPtr, 'SpacingBeforeOpen');
  42. }
  43. return;
  44. }
  45. $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  46. if ($next === false) {
  47. return;
  48. }
  49. if (!in_array($tokens[$next]['code'], [T_CLOSE_TAG, T_CLOSE_CURLY_BRACKET])) {
  50. $found = (($tokens[$next]['line'] - $tokens[$stackPtr]['line']) - 1);
  51. if ($found !== 1) {
  52. $error = 'Expected one blank line after closing brace of class definition; %s found';
  53. $data = [$found];
  54. // Will be implemented in MAGETWO-49778
  55. //$phpcsFile->addWarning($error, $stackPtr, 'SpacingAfterClose', $data);
  56. }
  57. }
  58. // Ignore nested style definitions from here on. The spacing before the closing brace
  59. // (a single blank line) will be enforced by the above check, which ensures there is a
  60. // blank line after the last nested class.
  61. $found = $phpcsFile->findPrevious(
  62. T_CLOSE_CURLY_BRACKET,
  63. ($stackPtr - 1),
  64. $tokens[$stackPtr]['bracket_opener']
  65. );
  66. if ($found !== false) {
  67. return;
  68. }
  69. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  70. if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
  71. $num = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1);
  72. $error = 'Expected 0 blank lines before closing brace of class definition; %s found';
  73. $data = [$num];
  74. $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeClose', $data);
  75. }
  76. }
  77. }