InternalInterfacesSniff.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2019 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Sniffs\Interfaces;
  11. use PHPCompatibility\Sniff;
  12. use PHPCompatibility\PHPCSHelper;
  13. use PHP_CodeSniffer_File as File;
  14. /**
  15. * Detect classes which implement PHP native interfaces intended only for PHP internal use.
  16. *
  17. * PHP version 5.0+
  18. *
  19. * @link https://www.php.net/manual/en/class.traversable.php
  20. * @link https://www.php.net/manual/en/class.throwable.php
  21. * @link https://www.php.net/manual/en/class.datetimeinterface.php
  22. *
  23. * @since 7.0.3
  24. */
  25. class InternalInterfacesSniff extends Sniff
  26. {
  27. /**
  28. * A list of PHP internal interfaces, not intended to be implemented by userland classes.
  29. *
  30. * The array lists : the error message to use.
  31. *
  32. * @since 7.0.3
  33. *
  34. * @var array(string => string)
  35. */
  36. protected $internalInterfaces = array(
  37. 'Traversable' => 'shouldn\'t be implemented directly, implement the Iterator or IteratorAggregate interface instead.',
  38. 'DateTimeInterface' => 'is intended for type hints only and is not implementable.',
  39. 'Throwable' => 'cannot be implemented directly, extend the Exception class instead.',
  40. );
  41. /**
  42. * Returns an array of tokens this test wants to listen for.
  43. *
  44. * @since 7.0.3
  45. *
  46. * @return array
  47. */
  48. public function register()
  49. {
  50. // Handle case-insensitivity of interface names.
  51. $this->internalInterfaces = $this->arrayKeysToLowercase($this->internalInterfaces);
  52. $targets = array(\T_CLASS);
  53. if (\defined('T_ANON_CLASS')) {
  54. $targets[] = \T_ANON_CLASS;
  55. }
  56. return $targets;
  57. }
  58. /**
  59. * Processes this test, when one of its tokens is encountered.
  60. *
  61. * @since 7.0.3
  62. *
  63. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  64. * @param int $stackPtr The position of the current token in
  65. * the stack passed in $tokens.
  66. *
  67. * @return void
  68. */
  69. public function process(File $phpcsFile, $stackPtr)
  70. {
  71. $interfaces = PHPCSHelper::findImplementedInterfaceNames($phpcsFile, $stackPtr);
  72. if (\is_array($interfaces) === false || $interfaces === array()) {
  73. return;
  74. }
  75. foreach ($interfaces as $interface) {
  76. $interface = ltrim($interface, '\\');
  77. $interfaceLc = strtolower($interface);
  78. if (isset($this->internalInterfaces[$interfaceLc]) === true) {
  79. $error = 'The interface %s %s';
  80. $errorCode = $this->stringToErrorCode($interfaceLc) . 'Found';
  81. $data = array(
  82. $interface,
  83. $this->internalInterfaces[$interfaceLc],
  84. );
  85. $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
  86. }
  87. }
  88. }
  89. }