InterfaceNameSniff.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\NamingConventions;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. /**
  10. * Validates that interface name ends with "Interface" suffix.
  11. */
  12. class InterfaceNameSniff implements Sniff
  13. {
  14. const INTERFACE_SUFFIX = 'Interface';
  15. /**
  16. * @inheritdoc
  17. */
  18. public function register()
  19. {
  20. return [T_INTERFACE];
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function process(File $sourceFile, $stackPtr)
  26. {
  27. $tokens = $sourceFile->getTokens();
  28. $declarationLine = $tokens[$stackPtr]['line'];
  29. $suffixLength = strlen(self::INTERFACE_SUFFIX);
  30. // Find first T_STRING after 'interface' keyword in the line and verify it
  31. while ($tokens[$stackPtr]['line'] == $declarationLine) {
  32. if ($tokens[$stackPtr]['type'] == 'T_STRING') {
  33. if (substr($tokens[$stackPtr]['content'], 0 - $suffixLength) != self::INTERFACE_SUFFIX) {
  34. $sourceFile->addError(
  35. 'Interface should have name that ends with "Interface" suffix.',
  36. $stackPtr,
  37. 'WrongInterfaceName'
  38. );
  39. }
  40. break;
  41. }
  42. $stackPtr++;
  43. }
  44. }
  45. }