NewArrayUnpackingSniff.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Using the spread operator for unpacking arrays in array expressions is available since PHP 7.4.
  16. *
  17. * PHP version 7.4
  18. *
  19. * @link https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.unpack-inside-array
  20. * @link https://wiki.php.net/rfc/spread_operator_for_array
  21. *
  22. * @since 9.2.0
  23. */
  24. class NewArrayUnpackingSniff extends Sniff
  25. {
  26. /**
  27. * Returns an array of tokens this test wants to listen for.
  28. *
  29. * @since 9.2.0
  30. *
  31. * @return array
  32. */
  33. public function register()
  34. {
  35. return array(
  36. \T_ARRAY,
  37. \T_OPEN_SHORT_ARRAY,
  38. );
  39. }
  40. /**
  41. * Processes this test, when one of its tokens is encountered.
  42. *
  43. * @since 9.2.0
  44. *
  45. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token in the
  47. * stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. public function process(File $phpcsFile, $stackPtr)
  52. {
  53. if ($this->supportsBelow('7.3') === false) {
  54. return;
  55. }
  56. $tokens = $phpcsFile->getTokens();
  57. /*
  58. * Determine the array opener & closer.
  59. */
  60. $closer = $phpcsFile->numTokens;
  61. if ($tokens[$stackPtr]['code'] === \T_ARRAY) {
  62. if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
  63. return;
  64. }
  65. $opener = $tokens[$stackPtr]['parenthesis_opener'];
  66. if (isset($tokens[$opener]['parenthesis_closer'])) {
  67. $closer = $tokens[$opener]['parenthesis_closer'];
  68. }
  69. } else {
  70. // Short array syntax.
  71. $opener = $stackPtr;
  72. if (isset($tokens[$stackPtr]['bracket_closer'])) {
  73. $closer = $tokens[$stackPtr]['bracket_closer'];
  74. }
  75. }
  76. $nestingLevel = 0;
  77. if (isset($tokens[($opener + 1)]['nested_parenthesis'])) {
  78. $nestingLevel = count($tokens[($opener + 1)]['nested_parenthesis']);
  79. }
  80. for ($i = $opener; $i < $closer;) {
  81. $i = $phpcsFile->findNext(array(\T_ELLIPSIS, \T_OPEN_SHORT_ARRAY, \T_ARRAY), ($i + 1), $closer);
  82. if ($i === false) {
  83. return;
  84. }
  85. if ($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY) {
  86. if (isset($tokens[$i]['bracket_closer']) === false) {
  87. // Live coding, unfinished nested array, handle this when the array opener
  88. // of the nested array is passed.
  89. return;
  90. }
  91. // Skip over nested short arrays. These will be handled when the array opener
  92. // of the nested array is passed.
  93. $i = $tokens[$i]['bracket_closer'];
  94. continue;
  95. }
  96. if ($tokens[$i]['code'] === \T_ARRAY) {
  97. if (isset($tokens[$i]['parenthesis_closer']) === false) {
  98. // Live coding, unfinished nested array, handle this when the array opener
  99. // of the nested array is passed.
  100. return;
  101. }
  102. // Skip over nested long arrays. These will be handled when the array opener
  103. // of the nested array is passed.
  104. $i = $tokens[$i]['parenthesis_closer'];
  105. continue;
  106. }
  107. // Ensure this is not function call variable unpacking.
  108. if (isset($tokens[$i]['nested_parenthesis'])
  109. && count($tokens[$i]['nested_parenthesis']) > $nestingLevel
  110. ) {
  111. continue;
  112. }
  113. // Ok, found one.
  114. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
  115. $snippet = trim($phpcsFile->getTokensAsString($i, (($nextNonEmpty - $i) + 1)));
  116. $phpcsFile->addError(
  117. 'Array unpacking within array declarations using the spread operator is not supported in PHP 7.3 or earlier. Found: %s',
  118. $i,
  119. 'Found',
  120. array($snippet)
  121. );
  122. }
  123. }
  124. }