NewHTMLEntitiesEncodingDefaultSniff.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * As of PHP 5.4, the default character set for `htmlspecialchars()`, `htmlentities()`
  15. * and `html_entity_decode()` is now `UTF-8`, instead of `ISO-8859-1`.
  16. *
  17. * PHP version 5.4
  18. *
  19. * @link https://www.php.net/manual/en/migration54.other.php
  20. * @link https://www.php.net/manual/en/function.html-entity-decode.php#refsect1-function.html-entity-decode-changelog
  21. * @link https://www.php.net/manual/en/function.htmlentities.php#refsect1-function.htmlentities-changelog
  22. * @link https://www.php.net/manual/en/function.htmlspecialchars.php#refsect1-function.htmlspecialchars-changelog
  23. *
  24. * @since 9.3.0
  25. */
  26. class NewHTMLEntitiesEncodingDefaultSniff 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 $encoding parameter.
  33. *
  34. * @since 9.3.0
  35. *
  36. * @var array
  37. */
  38. protected $targetFunctions = array(
  39. 'html_entity_decode' => 3,
  40. 'htmlentities' => 3,
  41. 'htmlspecialchars' => 3,
  42. );
  43. /**
  44. * Do a version check to determine if this sniff needs to run at all.
  45. *
  46. * Note: This sniff should only trigger errors when both PHP 5.3 or lower,
  47. * as well as PHP 5.4 or higher need to be supported within the application.
  48. *
  49. * @since 9.3.0
  50. *
  51. * @return bool
  52. */
  53. protected function bowOutEarly()
  54. {
  55. return ($this->supportsBelow('5.3') === false || $this->supportsAbove('5.4') === false);
  56. }
  57. /**
  58. * Process the parameters of a matched function.
  59. *
  60. * @since 9.3.0
  61. *
  62. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  63. * @param int $stackPtr The position of the current token in the stack.
  64. * @param string $functionName The token content (function name) which was matched.
  65. * @param array $parameters Array with information about the parameters.
  66. *
  67. * @return int|void Integer stack pointer to skip forward or void to continue
  68. * normal file processing.
  69. */
  70. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  71. {
  72. $functionLC = strtolower($functionName);
  73. if (isset($parameters[$this->targetFunctions[$functionLC]]) === true) {
  74. return;
  75. }
  76. $phpcsFile->addError(
  77. 'The default value of the $encoding parameter for %s() was changed from ISO-8859-1 to UTF-8 in PHP 5.4. For cross-version compatibility, the $encoding parameter should be explicitly set.',
  78. $stackPtr,
  79. 'NotSet',
  80. array($functionName)
  81. );
  82. }
  83. }