AbstractRemovedFeatureSniff.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 removed feature sniffs.
  14. *
  15. * @since 7.1.0
  16. */
  17. abstract class AbstractRemovedFeatureSniff 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['deprecated'] !== '' || $errorInfo['removed'] !== '');
  31. }
  32. /**
  33. * Get an array of the non-PHP-version array keys used in a sub-array.
  34. *
  35. * By default, removed feature version arrays, contain an additional 'alternative' array key.
  36. *
  37. * @since 7.1.0
  38. *
  39. * @return array
  40. */
  41. protected function getNonVersionArrayKeys()
  42. {
  43. return array('alternative');
  44. }
  45. /**
  46. * Retrieve the relevant detail (version) information for use in an error message.
  47. *
  48. * @since 7.1.0
  49. *
  50. * @param array $itemArray Version and other information about the item.
  51. * @param array $itemInfo Base information about the item.
  52. *
  53. * @return array
  54. */
  55. public function getErrorInfo(array $itemArray, array $itemInfo)
  56. {
  57. $errorInfo = array(
  58. 'deprecated' => '',
  59. 'removed' => '',
  60. 'alternative' => '',
  61. 'error' => false,
  62. );
  63. $versionArray = $this->getVersionArray($itemArray);
  64. if (empty($versionArray) === false) {
  65. foreach ($versionArray as $version => $removed) {
  66. if ($this->supportsAbove($version) === true) {
  67. if ($removed === true && $errorInfo['removed'] === '') {
  68. $errorInfo['removed'] = $version;
  69. $errorInfo['error'] = true;
  70. } elseif ($errorInfo['deprecated'] === '') {
  71. $errorInfo['deprecated'] = $version;
  72. }
  73. }
  74. }
  75. }
  76. if (isset($itemArray['alternative']) === true) {
  77. $errorInfo['alternative'] = $itemArray['alternative'];
  78. }
  79. return $errorInfo;
  80. }
  81. /**
  82. * Get the error message template for suggesting an alternative for a specific sniff.
  83. *
  84. * @since 7.1.0
  85. *
  86. * @return string
  87. */
  88. protected function getAlternativeOptionTemplate()
  89. {
  90. return '; Use %s instead';
  91. }
  92. /**
  93. * Generates the error or warning for this item.
  94. *
  95. * @since 7.1.0
  96. *
  97. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  98. * @param int $stackPtr The position of the relevant token in
  99. * the stack.
  100. * @param array $itemInfo Base information about the item.
  101. * @param array $errorInfo Array with detail (version) information
  102. * relevant to the item.
  103. *
  104. * @return void
  105. */
  106. public function addError(File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
  107. {
  108. $itemName = $this->getItemName($itemInfo, $errorInfo);
  109. $error = $this->getErrorMsgTemplate();
  110. $errorCode = $this->stringToErrorCode($itemName);
  111. $data = array($itemName);
  112. if ($errorInfo['deprecated'] !== '') {
  113. $error .= 'deprecated since PHP %s and ';
  114. $errorCode .= 'Deprecated';
  115. $data[] = $errorInfo['deprecated'];
  116. }
  117. if ($errorInfo['removed'] !== '') {
  118. $error .= 'removed since PHP %s and ';
  119. $errorCode .= 'Removed';
  120. $data[] = $errorInfo['removed'];
  121. }
  122. // Remove the last 'and' from the message.
  123. $error = substr($error, 0, (\strlen($error) - 5));
  124. if ($errorInfo['alternative'] !== '') {
  125. $error .= $this->getAlternativeOptionTemplate();
  126. $data[] = $errorInfo['alternative'];
  127. }
  128. $error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
  129. $data = $this->filterErrorData($data, $itemInfo, $errorInfo);
  130. $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
  131. }
  132. }