NewPCREModifiersSniff.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Sniffs\ParameterValues\RemovedPCREModifiersSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Check for the use of newly added regex modifiers for PCRE functions.
  15. *
  16. * Initially just checks for the PHP 7.2 new `J` modifier.
  17. *
  18. * PHP version 7.2+
  19. *
  20. * @link https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
  21. * @link https://www.php.net/manual/en/migration72.new-features.php#migration72.new-features.pcre
  22. *
  23. * @since 8.2.0
  24. * @since 9.0.0 Renamed from `PCRENewModifiersSniff` to `NewPCREModifiersSniff`.
  25. */
  26. class NewPCREModifiersSniff extends RemovedPCREModifiersSniff
  27. {
  28. /**
  29. * Functions to check for.
  30. *
  31. * @since 8.2.0
  32. *
  33. * @var array
  34. */
  35. protected $targetFunctions = array(
  36. 'preg_filter' => true,
  37. 'preg_grep' => true,
  38. 'preg_match_all' => true,
  39. 'preg_match' => true,
  40. 'preg_replace_callback_array' => true,
  41. 'preg_replace_callback' => true,
  42. 'preg_replace' => true,
  43. 'preg_split' => true,
  44. );
  45. /**
  46. * Array listing newly introduced regex modifiers.
  47. *
  48. * The key should be the modifier (case-sensitive!).
  49. * The value should be the PHP version in which the modifier was introduced.
  50. *
  51. * @since 8.2.0
  52. *
  53. * @var array
  54. */
  55. protected $newModifiers = array(
  56. 'J' => array(
  57. '7.1' => false,
  58. '7.2' => true,
  59. ),
  60. );
  61. /**
  62. * Do a version check to determine if this sniff needs to run at all.
  63. *
  64. * @since 8.2.0
  65. *
  66. * @return bool
  67. */
  68. protected function bowOutEarly()
  69. {
  70. // Version used here should be the highest version from the `$newModifiers` array,
  71. // i.e. the last PHP version in which a new modifier was introduced.
  72. return ($this->supportsBelow('7.2') === false);
  73. }
  74. /**
  75. * Examine the regex modifier string.
  76. *
  77. * @since 8.2.0
  78. *
  79. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  80. * @param int $stackPtr The position of the current token in the
  81. * stack passed in $tokens.
  82. * @param string $functionName The function which contained the pattern.
  83. * @param string $modifiers The regex modifiers found.
  84. *
  85. * @return void
  86. */
  87. protected function examineModifiers(File $phpcsFile, $stackPtr, $functionName, $modifiers)
  88. {
  89. $error = 'The PCRE regex modifier "%s" is not present in PHP version %s or earlier';
  90. foreach ($this->newModifiers as $modifier => $versionArray) {
  91. if (strpos($modifiers, $modifier) === false) {
  92. continue;
  93. }
  94. $notInVersion = '';
  95. foreach ($versionArray as $version => $present) {
  96. if ($notInVersion === '' && $present === false
  97. && $this->supportsBelow($version) === true
  98. ) {
  99. $notInVersion = $version;
  100. }
  101. }
  102. if ($notInVersion === '') {
  103. continue;
  104. }
  105. $errorCode = $modifier . 'ModifierFound';
  106. $data = array(
  107. $modifier,
  108. $notInVersion,
  109. );
  110. $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
  111. }
  112. }
  113. }