ClassNamingSniff.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ClassNamingSniff
  11. *
  12. * Ensure that class name responds to the following requirements:
  13. *
  14. * - names should be lowercase;
  15. * - start with a letter (except helper classes);
  16. * - words should be separated with dash '-';
  17. *
  18. * @link https://devdocs.magento.com/guides/v2.0/coding-standards/code-standard-less.html#standard-classes
  19. */
  20. class ClassNamingSniff implements Sniff
  21. {
  22. const STRING_HELPER_CLASSES_PREFIX = '_';
  23. const STRING_ALLOWED_UNDERSCORES = '__';
  24. /**
  25. * A list of tokenizers this sniff supports.
  26. *
  27. * @var array
  28. */
  29. public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
  30. /**
  31. * @inheritdoc
  32. */
  33. public function register()
  34. {
  35. return [T_STRING_CONCAT];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function process(File $phpcsFile, $stackPtr)
  41. {
  42. $tokens = $phpcsFile->getTokens();
  43. if (T_WHITESPACE !== $tokens[$stackPtr - 1]['code']
  44. && !in_array($tokens[$stackPtr - 1]['content'], [
  45. TokenizerSymbolsInterface::INDENT_SPACES,
  46. TokenizerSymbolsInterface::NEW_LINE,
  47. ])
  48. ) {
  49. return;
  50. }
  51. $className = $tokens[$stackPtr + 1]['content'];
  52. if (preg_match_all('/[^a-z0-9\-_]/U', $className, $matches)) {
  53. $phpcsFile->addError('Class name contains not allowed symbols', $stackPtr, 'NotAllowedSymbol', $matches);
  54. }
  55. }
  56. }