AbstractNewFeatureSniff.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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;
  11. use PHP_CodeSniffer_File as File;
  12. /**
  13. * Base class for new feature sniffs.
  14. *
  15. * @since 7.1.0
  16. */
  17. abstract class AbstractNewFeatureSniff extends AbstractComplexVersionSniff
  18. {
  19. /**
  20. * Determine whether an error/warning should be thrown for an item based on collected information.
  21. *
  22. * @since 7.1.0
  23. *
  24. * @param array $errorInfo Detail information about an item.
  25. *
  26. * @return bool
  27. */
  28. protected function shouldThrowError(array $errorInfo)
  29. {
  30. return ($errorInfo['not_in_version'] !== '');
  31. }
  32. /**
  33. * Retrieve the relevant detail (version) information for use in an error message.
  34. *
  35. * @since 7.1.0
  36. *
  37. * @param array $itemArray Version and other information about the item.
  38. * @param array $itemInfo Base information about the item.
  39. *
  40. * @return array
  41. */
  42. public function getErrorInfo(array $itemArray, array $itemInfo)
  43. {
  44. $errorInfo = array(
  45. 'not_in_version' => '',
  46. 'error' => true,
  47. );
  48. $versionArray = $this->getVersionArray($itemArray);
  49. if (empty($versionArray) === false) {
  50. foreach ($versionArray as $version => $present) {
  51. if ($errorInfo['not_in_version'] === '' && $present === false
  52. && $this->supportsBelow($version) === true
  53. ) {
  54. $errorInfo['not_in_version'] = $version;
  55. }
  56. }
  57. }
  58. return $errorInfo;
  59. }
  60. /**
  61. * Get the error message template for this sniff.
  62. *
  63. * @since 7.1.0
  64. *
  65. * @return string
  66. */
  67. protected function getErrorMsgTemplate()
  68. {
  69. return '%s is not present in PHP version %s or earlier';
  70. }
  71. /**
  72. * Generates the error or warning for this item.
  73. *
  74. * @since 7.1.0
  75. *
  76. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  77. * @param int $stackPtr The position of the relevant token in
  78. * the stack.
  79. * @param array $itemInfo Base information about the item.
  80. * @param array $errorInfo Array with detail (version) information
  81. * relevant to the item.
  82. *
  83. * @return void
  84. */
  85. public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
  86. {
  87. $itemName = $this->getItemName($itemInfo, $errorInfo);
  88. $error = $this->getErrorMsgTemplate();
  89. $errorCode = $this->stringToErrorCode($itemName) . 'Found';
  90. $data = array(
  91. $itemName,
  92. $errorInfo['not_in_version'],
  93. );
  94. $error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
  95. $data = $this->filterErrorData($data, $itemInfo, $errorInfo);
  96. $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
  97. }
  98. }