RemovedIconvEncodingSniff.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 passing deprecated `$type` values to `iconv_get_encoding()`.
  15. *
  16. * "The iconv and mbstring configuration options related to encoding have been
  17. * deprecated in favour of default_charset."
  18. *
  19. * {@internal It is unclear which mbstring functions should be targetted, so for now,
  20. * only the iconv function is handled.}
  21. *
  22. * PHP version 5.6
  23. *
  24. * @link https://www.php.net/manual/en/migration56.deprecated.php#migration56.deprecated.iconv-mbstring-encoding
  25. * @link https://wiki.php.net/rfc/default_encoding
  26. *
  27. * @since 9.0.0
  28. */
  29. class RemovedIconvEncodingSniff extends AbstractFunctionCallParameterSniff
  30. {
  31. /**
  32. * Functions to check for.
  33. *
  34. * @since 9.0.0
  35. *
  36. * @var array
  37. */
  38. protected $targetFunctions = array(
  39. 'iconv_set_encoding' => true,
  40. );
  41. /**
  42. * Do a version check to determine if this sniff needs to run at all.
  43. *
  44. * @since 9.0.0
  45. *
  46. * @return bool
  47. */
  48. protected function bowOutEarly()
  49. {
  50. return ($this->supportsAbove('5.6') === false);
  51. }
  52. /**
  53. * Process the parameters of a matched function.
  54. *
  55. * @since 9.0.0
  56. *
  57. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  58. * @param int $stackPtr The position of the current token in the stack.
  59. * @param string $functionName The token content (function name) which was matched.
  60. * @param array $parameters Array with information about the parameters.
  61. *
  62. * @return int|void Integer stack pointer to skip forward or void to continue
  63. * normal file processing.
  64. */
  65. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  66. {
  67. if (isset($parameters[1]) === false) {
  68. return;
  69. }
  70. $phpcsFile->addWarning(
  71. 'All previously accepted values for the $type parameter of iconv_set_encoding() have been deprecated since PHP 5.6. Found %s',
  72. $parameters[1]['start'],
  73. 'DeprecatedValueFound',
  74. $parameters[1]['raw']
  75. );
  76. }
  77. }