/** * 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; $tokens = $phpcsFile->getTokens(); $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } // If the token that we found was a class or a function, then this // function has no doc comment. $code = $tokens[$commentEnd]['code']; if ($code === T_COMMENT) { // The function might actually be missing a comment, and this last comment // found is just commenting a bit of code on a line. So if it is not the // only thing on the line, assume we found nothing. $prevContent = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $commentEnd); if ($tokens[$commentEnd]['line'] === $tokens[$commentEnd]['line']) { $error = 'Missing function doc comment'; $phpcsFile->addError($error, $stackPtr, 'Missing'); } else { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); } return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing function doc comment'; $phpcsFile->addError($error, $stackPtr, 'Missing'); return; } else { if (trim($tokens[$commentEnd]['content']) !== '*/') { $error = 'You must use "*/" to end a function comment; found "%s"'; $phpcsFile->addError($error, $commentEnd, 'WrongEnd', array(trim($tokens[$commentEnd]['content']))); return; } } } // If there is any code between the function keyword and the doc block // then the doc block is not for us. $ignore = PHP_CodeSniffer_Tokens::$scopeModifiers; $ignore[] = T_STATIC; $ignore[] = T_WHITESPACE; $ignore[] = T_ABSTRACT; $ignore[] = T_FINAL; $prevToken = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true); if ($prevToken !== $commentEnd) { $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); return; } $this->_functionToken = $stackPtr; $this->_classToken = null; foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS || $condition === T_INTERFACE) { $this->_classToken = $condPtr; break; } } // Find the first doc comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $commentString = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($commentString, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line, 'FailedParse'); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($error, $commentStart, 'Empty'); return; } // The first line of the comment should just be the /** code. $eolPos = strpos($commentString, $phpcsFile->eolChar); $firstLine = substr($commentString, 0, $eolPos); if ($firstLine !== '/**') { $error = 'The open comment tag must be the only content on the line'; $phpcsFile->addError($error, $commentStart, 'ContentAfterOpen'); } $this->processParams($commentStart, $commentEnd); $this->processSees($commentStart); $this->processReturn($commentStart, $commentEnd); $this->processThrows($commentStart); // Check for a comment description. $short = $comment->getShortComment(); if (trim($short) === '') { $error = 'Missing short description in function doc comment'; $phpcsFile->addError($error, $commentStart, 'MissingShort'); return; } // No extra newline before short description. $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $error = 'Extra newline(s) found before function comment short description'; $phpcsFile->addError($error, $commentStart + 1, 'SpacingBeforeShort'); } $newlineCount = substr_count($short, $phpcsFile->eolChar) + 1; // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in function comment'; $phpcsFile->addError($error, $commentStart + $newlineCount + 1, 'SpacingBetween'); } $newlineCount += $newlineBetween; $testLong = trim($long); if (preg_match('|[A-Z]|', $testLong[0]) === 0) { $error = 'Function comment long description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + $newlineCount, 'LongNotCapital'); } } //end if // Exactly one blank line before tags. $params = $this->commentParser->getTagOrders(); if (count($params) > 1) { $newlineSpan = $comment->getNewlineAfter(); if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in function comment'; if ($long !== '') { $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1; } $phpcsFile->addError($error, $commentStart + $newlineCount, 'SpacingBeforeTags'); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } // Short description must be single line and end with a full stop. $testShort = trim($short); $lastChar = $testShort[strlen($testShort) - 1]; if (substr_count($testShort, $phpcsFile->eolChar) !== 0) { $error = 'Function comment short description must be on a single line'; $phpcsFile->addError($error, $commentStart + 1, 'ShortSingleLine'); } if (preg_match('|[A-Z]|', $testShort[0]) === 0) { $error = 'Function comment short description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + 1, 'ShortNotCapital'); } if ($lastChar !== '.') { $error = 'Function comment short description must end with a full stop'; $phpcsFile->addError($error, $commentStart + 1, 'ShortFullStop'); } // Check for unknown/deprecated tags. $this->processUnknownTags($commentStart, $commentEnd); // The last content should be a newline and the content before // that should not be blank. If there is more blank space // then they have additional blank lines at the end of the comment. $words = $this->commentParser->getWords(); $lastPos = count($words) - 1; if (trim($words[$lastPos - 1]) !== '' || strpos($words[$lastPos - 1], $this->currentFile->eolChar) === false || trim($words[$lastPos - 2]) === '') { $error = 'Additional blank lines found at end of function comment'; $this->currentFile->addError($error, $commentEnd, 'SpacingAfter'); } }