NewHashAlgorithmsSniff.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\AbstractNewFeatureSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect the use of newly introduced hash algorithms.
  15. *
  16. * PHP version 5.2+
  17. *
  18. * @link https://www.php.net/manual/en/function.hash-algos.php#refsect1-function.hash-algos-changelog
  19. *
  20. * @since 7.0.7
  21. * @since 7.1.0 Now extends the `AbstractNewFeatureSniff` instead of the base `Sniff` class..
  22. */
  23. class NewHashAlgorithmsSniff extends AbstractNewFeatureSniff
  24. {
  25. /**
  26. * A list of new hash algorithms, not present in older versions.
  27. *
  28. * The array lists : version number with false (not present) or true (present).
  29. * If's sufficient to list the first version where the hash algorithm appears.
  30. *
  31. * @since 7.0.7
  32. *
  33. * @var array(string => array(string => bool))
  34. */
  35. protected $newAlgorithms = array(
  36. 'md2' => array(
  37. '5.2' => false,
  38. '5.3' => true,
  39. ),
  40. 'ripemd256' => array(
  41. '5.2' => false,
  42. '5.3' => true,
  43. ),
  44. 'ripemd320' => array(
  45. '5.2' => false,
  46. '5.3' => true,
  47. ),
  48. 'salsa10' => array(
  49. '5.2' => false,
  50. '5.3' => true,
  51. ),
  52. 'salsa20' => array(
  53. '5.2' => false,
  54. '5.3' => true,
  55. ),
  56. 'snefru256' => array(
  57. '5.2' => false,
  58. '5.3' => true,
  59. ),
  60. 'sha224' => array(
  61. '5.2' => false,
  62. '5.3' => true,
  63. ),
  64. 'joaat' => array(
  65. '5.3' => false,
  66. '5.4' => true,
  67. ),
  68. 'fnv132' => array(
  69. '5.3' => false,
  70. '5.4' => true,
  71. ),
  72. 'fnv164' => array(
  73. '5.3' => false,
  74. '5.4' => true,
  75. ),
  76. 'gost-crypto' => array(
  77. '5.5' => false,
  78. '5.6' => true,
  79. ),
  80. 'sha512/224' => array(
  81. '7.0' => false,
  82. '7.1' => true,
  83. ),
  84. 'sha512/256' => array(
  85. '7.0' => false,
  86. '7.1' => true,
  87. ),
  88. 'sha3-224' => array(
  89. '7.0' => false,
  90. '7.1' => true,
  91. ),
  92. 'sha3-256' => array(
  93. '7.0' => false,
  94. '7.1' => true,
  95. ),
  96. 'sha3-384' => array(
  97. '7.0' => false,
  98. '7.1' => true,
  99. ),
  100. 'sha3-512' => array(
  101. '7.0' => false,
  102. '7.1' => true,
  103. ),
  104. 'crc32c' => array(
  105. '7.3' => false,
  106. '7.4' => true,
  107. ),
  108. );
  109. /**
  110. * Returns an array of tokens this test wants to listen for.
  111. *
  112. * @since 7.0.7
  113. *
  114. * @return array
  115. */
  116. public function register()
  117. {
  118. return array(\T_STRING);
  119. }
  120. /**
  121. * Processes this test, when one of its tokens is encountered.
  122. *
  123. * @since 7.0.7
  124. *
  125. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  126. * @param int $stackPtr The position of the current token in the
  127. * stack passed in $tokens.
  128. *
  129. * @return void
  130. */
  131. public function process(File $phpcsFile, $stackPtr)
  132. {
  133. $algo = $this->getHashAlgorithmParameter($phpcsFile, $stackPtr);
  134. if (empty($algo) || \is_string($algo) === false) {
  135. return;
  136. }
  137. // Bow out if not one of the algorithms we're targetting.
  138. if (isset($this->newAlgorithms[$algo]) === false) {
  139. return;
  140. }
  141. // Check if the algorithm used is new.
  142. $itemInfo = array(
  143. 'name' => $algo,
  144. );
  145. $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
  146. }
  147. /**
  148. * Get the relevant sub-array for a specific item from a multi-dimensional array.
  149. *
  150. * @since 7.1.0
  151. *
  152. * @param array $itemInfo Base information about the item.
  153. *
  154. * @return array Version and other information about the item.
  155. */
  156. public function getItemArray(array $itemInfo)
  157. {
  158. return $this->newAlgorithms[$itemInfo['name']];
  159. }
  160. /**
  161. * Get the error message template for this sniff.
  162. *
  163. * @since 7.1.0
  164. *
  165. * @return string
  166. */
  167. protected function getErrorMsgTemplate()
  168. {
  169. return 'The %s hash algorithm is not present in PHP version %s or earlier';
  170. }
  171. }