RemovedSetlocaleStringSniff.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 a string literal as `$category` to `setlocale()`.
  15. *
  16. * Support for the category parameter passed as a string has been removed.
  17. * Only `LC_*` constants can be used as of PHP 7.0.0.
  18. *
  19. * PHP version 4.2
  20. * PHP version 7.0
  21. *
  22. * @link https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
  23. * @link https://www.php.net/manual/en/function.setlocale.php#refsect1-function.setlocale-changelog
  24. *
  25. * @since 9.0.0
  26. */
  27. class RemovedSetlocaleStringSniff extends AbstractFunctionCallParameterSniff
  28. {
  29. /**
  30. * Functions to check for.
  31. *
  32. * @since 9.0.0
  33. *
  34. * @var array
  35. */
  36. protected $targetFunctions = array(
  37. 'setlocale' => true,
  38. );
  39. /**
  40. * Do a version check to determine if this sniff needs to run at all.
  41. *
  42. * @since 9.0.0
  43. *
  44. * @return bool
  45. */
  46. protected function bowOutEarly()
  47. {
  48. return ($this->supportsAbove('4.2') === false);
  49. }
  50. /**
  51. * Process the parameters of a matched function.
  52. *
  53. * @since 9.0.0
  54. *
  55. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  56. * @param int $stackPtr The position of the current token in the stack.
  57. * @param string $functionName The token content (function name) which was matched.
  58. * @param array $parameters Array with information about the parameters.
  59. *
  60. * @return int|void Integer stack pointer to skip forward or void to continue
  61. * normal file processing.
  62. */
  63. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  64. {
  65. if (isset($parameters[1]) === false) {
  66. return;
  67. }
  68. $tokens = $phpcsFile->getTokens();
  69. $targetParam = $parameters[1];
  70. for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
  71. if ($tokens[$i]['code'] !== \T_CONSTANT_ENCAPSED_STRING
  72. && $tokens[$i]['code'] !== \T_DOUBLE_QUOTED_STRING
  73. ) {
  74. continue;
  75. }
  76. $message = 'Passing the $category as a string to setlocale() has been deprecated since PHP 4.2';
  77. $isError = false;
  78. $errorCode = 'Deprecated';
  79. $data = array($targetParam['raw']);
  80. if ($this->supportsAbove('7.0') === true) {
  81. $message .= ' and is removed since PHP 7.0';
  82. $isError = true;
  83. $errorCode = 'Removed';
  84. }
  85. $message .= '; Pass one of the LC_* constants instead. Found: %s';
  86. $this->addMessage($phpcsFile, $message, $i, $isError, $errorCode, $data);
  87. break;
  88. }
  89. }
  90. }