NewIDNVariantDefaultSniff.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * The default value for the `$variant` parameter has changed from `INTL_IDNA_VARIANT_2003`
  15. * to `INTL_IDNA_VARIANT_UTS46` in PHP 7.4.
  16. *
  17. * PHP version 7.4
  18. *
  19. * @link https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.intl
  20. * @link https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
  21. * @link https://www.php.net/manual/en/function.idn-to-ascii.php
  22. * @link https://www.php.net/manual/en/function.idn-to-utf8.php
  23. *
  24. * @since 9.3.0
  25. */
  26. class NewIDNVariantDefaultSniff extends AbstractFunctionCallParameterSniff
  27. {
  28. /**
  29. * Functions to check for.
  30. *
  31. * Key is the function name, value the 1-based parameter position of
  32. * the $variant parameter.
  33. *
  34. * @since 9.3.0
  35. *
  36. * @var array
  37. */
  38. protected $targetFunctions = array(
  39. 'idn_to_ascii' => 3,
  40. 'idn_to_utf8' => 3,
  41. );
  42. /**
  43. * Do a version check to determine if this sniff needs to run at all.
  44. *
  45. * Note: This sniff should only trigger errors when both PHP 7.3 or lower,
  46. * as well as PHP 7.4 or higher need to be supported within the application.
  47. *
  48. * @since 9.3.0
  49. *
  50. * @return bool
  51. */
  52. protected function bowOutEarly()
  53. {
  54. return ($this->supportsBelow('7.3') === false || $this->supportsAbove('7.4') === false);
  55. }
  56. /**
  57. * Process the parameters of a matched function.
  58. *
  59. * @since 9.3.0
  60. *
  61. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  62. * @param int $stackPtr The position of the current token in the stack.
  63. * @param string $functionName The token content (function name) which was matched.
  64. * @param array $parameters Array with information about the parameters.
  65. *
  66. * @return int|void Integer stack pointer to skip forward or void to continue
  67. * normal file processing.
  68. */
  69. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  70. {
  71. $functionLC = strtolower($functionName);
  72. if (isset($parameters[$this->targetFunctions[$functionLC]]) === true) {
  73. return;
  74. }
  75. $error = 'The default value of the %s() $variant parameter has changed from INTL_IDNA_VARIANT_2003 to INTL_IDNA_VARIANT_UTS46 in PHP 7.4. For optimal cross-version compatibility, the $variant parameter should be explicitly set.';
  76. $phpcsFile->addError(
  77. $error,
  78. $stackPtr,
  79. 'NotSet',
  80. array($functionName)
  81. );
  82. }
  83. }