NewPasswordAlgoConstantValuesSniff.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * The constant value of the password hash algorithm constants has changed in PHP 7.4.
  16. *
  17. * Applications using the constants `PASSWORD_DEFAULT`, `PASSWORD_BCRYPT`,
  18. * `PASSWORD_ARGON2I`, and `PASSWORD_ARGON2ID` will continue to function correctly.
  19. * Using an integer will still work, but will produce a deprecation warning.
  20. *
  21. * PHP version 7.4
  22. *
  23. * @link https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.password-algorithm-constants
  24. * @link https://wiki.php.net/rfc/password_registry
  25. *
  26. * @since 9.3.0
  27. */
  28. class NewPasswordAlgoConstantValuesSniff extends AbstractFunctionCallParameterSniff
  29. {
  30. /**
  31. * Functions to check for.
  32. *
  33. * Key is the function name, value the 1-based parameter position of
  34. * the $algo parameter.
  35. *
  36. * @since 9.3.0
  37. *
  38. * @var array
  39. */
  40. protected $targetFunctions = array(
  41. 'password_hash' => 2,
  42. 'password_needs_rehash' => 2,
  43. );
  44. /**
  45. * Tokens types which indicate that the parameter passed is not the PHP native constant.
  46. *
  47. * @since 9.3.0
  48. *
  49. * @var array
  50. */
  51. private $invalidTokenTypes = array(
  52. \T_NULL => true,
  53. \T_TRUE => true,
  54. \T_FALSE => true,
  55. \T_LNUMBER => true,
  56. \T_DNUMBER => true,
  57. \T_CONSTANT_ENCAPSED_STRING => true,
  58. \T_DOUBLE_QUOTED_STRING => true,
  59. \T_HEREDOC => true,
  60. \T_NOWDOC => true,
  61. );
  62. /**
  63. * Do a version check to determine if this sniff needs to run at all.
  64. *
  65. * @since 9.3.0
  66. *
  67. * @return bool
  68. */
  69. protected function bowOutEarly()
  70. {
  71. return ($this->supportsAbove('7.4') === false);
  72. }
  73. /**
  74. * Process the parameters of a matched function.
  75. *
  76. * @since 9.3.0
  77. *
  78. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  79. * @param int $stackPtr The position of the current token in the stack.
  80. * @param string $functionName The token content (function name) which was matched.
  81. * @param array $parameters Array with information about the parameters.
  82. *
  83. * @return int|void Integer stack pointer to skip forward or void to continue
  84. * normal file processing.
  85. */
  86. public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
  87. {
  88. $functionLC = strtolower($functionName);
  89. if (isset($parameters[$this->targetFunctions[$functionLC]]) === false) {
  90. return;
  91. }
  92. $targetParam = $parameters[$this->targetFunctions[$functionLC]];
  93. $tokens = $phpcsFile->getTokens();
  94. for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
  95. if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
  96. continue;
  97. }
  98. if (isset($this->invalidTokenTypes[$tokens[$i]['code']]) === true) {
  99. $phpcsFile->addWarning(
  100. 'The value of the password hash algorithm constants has changed in PHP 7.4. Pass a PHP native constant to the %s() function instead of using the value of the constant. Found: %s',
  101. $stackPtr,
  102. 'NotAlgoConstant',
  103. array(
  104. $functionName,
  105. $targetParam['raw'],
  106. )
  107. );
  108. break;
  109. }
  110. }
  111. }
  112. }