NewUseConstFunctionSniff.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\UseDeclarations;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect importing constants and functions via a `use` statement.
  16. *
  17. * The `use` operator has been extended to support importing functions and
  18. * constants in addition to classes. This is achieved via the `use function`
  19. * and `use const` constructs, respectively.
  20. *
  21. * PHP version 5.6
  22. *
  23. * @link https://www.php.net/manual/en/migration56.new-features.php#migration56.new-features.use
  24. * @link https://wiki.php.net/rfc/use_function
  25. * @link https://www.php.net/manual/en/language.namespaces.importing.php
  26. *
  27. * @since 7.1.4
  28. */
  29. class NewUseConstFunctionSniff extends Sniff
  30. {
  31. /**
  32. * A list of keywords that can follow use statements.
  33. *
  34. * @since 7.1.4
  35. *
  36. * @var array(string => string)
  37. */
  38. protected $validUseNames = array(
  39. 'const' => true,
  40. 'function' => true,
  41. );
  42. /**
  43. * Returns an array of tokens this test wants to listen for.
  44. *
  45. * @since 7.1.4
  46. *
  47. * @return array
  48. */
  49. public function register()
  50. {
  51. return array(\T_USE);
  52. }
  53. /**
  54. * Processes this test, when one of its tokens is encountered.
  55. *
  56. * @since 7.1.4
  57. *
  58. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  59. * @param int $stackPtr The position of the current token in the
  60. * stack passed in $tokens.
  61. *
  62. * @return void
  63. */
  64. public function process(File $phpcsFile, $stackPtr)
  65. {
  66. if ($this->supportsBelow('5.5') !== true) {
  67. return;
  68. }
  69. $tokens = $phpcsFile->getTokens();
  70. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  71. if ($nextNonEmpty === false) {
  72. // Live coding.
  73. return;
  74. }
  75. if (isset($this->validUseNames[strtolower($tokens[$nextNonEmpty]['content'])]) === false) {
  76. // Not a `use const` or `use function` statement.
  77. return;
  78. }
  79. // `use const` and `use function` have to be followed by the function/constant name.
  80. $functionOrConstName = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true);
  81. if ($functionOrConstName === false
  82. // Identifies as T_AS or T_STRING, this covers both.
  83. || ($tokens[$functionOrConstName]['content'] === 'as'
  84. || $tokens[$functionOrConstName]['code'] === \T_COMMA)
  85. ) {
  86. // Live coding or incorrect use of reserved keyword, but that is
  87. // covered by the ForbiddenNames sniff.
  88. return;
  89. }
  90. // Still here ? In that case we have encountered a `use const` or `use function` statement.
  91. $phpcsFile->addError(
  92. 'Importing functions and constants through a "use" statement is not supported in PHP 5.5 or lower.',
  93. $nextNonEmpty,
  94. 'Found'
  95. );
  96. }
  97. }