Unverified Commit 9fc079a2 authored by Claudiu Cristea's avatar Claudiu Cristea Committed by GitHub
Browse files

fix(InlineComment): Allow PHP attributes between docblock and function (#157)

parent 6fc09fdc
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -92,7 +92,18 @@ class FunctionCommentSniff implements Sniff
        $find   = Tokens::$methodPrefixes;
        $find[] = T_WHITESPACE;

        $beforeFunction = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
        if ($tokens[$beforeFunction]['code'] === T_ATTRIBUTE_END
            && $tokens[$tokens[$beforeFunction]['attribute_opener']]['code'] === T_ATTRIBUTE
        ) {
            // It's an attribute, such as #[\ReturnTypeWillChange].
            $attributeLines = ($tokens[$beforeFunction]['line'] - $tokens[$tokens[$beforeFunction]['attribute_opener']]['line'] + 1);
            $commentEnd     = $phpcsFile->findPrevious($find, ($tokens[$beforeFunction]['attribute_opener'] - 1), null, true);
        } else {
            $attributeLines = 0;
            $commentEnd     = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
        }

        $beforeCommentEnd = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($commentEnd - 1), null, true);
        if (($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
            && $tokens[$commentEnd]['code'] !== T_COMMENT)
@@ -151,7 +162,7 @@ class FunctionCommentSniff implements Sniff
            }
        }//end foreach

        if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
        if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - $attributeLines - 1)) {
            $error = 'There must be no blank lines after the function comment';
            $fix   = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter');
            if ($fix === true) {
+1 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ class InlineCommentSniff implements Sniff
            );

            $ignore = [
                T_ATTRIBUTE,
                T_CLASS,
                T_INTERFACE,
                T_TRAIT,
+14 −0
Original line number Diff line number Diff line
@@ -1800,3 +1800,17 @@ interface Test5Interface {
  public function test3();

}

/**
 * Test PHP attributes.
 */
class TestPhpAttributes {

  /**
   * Tests method with PHP attribute and docblock.
   */
  #[\ReturnTypeWillChange]
  public function attributes(): void {
  }

}