NewArrayStringDereferencingSniff.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * Detect array and string literal dereferencing.
  16. *
  17. * As of PHP 5.5, array and string literals can now be dereferenced directly to
  18. * access individual elements and characters.
  19. *
  20. * As of PHP 7.0, this also works when using curly braces for the dereferencing.
  21. * While unclear, this most likely has to do with the Uniform Variable Syntax changes.
  22. *
  23. * PHP version 5.5
  24. * PHP version 7.0
  25. *
  26. * @link https://www.php.net/manual/en/migration55.new-features.php#migration55.new-features.const-dereferencing
  27. * @link https://wiki.php.net/rfc/constdereference
  28. * @link https://wiki.php.net/rfc/uniform_variable_syntax
  29. * @link https://www.php.net/manual/en/language.types.array.php#example-63
  30. *
  31. * {@internal The reason for splitting the logic of this sniff into different methods is
  32. * to allow re-use of the logic by the PHP 7.4 `RemovedCurlyBraceArrayAccess` sniff.}
  33. *
  34. * @since 7.1.4
  35. * @since 9.3.0 Now also detects dereferencing using curly braces.
  36. */
  37. class NewArrayStringDereferencingSniff extends Sniff
  38. {
  39. /**
  40. * Returns an array of tokens this test wants to listen for.
  41. *
  42. * @since 7.1.4
  43. *
  44. * @return array
  45. */
  46. public function register()
  47. {
  48. return array(
  49. \T_ARRAY,
  50. \T_OPEN_SHORT_ARRAY,
  51. \T_CONSTANT_ENCAPSED_STRING,
  52. );
  53. }
  54. /**
  55. * Processes this test, when one of its tokens is encountered.
  56. *
  57. * @since 7.1.4
  58. *
  59. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  60. * @param int $stackPtr The position of the current token in
  61. * the stack passed in $tokens.
  62. *
  63. * @return void
  64. */
  65. public function process(File $phpcsFile, $stackPtr)
  66. {
  67. if ($this->supportsBelow('5.6') === false) {
  68. return;
  69. }
  70. $dereferencing = $this->isArrayStringDereferencing($phpcsFile, $stackPtr);
  71. if (empty($dereferencing)) {
  72. return;
  73. }
  74. $tokens = $phpcsFile->getTokens();
  75. $supports54 = $this->supportsBelow('5.4');
  76. foreach ($dereferencing['braces'] as $openBrace => $closeBrace) {
  77. if ($supports54 === true
  78. && ($tokens[$openBrace]['type'] === 'T_OPEN_SQUARE_BRACKET'
  79. || $tokens[$openBrace]['type'] === 'T_OPEN_SHORT_ARRAY') // Work around bug #1381 in PHPCS 2.8.1 and lower.
  80. ) {
  81. $phpcsFile->addError(
  82. 'Direct array dereferencing of %s is not present in PHP version 5.4 or earlier',
  83. $openBrace,
  84. 'Found',
  85. array($dereferencing['type'])
  86. );
  87. continue;
  88. }
  89. // PHP 7.0 Array/string dereferencing using curly braces.
  90. if ($tokens[$openBrace]['type'] === 'T_OPEN_CURLY_BRACKET') {
  91. $phpcsFile->addError(
  92. 'Direct array dereferencing of %s using curly braces is not present in PHP version 5.6 or earlier',
  93. $openBrace,
  94. 'FoundUsingCurlies',
  95. array($dereferencing['type'])
  96. );
  97. }
  98. }
  99. }
  100. /**
  101. * Check if this string/array is being dereferenced.
  102. *
  103. * @since 9.3.0 Logic split off from the process method.
  104. *
  105. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  106. * @param int $stackPtr The position of the current token in
  107. * the stack passed in $tokens.
  108. *
  109. * @return array Array containing the type of access and stack pointers to the
  110. * open/close braces involved in the array/string dereferencing;
  111. * or an empty array if no array/string dereferencing was detected.
  112. */
  113. public function isArrayStringDereferencing(File $phpcsFile, $stackPtr)
  114. {
  115. $tokens = $phpcsFile->getTokens();
  116. switch ($tokens[$stackPtr]['code']) {
  117. case \T_CONSTANT_ENCAPSED_STRING:
  118. $type = 'string literals';
  119. $end = $stackPtr;
  120. break;
  121. case \T_ARRAY:
  122. if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
  123. // Live coding.
  124. return array();
  125. } else {
  126. $type = 'arrays';
  127. $end = $tokens[$stackPtr]['parenthesis_closer'];
  128. }
  129. break;
  130. case \T_OPEN_SHORT_ARRAY:
  131. if (isset($tokens[$stackPtr]['bracket_closer']) === false) {
  132. // Live coding.
  133. return array();
  134. } else {
  135. $type = 'arrays';
  136. $end = $tokens[$stackPtr]['bracket_closer'];
  137. }
  138. break;
  139. }
  140. if (isset($type, $end) === false) {
  141. // Shouldn't happen, but for some reason did.
  142. return array();
  143. }
  144. $braces = array();
  145. do {
  146. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true, null, true);
  147. if ($nextNonEmpty === false) {
  148. break;
  149. }
  150. if ($tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET'
  151. || $tokens[$nextNonEmpty]['type'] === 'T_OPEN_CURLY_BRACKET' // PHP 7.0+.
  152. || $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SHORT_ARRAY' // Work around bug #1381 in PHPCS 2.8.1 and lower.
  153. ) {
  154. if (isset($tokens[$nextNonEmpty]['bracket_closer']) === false) {
  155. // Live coding or parse error.
  156. break;
  157. }
  158. $braces[$nextNonEmpty] = $tokens[$nextNonEmpty]['bracket_closer'];
  159. // Continue, just in case there is nested array access, i.e. `array(1, 2, 3)[$i][$j];`.
  160. $end = $tokens[$nextNonEmpty]['bracket_closer'];
  161. continue;
  162. }
  163. // If we're still here, we've reached the end of the variable.
  164. break;
  165. } while (true);
  166. if (empty($braces)) {
  167. return array();
  168. }
  169. return array(
  170. 'type' => $type,
  171. 'braces' => $braces,
  172. );
  173. }
  174. }