NewShortArraySniff.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Syntax;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect use of short array syntax which is available since PHP 5.4.
  15. *
  16. * PHP version 5.4
  17. *
  18. * @link https://wiki.php.net/rfc/shortsyntaxforarrays
  19. * @link https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax
  20. *
  21. * @since 7.0.0
  22. * @since 9.0.0 Renamed from `ShortArraySniff` to `NewShortArraySniff`.
  23. */
  24. class NewShortArraySniff extends Sniff
  25. {
  26. /**
  27. * Returns an array of tokens this test wants to listen for.
  28. *
  29. * @since 7.0.0
  30. *
  31. * @return array
  32. */
  33. public function register()
  34. {
  35. return array(
  36. \T_OPEN_SHORT_ARRAY,
  37. \T_CLOSE_SHORT_ARRAY,
  38. );
  39. }
  40. /**
  41. * Processes this test, when one of its tokens is encountered.
  42. *
  43. * @since 7.0.0
  44. *
  45. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token in
  47. * the stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. public function process(File $phpcsFile, $stackPtr)
  52. {
  53. if ($this->supportsBelow('5.3') === false) {
  54. return;
  55. }
  56. $tokens = $phpcsFile->getTokens();
  57. $token = $tokens[$stackPtr];
  58. $error = '%s is not supported in PHP 5.3 or lower';
  59. $data = array();
  60. if ($token['type'] === 'T_OPEN_SHORT_ARRAY') {
  61. $data[] = 'Short array syntax (open)';
  62. } elseif ($token['type'] === 'T_CLOSE_SHORT_ARRAY') {
  63. $data[] = 'Short array syntax (close)';
  64. }
  65. $phpcsFile->addError($error, $stackPtr, 'Found', $data);
  66. }
  67. }