RemovedCurlyBraceArrayAccessSniff.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 PHPCompatibility\Sniffs\Syntax\NewArrayStringDereferencingSniff;
  13. use PHPCompatibility\Sniffs\Syntax\NewClassMemberAccessSniff;
  14. use PHPCompatibility\Sniffs\Syntax\NewFunctionArrayDereferencingSniff;
  15. use PHP_CodeSniffer_File as File;
  16. use PHP_CodeSniffer_Tokens as Tokens;
  17. /**
  18. * Using the curly brace syntax to access array or string offsets has been deprecated in PHP 7.4.
  19. *
  20. * PHP version 7.4
  21. *
  22. * @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.array-string-access-curly-brace
  23. * @link https://wiki.php.net/rfc/deprecate_curly_braces_array_access
  24. *
  25. * @since 9.3.0
  26. */
  27. class RemovedCurlyBraceArrayAccessSniff extends Sniff
  28. {
  29. /**
  30. * Instance of the NewArrayStringDereferencing sniff.
  31. *
  32. * @since 9.3.0
  33. *
  34. * @var \PHPCompatibility\Sniffs\Syntax\NewArrayStringDereferencingSniff
  35. */
  36. private $newArrayStringDereferencing;
  37. /**
  38. * Target tokens as register by the NewArrayStringDereferencing sniff.
  39. *
  40. * @since 9.3.0
  41. *
  42. * @var array
  43. */
  44. private $newArrayStringDereferencingTargets;
  45. /**
  46. * Instance of the NewClassMemberAccess sniff.
  47. *
  48. * @since 9.3.0
  49. *
  50. * @var \PHPCompatibility\Sniffs\Syntax\NewClassMemberAccessSniff
  51. */
  52. private $newClassMemberAccess;
  53. /**
  54. * Target tokens as register by the NewClassMemberAccess sniff.
  55. *
  56. * @since 9.3.0
  57. *
  58. * @var array
  59. */
  60. private $newClassMemberAccessTargets;
  61. /**
  62. * Instance of the NewFunctionArrayDereferencing sniff.
  63. *
  64. * @since 9.3.0
  65. *
  66. * @var \PHPCompatibility\Sniffs\Syntax\NewFunctionArrayDereferencingSniff
  67. */
  68. private $newFunctionArrayDereferencing;
  69. /**
  70. * Target tokens as register by the NewFunctionArrayDereferencing sniff.
  71. *
  72. * @since 9.3.0
  73. *
  74. * @var array
  75. */
  76. private $newFunctionArrayDereferencingTargets;
  77. /**
  78. * Constructor.
  79. *
  80. * @since 9.3.0
  81. */
  82. public function __construct()
  83. {
  84. $this->newArrayStringDereferencing = new NewArrayStringDereferencingSniff();
  85. $this->newClassMemberAccess = new NewClassMemberAccessSniff();
  86. $this->newFunctionArrayDereferencing = new NewFunctionArrayDereferencingSniff();
  87. }
  88. /**
  89. * Returns an array of tokens this test wants to listen for.
  90. *
  91. * @since 9.3.0
  92. *
  93. * @return array
  94. */
  95. public function register()
  96. {
  97. $targets = array(
  98. array(
  99. \T_VARIABLE,
  100. \T_STRING, // Constants.
  101. ),
  102. );
  103. // Registers T_ARRAY, T_OPEN_SHORT_ARRAY and T_CONSTANT_ENCAPSED_STRING.
  104. $additionalTargets = $this->newArrayStringDereferencing->register();
  105. $this->newArrayStringDereferencingTargets = array_flip($additionalTargets);
  106. $targets[] = $additionalTargets;
  107. // Registers T_NEW and T_CLONE.
  108. $additionalTargets = $this->newClassMemberAccess->register();
  109. $this->newClassMemberAccessTargets = array_flip($additionalTargets);
  110. $targets[] = $additionalTargets;
  111. // Registers T_STRING.
  112. $additionalTargets = $this->newFunctionArrayDereferencing->register();
  113. $this->newFunctionArrayDereferencingTargets = array_flip($additionalTargets);
  114. $targets[] = $additionalTargets;
  115. return call_user_func_array('array_merge', $targets);
  116. }
  117. /**
  118. * Processes this test, when one of its tokens is encountered.
  119. *
  120. * @since 9.3.0
  121. *
  122. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  123. * @param int $stackPtr The position of the current token in
  124. * the stack passed in $tokens.
  125. *
  126. * @return void
  127. */
  128. public function process(File $phpcsFile, $stackPtr)
  129. {
  130. if ($this->supportsAbove('7.4') === false) {
  131. return;
  132. }
  133. $tokens = $phpcsFile->getTokens();
  134. $braces = array();
  135. // Note: Overwriting braces in each `if` is fine as only one will match anyway.
  136. if ($tokens[$stackPtr]['code'] === \T_VARIABLE) {
  137. $braces = $this->isVariableArrayAccess($phpcsFile, $stackPtr);
  138. }
  139. if (isset($this->newArrayStringDereferencingTargets[$tokens[$stackPtr]['code']])) {
  140. $dereferencing = $this->newArrayStringDereferencing->isArrayStringDereferencing($phpcsFile, $stackPtr);
  141. if (isset($dereferencing['braces'])) {
  142. $braces = $dereferencing['braces'];
  143. }
  144. }
  145. if (isset($this->newClassMemberAccessTargets[$tokens[$stackPtr]['code']])) {
  146. $braces = $this->newClassMemberAccess->isClassMemberAccess($phpcsFile, $stackPtr);
  147. }
  148. if (isset($this->newFunctionArrayDereferencingTargets[$tokens[$stackPtr]['code']])) {
  149. $braces = $this->newFunctionArrayDereferencing->isFunctionArrayDereferencing($phpcsFile, $stackPtr);
  150. }
  151. if (empty($braces) && $tokens[$stackPtr]['code'] === \T_STRING) {
  152. $braces = $this->isConstantArrayAccess($phpcsFile, $stackPtr);
  153. }
  154. if (empty($braces)) {
  155. return;
  156. }
  157. foreach ($braces as $open => $close) {
  158. // Some of the functions will sniff for both curlies as well as square braces.
  159. if ($tokens[$open]['code'] !== \T_OPEN_CURLY_BRACKET) {
  160. continue;
  161. }
  162. // Make sure there is something between the braces, otherwise it's still not curly brace array access.
  163. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($open + 1), $close, true);
  164. if ($nextNonEmpty === false) {
  165. // Nothing between the brackets. Parse error. Ignore.
  166. continue;
  167. }
  168. // OK, so we've found curly brace array access.
  169. $snippet = $phpcsFile->getTokensAsString($stackPtr, (($close - $stackPtr) + 1));
  170. $fix = $phpcsFile->addFixableWarning(
  171. 'Curly brace syntax for accessing array elements and string offsets has been deprecated in PHP 7.4. Found: %s',
  172. $open,
  173. 'Found',
  174. array($snippet)
  175. );
  176. if ($fix === true) {
  177. $phpcsFile->fixer->beginChangeset();
  178. $phpcsFile->fixer->replaceToken($open, '[');
  179. $phpcsFile->fixer->replaceToken($close, ']');
  180. $phpcsFile->fixer->endChangeset();
  181. }
  182. }
  183. }
  184. /**
  185. * Determine whether a variable is being dereferenced using curly brace syntax.
  186. *
  187. * @since 9.3.0
  188. *
  189. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  190. * @param int $stackPtr The position of the current token in
  191. * the stack passed in $tokens.
  192. *
  193. * @return array An array with the stack pointers to the open/close braces of
  194. * the curly brace array access, or an empty array if no curly
  195. * brace array access was detected.
  196. */
  197. protected function isVariableArrayAccess(File $phpcsFile, $stackPtr)
  198. {
  199. $tokens = $phpcsFile->getTokens();
  200. $current = $stackPtr;
  201. $braces = array();
  202. do {
  203. $current = $phpcsFile->findNext(Tokens::$emptyTokens, ($current + 1), null, true);
  204. if ($current === false) {
  205. break;
  206. }
  207. // Skip over square bracket array access. Bracket styles can be mixed.
  208. if ($tokens[$current]['code'] === \T_OPEN_SQUARE_BRACKET
  209. && isset($tokens[$current]['bracket_closer']) === true
  210. && $current === $tokens[$current]['bracket_opener']
  211. ) {
  212. $current = $tokens[$current]['bracket_closer'];
  213. continue;
  214. }
  215. // Handle property access.
  216. if ($tokens[$current]['code'] === \T_OBJECT_OPERATOR) {
  217. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($current + 1), null, true);
  218. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) {
  219. // Live coding or parse error.
  220. break;
  221. }
  222. $current = $nextNonEmpty;
  223. continue;
  224. }
  225. if ($tokens[$current]['code'] === \T_OPEN_CURLY_BRACKET) {
  226. if (isset($tokens[$current]['bracket_closer']) === false) {
  227. // Live coding or parse error.
  228. break;
  229. }
  230. $braces[$current] = $tokens[$current]['bracket_closer'];
  231. // Continue, just in case there is nested access using curly braces, i.e. `$a{$i}{$j};`.
  232. $current = $tokens[$current]['bracket_closer'];
  233. continue;
  234. }
  235. // If we're still here, we've reached the end of the variable.
  236. break;
  237. } while (true);
  238. return $braces;
  239. }
  240. /**
  241. * Determine whether a T_STRING is a constant being dereferenced using curly brace syntax.
  242. *
  243. * {@internal Note: the first braces for array access to a constant, for some unknown reason,
  244. * can never be curlies, but have to be square brackets.
  245. * Subsequent braces can be curlies.}
  246. *
  247. * @since 9.3.0
  248. *
  249. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  250. * @param int $stackPtr The position of the current token in
  251. * the stack passed in $tokens.
  252. *
  253. * @return array An array with the stack pointers to the open/close braces of
  254. * the curly brace array access, or an empty array if no curly
  255. * brace array access was detected.
  256. */
  257. protected function isConstantArrayAccess(File $phpcsFile, $stackPtr)
  258. {
  259. $tokens = $phpcsFile->getTokens();
  260. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  261. if ($this->isUseOfGlobalConstant($phpcsFile, $stackPtr) === false
  262. && $tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON // Class constant access.
  263. ) {
  264. return array();
  265. }
  266. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  267. if ($nextNonEmpty === false) {
  268. return array();
  269. }
  270. if ($tokens[$nextNonEmpty]['code'] !== \T_OPEN_SQUARE_BRACKET
  271. || isset($tokens[$nextNonEmpty]['bracket_closer']) === false
  272. ) {
  273. // Array access for constants must start with square brackets.
  274. return array();
  275. }
  276. $current = $tokens[$nextNonEmpty]['bracket_closer'];
  277. $braces = array();
  278. do {
  279. $current = $phpcsFile->findNext(Tokens::$emptyTokens, ($current + 1), null, true);
  280. if ($current === false) {
  281. break;
  282. }
  283. // Skip over square bracket array access. Bracket styles can be mixed.
  284. if ($tokens[$current]['code'] === \T_OPEN_SQUARE_BRACKET
  285. && isset($tokens[$current]['bracket_closer']) === true
  286. && $current === $tokens[$current]['bracket_opener']
  287. ) {
  288. $current = $tokens[$current]['bracket_closer'];
  289. continue;
  290. }
  291. if ($tokens[$current]['code'] === \T_OPEN_CURLY_BRACKET) {
  292. if (isset($tokens[$current]['bracket_closer']) === false) {
  293. // Live coding or parse error.
  294. break;
  295. }
  296. $braces[$current] = $tokens[$current]['bracket_closer'];
  297. // Continue, just in case there is nested access using curly braces, i.e. `$a{$i}{$j};`.
  298. $current = $tokens[$current]['bracket_closer'];
  299. continue;
  300. }
  301. // If we're still here, we've reached the end of the variable.
  302. break;
  303. } while (true);
  304. return $braces;
  305. }
  306. }