RemovedNonCryptoHashSniff.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 usage of non-cryptographic hashes.
  15. *
  16. * "The `hash_hmac()`, `hash_hmac_file()`, `hash_pbkdf2()`, and `hash_init()`
  17. * (with `HASH_HMAC`) functions no longer accept non-cryptographic hashes."
  18. *
  19. * PHP version 7.2
  20. *
  21. * @link https://www.php.net/manual/en/migration72.incompatible.php#migration72.incompatible.hash-functions
  22. *
  23. * @since 9.0.0
  24. */
  25. class RemovedNonCryptoHashSniff extends AbstractFunctionCallParameterSniff
  26. {
  27. /**
  28. * Functions to check for.
  29. *
  30. * @since 9.0.0
  31. *
  32. * @var array
  33. */
  34. protected $targetFunctions = array(
  35. 'hash_hmac' => true,
  36. 'hash_hmac_file' => true,
  37. 'hash_init' => true,
  38. 'hash_pbkdf2' => true,
  39. );
  40. /**
  41. * List of the non-cryptographic hashes.
  42. *
  43. * @since 9.0.0
  44. *
  45. * @var array
  46. */
  47. protected $disabledCryptos = array(
  48. 'adler32' => true,
  49. 'crc32' => true,
  50. 'crc32b' => true,
  51. 'fnv132' => true,
  52. 'fnv1a32' => true,
  53. 'fnv164' => true,
  54. 'fnv1a64' => true,
  55. 'joaat' => true,
  56. );
  57. /**
  58. * Do a version check to determine if this sniff needs to run at all.
  59. *
  60. * @since 9.0.0
  61. *
  62. * @return bool
  63. */
  64. protected function bowOutEarly()
  65. {
  66. return ($this->supportsAbove('7.2') === false);
  67. }
  68. /**
  69. * Process the parameters of a matched function.
  70. *
  71. * @since 9.0.0
  72. *
  73. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  74. * @param int $stackPtr The position of the current token in the stack.
  75. * @param string $functionName The token content (function name) which was matched.
  76. * @param array $parameters Array with information about the parameters.
  77. *
  78. * @return int|void Integer stack pointer to skip forward or void to continue
  79. * normal file processing.
  80. */
  81. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  82. {
  83. if (isset($parameters[1]) === false) {
  84. return;
  85. }
  86. $targetParam = $parameters[1];
  87. if (isset($this->disabledCryptos[$this->stripQuotes($targetParam['raw'])]) === false) {
  88. return;
  89. }
  90. if (strtolower($functionName) === 'hash_init'
  91. && (isset($parameters[2]) === false
  92. || ($parameters[2]['raw'] !== 'HASH_HMAC'
  93. && $parameters[2]['raw'] !== (string) \HASH_HMAC))
  94. ) {
  95. // For hash_init(), these hashes are only disabled with HASH_HMAC set.
  96. return;
  97. }
  98. $phpcsFile->addError(
  99. 'Non-cryptographic hashes are no longer accepted by function %s() since PHP 7.2. Found: %s',
  100. $targetParam['start'],
  101. $this->stringToErrorCode($functionName),
  102. array(
  103. $functionName,
  104. $targetParam['raw'],
  105. )
  106. );
  107. }
  108. }