OptionalToRequiredFunctionParametersSniff.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\FunctionUse;
  11. use PHPCompatibility\Sniffs\FunctionUse\RequiredToOptionalFunctionParametersSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect missing required function parameters in calls to native PHP functions.
  15. *
  16. * Specifically when those function parameters used to be optional in older PHP versions.
  17. *
  18. * PHP version All
  19. *
  20. * @link https://www.php.net/manual/en/doc.changelog.php
  21. *
  22. * @since 8.1.0
  23. * @since 9.0.0 Renamed from `OptionalRequiredFunctionParametersSniff` to `OptionalToRequiredFunctionParametersSniff`.
  24. */
  25. class OptionalToRequiredFunctionParametersSniff extends RequiredToOptionalFunctionParametersSniff
  26. {
  27. /**
  28. * A list of function parameters, which were optional in older versions and became required later on.
  29. *
  30. * The array lists : version number with true (required) and false (optional use deprecated).
  31. *
  32. * The index is the location of the parameter in the parameter list, starting at 0 !
  33. * If's sufficient to list the last version in which the parameter was not yet required.
  34. *
  35. * @since 8.1.0
  36. *
  37. * @var array
  38. */
  39. protected $functionParameters = array(
  40. // Special case, the optional nature is not deprecated, but usage is recommended
  41. // and leaving the parameter out will throw an E_NOTICE.
  42. 'crypt' => array(
  43. 1 => array(
  44. 'name' => 'salt',
  45. '5.6' => 'recommended',
  46. ),
  47. ),
  48. 'parse_str' => array(
  49. 1 => array(
  50. 'name' => 'result',
  51. '7.2' => false,
  52. ),
  53. ),
  54. );
  55. /**
  56. * Determine whether an error/warning should be thrown for an item based on collected information.
  57. *
  58. * @since 8.1.0
  59. *
  60. * @param array $errorInfo Detail information about an item.
  61. *
  62. * @return bool
  63. */
  64. protected function shouldThrowError(array $errorInfo)
  65. {
  66. return ($errorInfo['optionalDeprecated'] !== ''
  67. || $errorInfo['optionalRemoved'] !== ''
  68. || $errorInfo['optionalRecommended'] !== '');
  69. }
  70. /**
  71. * Retrieve the relevant detail (version) information for use in an error message.
  72. *
  73. * @since 8.1.0
  74. *
  75. * @param array $itemArray Version and other information about the item.
  76. * @param array $itemInfo Base information about the item.
  77. *
  78. * @return array
  79. */
  80. public function getErrorInfo(array $itemArray, array $itemInfo)
  81. {
  82. $errorInfo = array(
  83. 'paramName' => '',
  84. 'optionalRecommended' => '',
  85. 'optionalDeprecated' => '',
  86. 'optionalRemoved' => '',
  87. 'error' => false,
  88. );
  89. $versionArray = $this->getVersionArray($itemArray);
  90. if (empty($versionArray) === false) {
  91. foreach ($versionArray as $version => $required) {
  92. if ($this->supportsAbove($version) === true) {
  93. if ($required === true && $errorInfo['optionalRemoved'] === '') {
  94. $errorInfo['optionalRemoved'] = $version;
  95. $errorInfo['error'] = true;
  96. } elseif ($required === 'recommended' && $errorInfo['optionalRecommended'] === '') {
  97. $errorInfo['optionalRecommended'] = $version;
  98. } elseif ($errorInfo['optionalDeprecated'] === '') {
  99. $errorInfo['optionalDeprecated'] = $version;
  100. }
  101. }
  102. }
  103. }
  104. $errorInfo['paramName'] = $itemArray['name'];
  105. return $errorInfo;
  106. }
  107. /**
  108. * Generates the error or warning for this item.
  109. *
  110. * @since 8.1.0
  111. *
  112. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  113. * @param int $stackPtr The position of the relevant token in
  114. * the stack.
  115. * @param array $itemInfo Base information about the item.
  116. * @param array $errorInfo Array with detail (version) information
  117. * relevant to the item.
  118. *
  119. * @return void
  120. */
  121. public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
  122. {
  123. $error = 'The "%s" parameter for function %s() is missing. Passing this parameter is ';
  124. if ($errorInfo['optionalRecommended'] === '') {
  125. $error .= 'no longer optional. The optional nature of the parameter is ';
  126. } else {
  127. $error .= 'strongly recommended ';
  128. }
  129. $errorCode = $this->stringToErrorCode($itemInfo['name'] . '_' . $errorInfo['paramName']);
  130. $data = array(
  131. $errorInfo['paramName'],
  132. $itemInfo['name'],
  133. );
  134. if ($errorInfo['optionalRecommended'] !== '') {
  135. $error .= 'since PHP %s ';
  136. $errorCode .= 'SoftRecommended';
  137. $data[] = $errorInfo['optionalRecommended'];
  138. } else {
  139. if ($errorInfo['optionalDeprecated'] !== '') {
  140. $error .= 'deprecated since PHP %s and ';
  141. $errorCode .= 'SoftRequired';
  142. $data[] = $errorInfo['optionalDeprecated'];
  143. }
  144. if ($errorInfo['optionalRemoved'] !== '') {
  145. $error .= 'removed since PHP %s and ';
  146. $errorCode .= 'HardRequired';
  147. $data[] = $errorInfo['optionalRemoved'];
  148. }
  149. // Remove the last 'and' from the message.
  150. $error = substr($error, 0, (\strlen($error) - 5));
  151. }
  152. $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
  153. }
  154. }