コード例 #1
0
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the current token
  *                                        in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $this->currentFile = $phpcsFile;
     // We are only interested if this is the first open tag.
     if ($stackPtr !== 0) {
         if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) {
             return;
         }
     }
     $tokens = $phpcsFile->getTokens();
     // Find the next non whitespace token.
     $commentStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
     // Allow declare() statements at the top of the file.
     if ($tokens[$commentStart]['code'] === T_DECLARE) {
         $semicolon = $phpcsFile->findNext(T_SEMICOLON, $commentStart + 1);
         $commentStart = $phpcsFile->findNext(T_WHITESPACE, $semicolon + 1, null, true);
     }
     // Ignore vim header.
     if ($tokens[$commentStart]['code'] === T_COMMENT) {
         if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) {
             $commentStart = $phpcsFile->findNext(T_WHITESPACE, $commentStart + 1, null, true);
         }
     }
     $errorToken = $stackPtr + 1;
     if (!isset($tokens[$errorToken])) {
         $errorToken--;
     }
     if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
         // We are only interested if this is the first open tag.
         return;
     } elseif ($tokens[$commentStart]['code'] === T_COMMENT) {
         $error = 'You must use "/**" style comments for a file comment';
         $phpcsFile->addError($error, $errorToken, 'WrongStyle');
         return;
     } elseif ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT) {
         $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing');
         return;
     } else {
         // Extract the header comment docblock.
         $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT, $commentStart + 1, null, true);
         $commentEnd--;
         // Check if there is only 1 doc comment between the
         // open tag and class token.
         $nextToken = array(T_ABSTRACT, T_CLASS, T_FUNCTION, T_DOC_COMMENT);
         $commentNext = $phpcsFile->findNext($nextToken, $commentEnd + 1);
         if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) {
             // Found a class token right after comment doc block.
             $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, $commentNext, false, $phpcsFile->eolChar);
             if ($newlineToken !== false) {
                 $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $newlineToken + 1, $commentNext, false, $phpcsFile->eolChar);
                 if ($newlineToken === false) {
                     // No blank line between the class token and the doc block.
                     // The doc block is most likely a class comment.
                     $error = 'Missing file doc comment';
                     $phpcsFile->addError($error, $errorToken, 'Missing');
                     return;
                 }
             }
         }
         $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
         // Parse the header comment docblock.
         try {
             $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
             $this->commentParser->parse();
         } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
             $line = $e->getLineWithinComment() + $commentStart;
             $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
             return;
         }
         /** @var PHP_CodeSniffer_CommentParser_CommentElement $comment */
         $comment = $this->commentParser->getComment();
         if (is_null($comment)) {
             $error = 'File doc comment is empty';
             $phpcsFile->addError($error, $commentStart, 'Empty');
             return;
         }
         // No extra newline before short description.
         $short = $comment->getShortComment();
         $newlineSpan = strspn($short, $phpcsFile->eolChar);
         if ($short !== '' && $newlineSpan > 0) {
             $line = $newlineSpan > 1 ? 'newlines' : 'newline';
             $error = "Extra {$line} found before file comment short description";
             $phpcsFile->addError($error, $commentStart + 1, 'SpacingBefore');
         }
         $newlineCount = substr_count($short, $phpcsFile->eolChar) + 1;
         // Exactly one blank line between short and long description.
         $long = $comment->getLongComment();
         if (!empty($long)) {
             $between = $comment->getWhiteSpaceBetween();
             $newlineBetween = substr_count($between, $phpcsFile->eolChar);
             if ($newlineBetween !== 2) {
                 $error = 'There must be exactly one blank line between descriptions in file comment';
                 $phpcsFile->addError($error, $commentStart + $newlineCount + 1, 'DescriptionSpacing');
             }
             $newlineCount += $newlineBetween;
         }
         // Exactly one blank line before tags if short description is present.
         $tags = $this->commentParser->getTagOrders();
         if (count($tags) > 1 && $short !== '' && $newlineSpan > 0) {
             $newlineSpan = $comment->getNewlineAfter();
             if ($newlineSpan !== 2) {
                 $error = 'There must be exactly one blank line before the tags in file comment';
                 if ($long !== '') {
                     $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1;
                 }
                 $phpcsFile->addError($error, $commentStart + $newlineCount, 'SpacingBeforeTags');
             }
         }
         $this->processPHPVersion($commentStart, $commentEnd, $long);
         $this->processTags($commentStart, $commentEnd);
     }
 }