RemovedHashAlgorithmsSniff.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\AbstractRemovedFeatureSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect the use of deprecated and removed hash algorithms.
  15. *
  16. * PHP version 5.4
  17. *
  18. * @link https://www.php.net/manual/en/function.hash-algos.php#refsect1-function.hash-algos-changelog
  19. *
  20. * @since 5.5
  21. * @since 7.1.0 Now extends the `AbstractRemovedFeatureSniff` instead of the base `Sniff` class.
  22. */
  23. class RemovedHashAlgorithmsSniff extends AbstractRemovedFeatureSniff
  24. {
  25. /**
  26. * A list of removed hash algorithms, which were present in older versions.
  27. *
  28. * The array lists : version number with false (deprecated) and true (removed).
  29. * If's sufficient to list the first version where the hash algorithm was deprecated/removed.
  30. *
  31. * @since 7.0.7
  32. *
  33. * @var array(string => array(string => bool))
  34. */
  35. protected $removedAlgorithms = array(
  36. 'salsa10' => array(
  37. '5.4' => true,
  38. ),
  39. 'salsa20' => array(
  40. '5.4' => true,
  41. ),
  42. );
  43. /**
  44. * Returns an array of tokens this test wants to listen for.
  45. *
  46. * @since 5.5
  47. *
  48. * @return array
  49. */
  50. public function register()
  51. {
  52. return array(\T_STRING);
  53. }
  54. /**
  55. * Processes this test, when one of its tokens is encountered.
  56. *
  57. * @since 5.5
  58. *
  59. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  60. * @param int $stackPtr The position of the current token in the
  61. * stack passed in $tokens.
  62. *
  63. * @return void
  64. */
  65. public function process(File $phpcsFile, $stackPtr)
  66. {
  67. $algo = $this->getHashAlgorithmParameter($phpcsFile, $stackPtr);
  68. if (empty($algo) || \is_string($algo) === false) {
  69. return;
  70. }
  71. // Bow out if not one of the algorithms we're targetting.
  72. if (isset($this->removedAlgorithms[$algo]) === false) {
  73. return;
  74. }
  75. $itemInfo = array(
  76. 'name' => $algo,
  77. );
  78. $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
  79. }
  80. /**
  81. * Get the relevant sub-array for a specific item from a multi-dimensional array.
  82. *
  83. * @since 7.1.0
  84. *
  85. * @param array $itemInfo Base information about the item.
  86. *
  87. * @return array Version and other information about the item.
  88. */
  89. public function getItemArray(array $itemInfo)
  90. {
  91. return $this->removedAlgorithms[$itemInfo['name']];
  92. }
  93. /**
  94. * Get the error message template for this sniff.
  95. *
  96. * @since 7.1.0
  97. *
  98. * @return string
  99. */
  100. protected function getErrorMsgTemplate()
  101. {
  102. return 'The %s hash algorithm is ';
  103. }
  104. }