NewNegativeStringOffsetSniff.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /**
  14. * Detect negative string offsets as parameters passed to functions where this
  15. * was not allowed prior to PHP 7.1.
  16. *
  17. * PHP version 7.1
  18. *
  19. * @link https://wiki.php.net/rfc/negative-string-offsets
  20. *
  21. * @since 9.0.0
  22. */
  23. class NewNegativeStringOffsetSniff extends AbstractFunctionCallParameterSniff
  24. {
  25. /**
  26. * Functions to check for.
  27. *
  28. * @since 9.0.0
  29. *
  30. * @var array Function name => 1-based parameter offset of the affected parameters => parameter name.
  31. */
  32. protected $targetFunctions = array(
  33. 'file_get_contents' => array(
  34. 4 => 'offset',
  35. ),
  36. 'grapheme_extract' => array(
  37. 4 => 'start',
  38. ),
  39. 'grapheme_stripos' => array(
  40. 3 => 'offset',
  41. ),
  42. 'grapheme_strpos' => array(
  43. 3 => 'offset',
  44. ),
  45. 'iconv_strpos' => array(
  46. 3 => 'offset',
  47. ),
  48. 'mb_ereg_search_setpos' => array(
  49. 1 => 'position',
  50. ),
  51. 'mb_strimwidth' => array(
  52. 2 => 'start',
  53. 3 => 'width',
  54. ),
  55. 'mb_stripos' => array(
  56. 3 => 'offset',
  57. ),
  58. 'mb_strpos' => array(
  59. 3 => 'offset',
  60. ),
  61. 'stripos' => array(
  62. 3 => 'offset',
  63. ),
  64. 'strpos' => array(
  65. 3 => 'offset',
  66. ),
  67. 'substr_count' => array(
  68. 3 => 'offset',
  69. 4 => 'length',
  70. ),
  71. );
  72. /**
  73. * Do a version check to determine if this sniff needs to run at all.
  74. *
  75. * @since 9.0.0
  76. *
  77. * @return bool
  78. */
  79. protected function bowOutEarly()
  80. {
  81. return ($this->supportsBelow('7.0') === false);
  82. }
  83. /**
  84. * Process the parameters of a matched function.
  85. *
  86. * @since 9.0.0
  87. *
  88. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  89. * @param int $stackPtr The position of the current token in the stack.
  90. * @param string $functionName The token content (function name) which was matched.
  91. * @param array $parameters Array with information about the parameters.
  92. *
  93. * @return int|void Integer stack pointer to skip forward or void to continue
  94. * normal file processing.
  95. */
  96. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  97. {
  98. $functionLC = strtolower($functionName);
  99. foreach ($this->targetFunctions[$functionLC] as $pos => $name) {
  100. if (isset($parameters[$pos]) === false) {
  101. continue;
  102. }
  103. $targetParam = $parameters[$pos];
  104. if ($this->isNegativeNumber($phpcsFile, $targetParam['start'], $targetParam['end']) === false) {
  105. continue;
  106. }
  107. $phpcsFile->addError(
  108. 'Negative string offsets were not supported for the $%s parameter in %s() in PHP 7.0 or lower. Found %s',
  109. $targetParam['start'],
  110. 'Found',
  111. array(
  112. $name,
  113. $functionName,
  114. $targetParam['raw'],
  115. )
  116. );
  117. }
  118. }
  119. }