RemovedMbStrrposEncodingThirdParamSniff.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\ParameterValues;
  11. use PHPCompatibility\AbstractFunctionCallParameterSniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect passing `$encoding` to `mb_strrpos()` as 3rd argument.
  16. *
  17. * The `$encoding` parameter was moved from the third position to the fourth in PHP 5.2.0.
  18. * For backward compatibility, `$encoding` could be specified as the third parameter, but doing
  19. * so is deprecated and will be removed in the future.
  20. *
  21. * Between PHP 5.2 and PHP 7.3, this was a deprecation in documentation only.
  22. * As of PHP 7.4, a deprecation warning will be thrown if an encoding is passed as the 3rd
  23. * argument.
  24. * As of PHP 8, the argument is expected to change to accept an integer only.
  25. *
  26. * PHP version 5.2
  27. * PHP version 7.4
  28. *
  29. * @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.mbstring
  30. * @link https://wiki.php.net/rfc/deprecations_php_7_4#mb_strrpos_with_encoding_as_3rd_argument
  31. * @link https://www.php.net/manual/en/function.mb-strrpos.php
  32. *
  33. * @since 9.3.0
  34. */
  35. class RemovedMbStrrposEncodingThirdParamSniff extends AbstractFunctionCallParameterSniff
  36. {
  37. /**
  38. * Functions to check for.
  39. *
  40. * @since 9.3.0
  41. *
  42. * @var array
  43. */
  44. protected $targetFunctions = array(
  45. 'mb_strrpos' => true,
  46. );
  47. /**
  48. * Tokens which should be recognized as text.
  49. *
  50. * @since 9.3.0
  51. *
  52. * @var array
  53. */
  54. private $textStringTokens = array(
  55. \T_CONSTANT_ENCAPSED_STRING,
  56. \T_DOUBLE_QUOTED_STRING,
  57. \T_HEREDOC,
  58. \T_NOWDOC,
  59. );
  60. /**
  61. * Tokens which should be recognized as numbers.
  62. *
  63. * @since 9.3.0
  64. *
  65. * @var array
  66. */
  67. private $numberTokens = array(
  68. \T_LNUMBER => \T_LNUMBER,
  69. \T_DNUMBER => \T_DNUMBER,
  70. \T_MINUS => \T_MINUS,
  71. \T_PLUS => \T_PLUS,
  72. );
  73. /**
  74. * Do a version check to determine if this sniff needs to run at all.
  75. *
  76. * @since 9.3.0
  77. *
  78. * @return bool
  79. */
  80. protected function bowOutEarly()
  81. {
  82. return $this->supportsAbove('5.2') === false;
  83. }
  84. /**
  85. * Process the parameters of a matched function.
  86. *
  87. * @since 9.3.0
  88. *
  89. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  90. * @param int $stackPtr The position of the current token in the stack.
  91. * @param string $functionName The token content (function name) which was matched.
  92. * @param array $parameters Array with information about the parameters.
  93. *
  94. * @return int|void Integer stack pointer to skip forward or void to continue
  95. * normal file processing.
  96. */
  97. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  98. {
  99. if (isset($parameters[3]) === false) {
  100. // Optional third parameter not set.
  101. return;
  102. }
  103. if (isset($parameters[4]) === true) {
  104. // Encoding set as fourth parameter.
  105. return;
  106. }
  107. $targetParam = $parameters[3];
  108. $targets = $this->numberTokens + Tokens::$emptyTokens;
  109. $nonNumber = $phpcsFile->findNext($targets, $targetParam['start'], ($targetParam['end'] + 1), true);
  110. if ($nonNumber === false) {
  111. return;
  112. }
  113. if ($this->isNumericCalculation($phpcsFile, $targetParam['start'], $targetParam['end']) === true) {
  114. return;
  115. }
  116. $hasString = $phpcsFile->findNext($this->textStringTokens, $targetParam['start'], ($targetParam['end'] + 1));
  117. if ($hasString === false) {
  118. // No text strings found. Undetermined.
  119. return;
  120. }
  121. $error = 'Passing the encoding to mb_strrpos() as third parameter is soft deprecated since PHP 5.2';
  122. if ($this->supportsAbove('7.4') === true) {
  123. $error .= ' and hard deprecated since PHP 7.4';
  124. }
  125. $error .= '. Use an explicit 0 as the offset in the third parameter.';
  126. $phpcsFile->addWarning(
  127. $error,
  128. $targetParam['start'],
  129. 'Deprecated'
  130. );
  131. }
  132. }