NewExceptionsFromToStringSniff.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\FunctionDeclarations;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * As of PHP 7.4, throwing exceptions from a `__toString()` method is allowed.
  16. *
  17. * PHP version 7.4
  18. *
  19. * @link https://wiki.php.net/rfc/tostring_exceptions
  20. * @link https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
  21. *
  22. * @since 9.2.0
  23. */
  24. class NewExceptionsFromToStringSniff extends Sniff
  25. {
  26. /**
  27. * Valid scopes for the __toString() method to live in.
  28. *
  29. * @since 9.2.0
  30. * @since 9.3.0 Visibility changed from `public` to `protected`.
  31. *
  32. * @var array
  33. */
  34. protected $ooScopeTokens = array(
  35. 'T_CLASS' => true,
  36. 'T_TRAIT' => true,
  37. 'T_ANON_CLASS' => true,
  38. );
  39. /**
  40. * Tokens which should be ignored when they preface a function declaration
  41. * when trying to find the docblock (if any).
  42. *
  43. * Array will be added to in the register() method.
  44. *
  45. * @since 9.3.0
  46. *
  47. * @var array
  48. */
  49. private $docblockIgnoreTokens = array(
  50. \T_WHITESPACE => \T_WHITESPACE,
  51. );
  52. /**
  53. * Returns an array of tokens this test wants to listen for.
  54. *
  55. * @since 9.2.0
  56. *
  57. * @return array
  58. */
  59. public function register()
  60. {
  61. // Enhance the array of tokens to ignore for finding the docblock.
  62. $this->docblockIgnoreTokens += Tokens::$methodPrefixes;
  63. if (isset(Tokens::$phpcsCommentTokens)) {
  64. $this->docblockIgnoreTokens += Tokens::$phpcsCommentTokens;
  65. }
  66. return array(\T_FUNCTION);
  67. }
  68. /**
  69. * Processes this test, when one of its tokens is encountered.
  70. *
  71. * @since 9.2.0
  72. *
  73. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  74. * @param int $stackPtr The position of the current token
  75. * in the stack passed in $tokens.
  76. *
  77. * @return void
  78. */
  79. public function process(File $phpcsFile, $stackPtr)
  80. {
  81. if ($this->supportsBelow('7.3') === false) {
  82. return;
  83. }
  84. $tokens = $phpcsFile->getTokens();
  85. if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
  86. // Abstract function, interface function, live coding or parse error.
  87. return;
  88. }
  89. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  90. if (strtolower($functionName) !== '__tostring') {
  91. // Not the right function.
  92. return;
  93. }
  94. if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) === false) {
  95. // Function, not method.
  96. return;
  97. }
  98. /*
  99. * Examine the content of the function.
  100. */
  101. $error = 'Throwing exceptions from __toString() was not allowed prior to PHP 7.4';
  102. $throwPtr = $tokens[$stackPtr]['scope_opener'];
  103. $errorThrown = false;
  104. do {
  105. $throwPtr = $phpcsFile->findNext(\T_THROW, ($throwPtr + 1), $tokens[$stackPtr]['scope_closer']);
  106. if ($throwPtr === false) {
  107. break;
  108. }
  109. $conditions = $tokens[$throwPtr]['conditions'];
  110. $conditions = array_reverse($conditions, true);
  111. $inTryCatch = false;
  112. foreach ($conditions as $ptr => $type) {
  113. if ($type === \T_TRY) {
  114. $inTryCatch = true;
  115. break;
  116. }
  117. if ($ptr === $stackPtr) {
  118. // Don't check the conditions outside the function scope.
  119. break;
  120. }
  121. }
  122. if ($inTryCatch === false) {
  123. $phpcsFile->addError($error, $throwPtr, 'Found');
  124. $errorThrown = true;
  125. }
  126. } while (true);
  127. if ($errorThrown === true) {
  128. // We've already thrown an error for this method, no need to examine the docblock.
  129. return;
  130. }
  131. /*
  132. * Check whether the function has a docblock and if so, whether it contains a @throws tag.
  133. *
  134. * {@internal This can be partially replaced by the findCommentAboveFunction()
  135. * utility function in due time.}
  136. */
  137. $commentEnd = $phpcsFile->findPrevious($this->docblockIgnoreTokens, ($stackPtr - 1), null, true);
  138. if ($commentEnd === false || $tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG) {
  139. return;
  140. }
  141. $commentStart = $tokens[$commentEnd]['comment_opener'];
  142. foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
  143. if ($tokens[$tag]['content'] !== '@throws') {
  144. continue;
  145. }
  146. // Found a throws tag.
  147. $phpcsFile->addError($error, $stackPtr, 'ThrowsTagFoundInDocblock');
  148. break;
  149. }
  150. }
  151. }