NewLanguageConstructsSniff.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\LanguageConstructs;
  11. use PHPCompatibility\AbstractNewFeatureSniff;
  12. use PHP_CodeSniffer_File as File;
  13. /**
  14. * Detect use of new PHP language constructs.
  15. *
  16. * PHP version All
  17. *
  18. * @link https://wiki.php.net/rfc/namespaceseparator
  19. * @link https://wiki.php.net/rfc/variadics
  20. * @link https://wiki.php.net/rfc/argument_unpacking
  21. *
  22. * @since 5.6
  23. * @since 7.1.0 Now extends the `AbstractNewFeatureSniff` instead of the base `Sniff` class..
  24. * @since 9.0.0 Detection for new operator tokens has been moved to the `NewOperators` sniff.
  25. */
  26. class NewLanguageConstructsSniff extends AbstractNewFeatureSniff
  27. {
  28. /**
  29. * A list of new language constructs, not present in older versions.
  30. *
  31. * The array lists : version number with false (not present) or true (present).
  32. * If's sufficient to list the first version where the keyword appears.
  33. *
  34. * @since 5.6
  35. *
  36. * @var array(string => array(string => bool|string))
  37. */
  38. protected $newConstructs = array(
  39. 'T_NS_SEPARATOR' => array(
  40. '5.2' => false,
  41. '5.3' => true,
  42. 'description' => 'the \ operator (for namespaces)',
  43. ),
  44. 'T_ELLIPSIS' => array(
  45. '5.5' => false,
  46. '5.6' => true,
  47. 'description' => 'the ... spread operator',
  48. ),
  49. );
  50. /**
  51. * Returns an array of tokens this test wants to listen for.
  52. *
  53. * @since 5.6
  54. *
  55. * @return array
  56. */
  57. public function register()
  58. {
  59. $tokens = array();
  60. foreach ($this->newConstructs as $token => $versions) {
  61. $tokens[] = constant($token);
  62. }
  63. return $tokens;
  64. }
  65. /**
  66. * Processes this test, when one of its tokens is encountered.
  67. *
  68. * @since 5.6
  69. *
  70. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  71. * @param int $stackPtr The position of the current token in
  72. * the stack passed in $tokens.
  73. *
  74. * @return void
  75. */
  76. public function process(File $phpcsFile, $stackPtr)
  77. {
  78. $tokens = $phpcsFile->getTokens();
  79. $tokenType = $tokens[$stackPtr]['type'];
  80. $itemInfo = array(
  81. 'name' => $tokenType,
  82. );
  83. $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
  84. }
  85. /**
  86. * Get the relevant sub-array for a specific item from a multi-dimensional array.
  87. *
  88. * @since 7.1.0
  89. *
  90. * @param array $itemInfo Base information about the item.
  91. *
  92. * @return array Version and other information about the item.
  93. */
  94. public function getItemArray(array $itemInfo)
  95. {
  96. return $this->newConstructs[$itemInfo['name']];
  97. }
  98. /**
  99. * Get an array of the non-PHP-version array keys used in a sub-array.
  100. *
  101. * @since 7.1.0
  102. *
  103. * @return array
  104. */
  105. protected function getNonVersionArrayKeys()
  106. {
  107. return array('description');
  108. }
  109. /**
  110. * Retrieve the relevant detail (version) information for use in an error message.
  111. *
  112. * @since 7.1.0
  113. *
  114. * @param array $itemArray Version and other information about the item.
  115. * @param array $itemInfo Base information about the item.
  116. *
  117. * @return array
  118. */
  119. public function getErrorInfo(array $itemArray, array $itemInfo)
  120. {
  121. $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
  122. $errorInfo['description'] = $itemArray['description'];
  123. return $errorInfo;
  124. }
  125. /**
  126. * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
  127. *
  128. * @since 7.1.0
  129. *
  130. * @param array $data The error data array which was created.
  131. * @param array $itemInfo Base information about the item this error message applies to.
  132. * @param array $errorInfo Detail information about an item this error message applies to.
  133. *
  134. * @return array
  135. */
  136. protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
  137. {
  138. $data[0] = $errorInfo['description'];
  139. return $data;
  140. }
  141. }