/** * Called to process class member vars. * * @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 processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $this->helper = new Helper($phpcsFile); // if we should skip this type we should do that if ($this->helper->shouldFilter()) { return; } $tokens = $phpcsFile->getTokens(); $commentToken = [T_COMMENT, T_DOC_COMMENT]; // Extract the var comment docblock. $commentEnd = $this->extractVarDocBlock($tokens, $commentToken, $stackPtr); if ($commentEnd === false) { return; } $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $commentString = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); // Parse the header comment docblock. try { $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($commentString, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $data = [$e->getMessage()]; $this->helper->addMessage($line, Helper::ERROR_PARSING, $data); return; } $comment = $this->commentParser->getComment(); if (($comment === null) === true) { $this->helper->addMessage($commentStart, Helper::EMPTY_DOC, ['Variable']); return; } // The first line of the comment should just be the /** code. $eolPos = strpos($commentString, $phpcsFile->eolChar); $firstLine = substr($commentString, 0, $eolPos); if ($firstLine !== '/**') { $this->helper->addMessage($commentStart, Helper::CONTENT_AFTER_OPEN); } // Check for a comment description. $this->checkForDescription($comment, $commentStart); // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { // Unknown tags are not parsed, do not process further. $data = [$errorTag['tag']]; $this->helper->addMessage($commentStart + $errorTag['line'], Helper::TAG_NOT_ALLOWED, $data); } // Check each tag. $this->processVar($commentStart, $commentEnd); $this->processSees($commentStart); // 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]) === '') { $this->helper->addMessage($commentEnd, Helper::SPACING_AFTER, ['variable']); } }
/** * 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->helper = new Helper($phpcsFile); // if we should skip this type we should do that if ($this->helper->shouldFilter()) { return; } $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']) { $this->helper->addMessage($stackPtr, Helper::MISSING, array('function')); } else { $this->helper->addMessage($stackPtr, Helper::WRONG_STYLE, array('function')); } return; } elseif ($code !== T_DOC_COMMENT) { $this->helper->addMessage($stackPtr, Helper::MISSING, array('function')); return; } elseif (trim($tokens[$commentEnd]['content']) !== '*/') { $this->helper->addMessage($commentEnd, Helper::WRONG_END, 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) { $this->helper->addMessage($stackPtr, Helper::MISSING, array('function')); 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; $this->helper->addMessage($line, Helper::FAILED_PARSE, array($e->getMessage())); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $this->helper->addMessage($commentStart, Helper::EMPTY_DOC, array('Function')); return; } // The first line of the comment should just be the /** code. $eolPos = strpos($commentString, $phpcsFile->eolChar); $firstLine = substr($commentString, 0, $eolPos); if ($firstLine !== '/**') { $this->helper->addMessage($commentStart, Helper::CONTENT_AFTER_OPEN); } // If the comment has an inherit doc note just move on if (preg_match('/\\{\\@inheritdoc\\}/', $commentString)) { return; } elseif (preg_match('/\\{?\\@?inherit[dD]oc\\}?/', $commentString)) { $this->helper->addMessage($commentStart, Helper::INCORRECT_INHERIT_DOC); return; } $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) === '') { $this->helper->addMessage($commentStart, Helper::MISSING_SHORT, array('function')); return; } // No extra newline before short description. $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $this->helper->addMessage($commentStart + 1, Helper::SPACING_BEFORE_SHORT, array('function')); } $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) { $this->helper->addMessage($commentStart + $newlineCount + 1, Helper::SPACING_BETWEEN, array('function')); } $newlineCount += $newlineBetween; $testLong = trim($long); if (preg_match('|\\p{Lu}|u', $testLong[0]) === 0) { $this->helper->addMessage($commentStart + $newlineCount, Helper::LONG_NOT_CAPITAL, array('Function')); } } // Exactly one blank line before tags. $params = $this->commentParser->getTagOrders(); if (count($params) > 1) { $newlineSpan = $comment->getNewlineAfter(); if ($newlineSpan !== 2) { if ($long !== '') { $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1; } $this->helper->addMessage($commentStart + $newlineCount, Helper::SPACING_BEFORE_TAGS, array('function')); $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) { $this->helper->addMessage($commentStart + 1, Helper::SHORT_SINGLE_LINE, array('Function')); } if (preg_match('|\\p{Lu}|u', $testShort[0]) === 0) { $this->helper->addMessage($commentStart + 1, Helper::SHORT_NOT_CAPITAL, array('Function')); } if ($lastChar !== '.') { $this->helper->addMessage($commentStart + 1, Helper::SHORT_FULL_STOP, array('Function')); } // Check for unknown/deprecated tags. // For example call: $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->helper->getCurrentFile()->eolChar) === false || trim($words[$lastPos - 2]) === '') { $this->helper->addMessage($commentEnd, Helper::SPACING_AFTER, array('function')); } }