AbstractComplexVersionSniff.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * Abstract base class for sniffs based on complex arrays with PHP version information.
  14. *
  15. * @since 7.1.0
  16. */
  17. abstract class AbstractComplexVersionSniff extends Sniff implements ComplexVersionInterface
  18. {
  19. /**
  20. * Handle the retrieval of relevant information and - if necessary - throwing of an
  21. * error/warning for an item.
  22. *
  23. * @since 7.1.0
  24. *
  25. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  26. * @param int $stackPtr The position of the relevant token in
  27. * the stack.
  28. * @param array $itemInfo Base information about the item.
  29. *
  30. * @return void
  31. */
  32. public function handleFeature(File $phpcsFile, $stackPtr, array $itemInfo)
  33. {
  34. $itemArray = $this->getItemArray($itemInfo);
  35. $errorInfo = $this->getErrorInfo($itemArray, $itemInfo);
  36. if ($this->shouldThrowError($errorInfo) === true) {
  37. $this->addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo);
  38. }
  39. }
  40. /**
  41. * Determine whether an error/warning should be thrown for an item based on collected information.
  42. *
  43. * @since 7.1.0
  44. *
  45. * @param array $errorInfo Detail information about an item.
  46. *
  47. * @return bool
  48. */
  49. abstract protected function shouldThrowError(array $errorInfo);
  50. /**
  51. * Get an array of the non-PHP-version array keys used in a sub-array.
  52. *
  53. * @since 7.1.0
  54. *
  55. * @return array
  56. */
  57. protected function getNonVersionArrayKeys()
  58. {
  59. return array();
  60. }
  61. /**
  62. * Retrieve a subset of an item array containing only the array keys which
  63. * contain PHP version information.
  64. *
  65. * @since 7.1.0
  66. *
  67. * @param array $itemArray Version and other information about an item.
  68. *
  69. * @return array Array with only the version information.
  70. */
  71. protected function getVersionArray(array $itemArray)
  72. {
  73. return array_diff_key($itemArray, array_flip($this->getNonVersionArrayKeys()));
  74. }
  75. /**
  76. * Get the item name to be used for the creation of the error code and in the error message.
  77. *
  78. * @since 7.1.0
  79. *
  80. * @param array $itemInfo Base information about the item.
  81. * @param array $errorInfo Detail information about an item.
  82. *
  83. * @return string
  84. */
  85. protected function getItemName(array $itemInfo, array $errorInfo)
  86. {
  87. return $itemInfo['name'];
  88. }
  89. /**
  90. * Get the error message template for a specific sniff.
  91. *
  92. * @since 7.1.0
  93. *
  94. * @return string
  95. */
  96. abstract protected function getErrorMsgTemplate();
  97. /**
  98. * Allow for concrete child classes to filter the error message before it's passed to PHPCS.
  99. *
  100. * @since 7.1.0
  101. *
  102. * @param string $error The error message which was created.
  103. * @param array $itemInfo Base information about the item this error message applies to.
  104. * @param array $errorInfo Detail information about an item this error message applies to.
  105. *
  106. * @return string
  107. */
  108. protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
  109. {
  110. return $error;
  111. }
  112. /**
  113. * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
  114. *
  115. * @since 7.1.0
  116. *
  117. * @param array $data The error data array which was created.
  118. * @param array $itemInfo Base information about the item this error message applies to.
  119. * @param array $errorInfo Detail information about an item this error message applies to.
  120. *
  121. * @return array
  122. */
  123. protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
  124. {
  125. return $data;
  126. }
  127. }