NewForeachExpressionReferencingSniff.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\ControlStructures;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect `foreach` expression referencing.
  15. *
  16. * Before PHP 5.5.0, referencing `$value` in a `foreach` was only possible
  17. * if the iterated array could be referenced (i.e. if it is a variable).
  18. *
  19. * PHP version 5.5
  20. *
  21. * @link https://www.php.net/manual/en/control-structures.foreach.php
  22. *
  23. * @since 9.0.0
  24. */
  25. class NewForeachExpressionReferencingSniff extends Sniff
  26. {
  27. /**
  28. * Returns an array of tokens this test wants to listen for.
  29. *
  30. * @since 9.0.0
  31. *
  32. * @return array
  33. */
  34. public function register()
  35. {
  36. return array(\T_FOREACH);
  37. }
  38. /**
  39. * Processes this test, when one of its tokens is encountered.
  40. *
  41. * @since 9.0.0
  42. *
  43. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  44. * @param int $stackPtr The position of the current token in the
  45. * stack passed in $tokens.
  46. *
  47. * @return void
  48. */
  49. public function process(File $phpcsFile, $stackPtr)
  50. {
  51. if ($this->supportsBelow('5.4') === false) {
  52. return;
  53. }
  54. $tokens = $phpcsFile->getTokens();
  55. if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
  56. return;
  57. }
  58. $opener = $tokens[$stackPtr]['parenthesis_opener'];
  59. $closer = $tokens[$stackPtr]['parenthesis_closer'];
  60. $asToken = $phpcsFile->findNext(\T_AS, ($opener + 1), $closer);
  61. if ($asToken === false) {
  62. return;
  63. }
  64. /*
  65. * Note: referencing $key is not allowed in any version, so this should only find referenced $values.
  66. * If it does find a referenced key, it would be a parse error anyway.
  67. */
  68. $hasReference = $phpcsFile->findNext(\T_BITWISE_AND, ($asToken + 1), $closer);
  69. if ($hasReference === false) {
  70. return;
  71. }
  72. $nestingLevel = 0;
  73. if ($asToken !== ($opener + 1) && isset($tokens[$opener + 1]['nested_parenthesis'])) {
  74. $nestingLevel = \count($tokens[$opener + 1]['nested_parenthesis']);
  75. }
  76. if ($this->isVariable($phpcsFile, ($opener + 1), $asToken, $nestingLevel) === true) {
  77. return;
  78. }
  79. // Non-variable detected before the `as` keyword.
  80. $phpcsFile->addError(
  81. 'Referencing $value is only possible if the iterated array is a variable in PHP 5.4 or earlier.',
  82. $hasReference,
  83. 'Found'
  84. );
  85. }
  86. }