/** * 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) { $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } $this->currentFile = $phpcsFile; $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['nested_parenthesis'])) { return; } if (!isset($tokens[$stackPtr]['conditions'])) { return; } end($tokens[$stackPtr]['conditions']); $ckey = key($tokens[$stackPtr]['conditions']); if (!isset($tokens[$ckey]) || $tokens[$ckey]['code'] !== T_CLASS && $tokens[$ckey]['code'] !== T_INTERFACE) { 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) { $error = 'You must use "/**" style comments for a variable comment'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.4') . $error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing variable doc comment'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.1') . $error, $stackPtr); 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($this->getReqPrefix('REQ.PHP.4.6.1') . 'Missing function doc comment', $stackPtr); return; } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.1') . 'Missing function doc comment', $stackPtr); return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Variable doc comment is empty'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.2') . $error, $commentStart); return; } $this->processTags($commentStart, $commentEnd); // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before variable comment short description"; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, $commentStart + 1); } $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 variable comment'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.19') . $error, $commentStart + $newlineCount + 1); } $newlineCount += $newlineBetween; } // 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($this->getReqPrefix('REQ.PHP.4.1.18') . $error, $commentStart + $newlineCount); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } }
/** * 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) { $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } $this->currentFile = $phpcsFile; $tokens = $phpcsFile->getTokens(); // 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) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr, 'WrongStyle'); return; } else { if ($code !== T_DOC_COMMENT) { $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); 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; } } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing'); return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $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; } $this->processParams($commentStart); $this->processReturn($commentStart, $commentEnd); $this->processThrows($commentStart); // No extra newline before short description. $short = $comment->getShortComment(); $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, 'SpacingAfterShort'); } $newlineCount += $newlineBetween; } // 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 . ' '); } } }
/** * 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'); } }
/** * Processes this test, when one of its tokens is encountered * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned * @param integer $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; $this->_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 = $this->_tokens[$commentEnd]['code']; if ($code === T_COMMENT) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing function doc comment'; $phpcsFile->addError($error, $stackPtr); 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); return; } $this->_functionToken = $stackPtr; foreach ($this->_tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS or $condition === T_INTERFACE) { $this->_classToken = $condPtr; break; } } // Find the first doc comment $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->_commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->_commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line); return; } $comment = $this->_commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($error, $commentStart); return; } $tagOrder = $this->_commentParser->getTagOrders(); $cnt = -1; $con = 'comment'; foreach ($tagOrder as $tag => $content) { switch ($content) { case 'comment': $cnt = $tag; break; case 'param': if ($con !== 'comment' and $con !== 'param') { $error = 'The @param tag is in the wrong order; the tag follows the function comment'; $this->_currentFile->addError($error, $commentStart + $tag); } break; case 'since': if ($con !== 'comment' and $con !== 'param') { $error = 'The @since tag is in the wrong order; the tag follows @param'; $this->_currentFile->addError($error, $commentStart + $tag); } break; case 'see': if ($con !== 'comment' and $con !== 'param' and $con !== 'since' and $con !== 'see') { $error = 'The @see tag is in the wrong order; the tag follows @since or @param'; $this->_currentFile->addError($error, $commentStart + $tag); } break; case 'throws': if ($con !== 'comment' and $con !== 'param' and $con !== 'since' and $con !== 'see' and $con !== 'throws') { $error = 'The @throws tag is in the wrong order; the tag follows @see or @since or @param'; $this->_currentFile->addError($error, $commentStart + $tag); } break; case 'return': if ($con !== 'comment' and $con !== 'param' and $con !== 'since' and $con !== 'see' and $con !== 'throws') { $error = 'The @throws tag is in the wrong order; the tag follows @see or @since or @param'; $this->_currentFile->addError($error, $commentStart + $tag); } break; default: $error = 'The @' . $content . ' tag is not allowed'; $this->_currentFile->addError($error, $commentStart + $tag); break; } $con = $content; } $this->_processParams($commentStart, $commentEnd); $this->_processSince($commentStart); $this->_processSees($commentStart); $this->_processThrows($commentStart); $this->_processReturn($commentStart, $commentEnd); // Check for a comment description $short = $comment->getShortComment(); if (trim($short) === '') { $error = 'Missing short description in function doc comment'; $phpcsFile->addError($error, $commentStart); return; } // No extra newline before short description $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' and $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before function comment short description"; $phpcsFile->addError($error, $commentStart + 1); } $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); } $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); } } // 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); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } $testShort = trim($short); $lastChar = $testShort[strlen($testShort) - 1]; if (preg_match('|[A-Z]|', $testShort[0]) === 0) { $error = 'Function comment short description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + 1); } // Check for unknown/deprecated tags $unknownTags = $this->_commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $error = "@{$errorTag['tag']} tag is not allowed in function comment"; $phpcsFile->addWarning($error, $commentStart + $errorTag['line']); } }
/** * 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) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing function doc comment'; $phpcsFile->addError($error, $stackPtr); 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); 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); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($error, $commentStart); 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); } $this->processParams($commentStart, $commentEnd); $this->processSince($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); return; } // No extra newline before short description. $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before function comment short description"; $phpcsFile->addError($error, $commentStart + 1); } $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); } $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)); // } } //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); $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); } // if (preg_match('|[A-Z]|', $testShort[0]) === 0) { // $error = 'Function comment short description must start with a capital letter'; // $phpcsFile->addError($error, ($commentStart + 1)); // } // if ($lastChar !== '.') { // $error = 'Function comment short description must end with a full stop'; // $phpcsFile->addError($error, ($commentStart + 1)); // } // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $error = "@{$errorTag['tag']} tag is not allowed in function comment"; $phpcsFile->addWarning($error, $commentStart + $errorTag['line']); } }
/** * Ths function is mainly copied from PEAR_Sniffs_Commenting_FunctionCommentSniff. * THX for this! * * @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 mixed */ protected function getDocCommentOfFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return null; } $tokens = $phpcsFile->getTokens(); // If the token that we found was a class or a function, then this // function has no doc comment. $code = $tokens[$commentEnd]['code']; // If the comment a "//" comment, get out of here if ($code === T_COMMENT) { return null; // If there is no doc comment block, get out of here, too } else { if ($code !== T_DOC_COMMENT) { return null; } } // 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) { return null; } $this->classToken = null; foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS || $condition === T_INTERFACE) { $this->classToken = $condPtr; break; } } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { return null; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { return null; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { return null; } return $this->commentParser; }
/** * 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) { $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } $tokens = $phpcsFile->getTokens(); // 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) { return; } elseif ($code !== T_DOC_COMMENT) { 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) { return; } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { return; } // Function doc comment is empty $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { return; } /** * Here we go. * Now we have a doc comment. Parse the annotations for @author-Tags! */ $error = '@author tag should not be used in function or method phpDoc comment blocks - only at class level'; $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $tagInfo) { if ($tagInfo['tag'] !== 'author') { continue; } $phpcsFile->addError($error, $tagInfo['line'] + $commentStart, 'NoAuthorAnnotationInFunctionDocComment'); } return null; }
/** * 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(); $commentEnd = $this->findDocComment($stackPtr, $phpcsFile); if ($commentEnd === FALSE) { 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; $ignore[] = T_COMMENT; $prevToken = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true); if ($prevToken !== $commentEnd) { //$phpcsFile->addError('Missing function doc comment', $stackPtr); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_MISSING', array(), $stackPtr); return; } // Check for empty lines between method comment and declaration $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $newlineToken + 1, $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { // Blank line between the class and the doc block. // The doc block is most likely a file comment. //$error = "Missing $type doc comment"; //$phpcsFile->addError($error, ($stackPtr + 1)); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_MISSING', array(), $stackPtr + 1); return; } } //end if $this->_functionToken = $stackPtr; $this->_classToken = null; foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS || $condition === T_INTERFACE) { $this->_classToken = $condPtr; break; } } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { //$phpcsFile->addError('Missing function doc comment', $stackPtr); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_MISSING', array(), $stackPtr); return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { //$error = 'Function doc comment is empty'; //$phpcsFile->addError($error, $commentStart); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_EMPTY', array(), $commentStart); return; } $this->processParams($commentStart); $this->processThrows($commentStart); $functionToken = $tokens[$this->_functionToken]; if ($phpcsFile->findNext(T_RETURN, $functionToken['scope_opener'], $functionToken['scope_closer'])) { $this->processReturn($commentStart, $commentEnd); } // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { //$line = ($newlineSpan > 1) ? 'newlines' : 'newline'; //$error = "Extra $line found before function comment short description"; //$phpcsFile->addError($error, ($commentStart + 1)); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_NEWLINE_BEFORE_SHORT_DESCRIPTION', array(), $commentStart + 1); } $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)); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_EXTRA_NEWLINE_BETWEEN_DESCRIPTION', array(), $commentStart + $newlineCount + 1); } $newlineCount += $newlineBetween; } // 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)); $phpcsFile->addEvent('XP_FUNCTION_COMMENT_EXTRA_NEWLINE_BEFORE_TAGS', array(), $commentStart + $newlineCount); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } }
/** * 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) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing function doc comment'; $phpcsFile->addError($error, $stackPtr); return; } } $this->_functionToken = $stackPtr; $classToken = $phpcsFile->findPrevious(array(T_CLASS, T_INTERFACE), $stackPtr - 1); if ($classToken !== false) { $this->_classToken = $classToken; } // Find the first doc comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($error, $commentStart); return; } $this->_processParams($commentStart, $commentEnd); $this->_processSince($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); return; } // No extra newline before short description. $newlineCount = 0; $newlineSpan = strspn($short, "\n"); if ($short !== '' && $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before function comment short description"; $phpcsFile->addError($error, $commentStart + 1); } $newlineCount = substr_count($short, "\n") + 1; // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); $newlineBetween = substr_count($between, "\n"); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in function comment'; $phpcsFile->addError($error, $commentStart + $newlineCount + 1); } $newlineCount += $newlineBetween; } // 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, "\n") - $newlineSpan + 1; } $phpcsFile->addError($error, $commentStart + $newlineCount); $short = rtrim($short, "\n "); } } // Short description must be single line and end with a full stop. $lastChar = $short[strlen($short) - 1]; if (substr_count($short, "\n") !== 0) { $error = 'Function comment short description must be on a single line'; $phpcsFile->addError($error, $commentStart + 1); } if ($lastChar !== '.') { $error = 'Function comment short description must end with a full stop'; $phpcsFile->addError($error, $commentStart + 1); } // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $error = "@{$errorTag['tag']} tag is not allowed in function comment"; $phpcsFile->addWarning($error, $commentStart + $errorTag['line']); } }
/** * Processes this test, when one of its tokens is encountered * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned * @param integer $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; $this->_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 = $this->_tokens[$commentEnd]['code']; if ($code === T_COMMENT) { $phpcsFile->addEvent('STYLE_FUNCTION_COMMENT', array(), $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $phpcsFile->addEvent('MISSING_FUNCTION_COMMENT', array(), $stackPtr); 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->addEvent('STYLE_FUNCTION_COMMENT', array(), $stackPtr); return; } $this->_functionToken = $stackPtr; foreach ($this->_tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS or $condition === T_INTERFACE) { $this->_classToken = $condPtr; break; } } // Find the first doc comment $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->_commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->_commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addEvent('ERROR_PARSING_FUNCTION_COMMENT', array(), $line); return; } $comment = $this->_commentParser->getComment(); if (is_null($comment) === true) { $phpcsFile->addEvent('FUNCTION_COMMENT_EMPTY', array(), $commentStart); return; } $tagOrder = $this->_commentParser->getTagOrders(); $cnt = -1; $con = 'comment'; foreach ($tagOrder as $tag => $content) { switch ($content) { case 'comment': $cnt = $tag; break; case 'param': if ($con !== 'comment' and $con !== 'param') { $this->_currentFile->addEvent('PARAM_TAG_ORDER_FUNCTION_COMMENT', array(), $commentStart + $tag); } break; case 'return': if ($con !== 'comment' and $con !== 'param') { $this->_currentFile->addEvent('PARAM_TAG_ORDER_FUNCTION_COMMENT', array(), $commentStart + $tag); } break; default: $this->_currentFile->addEvent('TAG_NOTALLOWED_FUNCTION_COMMENT', array('tagname' => $content), $commentStart + $tag); break; } $con = $content; } $this->_processParams($commentStart, $commentEnd); $this->_processReturn($commentStart, $commentEnd); // Check for a comment description $short = $comment->getShortComment(); if (trim($short) === '') { $phpcsFile->addEvent('MISSING_SHORT_DESC_FUNCTION_COMMENT', array(), $commentStart); return; } // No extra newline before short description $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' and $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $phpcsFile->addEvent('EXTRA_LINE_FUNCTION_COMMENT', array('line' => $line), $commentStart + 1); } $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 = ''; $phpcsFile->addEvent('BLANK_LINE_FUNCTION_COMMENT', array(), $commentStart + $newlineCount + 1); } } // 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; } $phpcsFile->addEvent('BLANK_LINE_TAGS_FUNCTION_COMMENT', array(), $commentStart + $newlineCount); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } // Check for unknown/deprecated tags $unknownTags = $this->_commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $tagname = $errorTag['tag']; $phpcsFile->addEvent('TAG_NOTALLOWED_FUNCTION_COMMENT', array('tagname' => $tagname), $commentStart + $errorTag['line']); } }
/** * 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) { $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } $this->currentFile = $phpcsFile; $tokens = $phpcsFile->getTokens(); // 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) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $phpcsFile->addError('Missing function doc comment', $stackPtr); 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); 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; } } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { $phpcsFile->addError('Missing function doc comment', $stackPtr); return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($error, $commentStart); return; } $this->processParams($commentStart); $this->processReturn($commentStart, $commentEnd); $this->processThrows($commentStart); // Check if hook_ implementation is formated correctly if (preg_match('/(Implement[^\\n]+?hook_[^\\n]+)/', $comment->getShortComment(), $matches)) { $formattingIssue = 0; if (!strstr($matches[0], 'Implements ')) { $formattingIssue++; } if (!preg_match('/ hook_[\\S\\(\\)]+\\.$/', $matches[0])) { $formattingIssue++; } if ($formattingIssue) { $phpcsFile->addWarning('Format should be * Implements hook_foo().', $commentStart + 1); } } //getLongComment // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before function comment short description"; $phpcsFile->addError($error, $commentStart + 1); } $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); } $newlineCount += $newlineBetween; } // Exactly one blank line before tags. $params = $this->commentParser->getTagOrders(); if (count($params) > 1) { //@TODO implement Drupal/Doxygen specific sniffs /*$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)); $short = rtrim($short, $phpcsFile->eolChar.' '); }*/ } }
/** * Processes this test, when one of its tokens is encountered * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned * @param integer $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; $this->_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 = $this->_tokens[$commentEnd]['code']; if ($code === T_COMMENT) { $phpcsFile->addError('Il faut utiliser "/**" comme style pour le commentaire de fonction', $stackPtr, 'StyleFunctionComment'); return; } else { if ($code !== T_DOC_COMMENT) { $phpcsFile->addError('Commentaire de fonction manquant', $stackPtr, 'MissingFunctionComment'); 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('Il faut utiliser "/**" comme style pour le commentaire de fonction', $stackPtr, 'StyleFunctionComment'); return; } $this->_functionToken = $stackPtr; foreach ($this->_tokens[$stackPtr]['conditions'] as $condPtr => $condition) { if ($condition === T_CLASS or $condition === T_INTERFACE) { $this->_classToken = $condPtr; break; } } // Find the first doc comment $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->_commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->_commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError('Erreur de traitement commentaire de fonction', $line, 'ErrorParsingFunctionComment'); return; } $comment = $this->_commentParser->getComment(); if (is_null($comment) === true) { $phpcsFile->addError('Le commentaire de fonction est vide', $commentStart, 'FunctionCommentEmpty'); return; } $tagOrder = $this->_commentParser->getTagOrders(); $cnt = -1; $con = 'comment'; foreach ($tagOrder as $tag => $content) { switch ($content) { case 'comment': $cnt = $tag; break; case 'param': if ($con !== 'comment' and $con !== 'param') { $this->_currentFile->addError('Le tag @param doit suivre le commentaire de la fonction', $commentStart + $tag, 'ParamTagOrderFunctionComment'); } break; case 'return': if ($con !== 'comment' and $con !== 'param') { $this->_currentFile->addError('Le tag @param doit suivre le commentaire de la fonction', $commentStart + $tag, 'ParamTagOrderFunctionComment'); } break; default: $this->_currentFile->addWarning("Le tag @{$content} est interdit", $commentStart + $tag, 'TagNotAllowedFunctionComment'); break; } $con = $content; } $this->_processParams($commentStart, $commentEnd); $this->_processReturn($commentStart, $commentEnd); // Check for a comment description $short = $comment->getShortComment(); if (trim($short) === '') { $phpcsFile->addError("La description courte dans le commentaire de fonction manquante", $commentStart, 'MissingShortDescFunctionComment'); return; } // No extra newline before short description $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' and $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $phpcsFile->addError("{$line} ligne(s) trouvée(s) en plus avant le commentaire court de fonction", $commentStart + 1, 'ExtraLineFunctionComment'); } $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 = ''; $phpcsFile->addError("Il doit y avoir 1 ligne vide après la description courte dans le commentaire de fonction", $commentStart + $newlineCount + 1, 'BlankLineFunctionComment'); } } // 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; } $phpcsFile->addError("Il doit y avoir 1 ligne vide avant les tags dans le commentaire de fonction", $commentStart + $newlineCount, 'BlankLineTagsFunctionComment'); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } // Check for unknown/deprecated tags $unknownTags = $this->_commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $tagname = $errorTag['tag']; $phpcsFile->addWarning("Le tag @{$tagname} est interdit", $commentStart + $errorTag['line'], 'TagNotAllowedFunctionComment'); } }
/** * 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) { $tokens = $phpcsFile->getTokens(); $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); if ($tokens[$next]['code'] !== T_STRING) { return; } $throwExists = isset($tokens[$stackPtr]['scope_closer']) ? $phpcsFile->findNext(T_THROW, $stackPtr + 1, $tokens[$stackPtr]['scope_closer'] - 1) : false; if (false === $throwExists) { if (isset($this->tags['throws'])) { unset($this->tags['throws']); } } else { $tags = $this->tags; $this->tags = array_slice($tags, 0, 2); $this->tags['throws'] = array('required' => true, 'allow_multiple' => true, 'order_text' => 'follows @return'); $this->tags += array_slice($tags, 2); } $find = array(T_COMMENT, T_DOC_COMMENT, T_CLASS, T_FUNCTION, T_OPEN_TAG); $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1); if ($commentEnd === false) { return; } $this->currentFile = $phpcsFile; // 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) { $error = 'You must use "/**" style comments for a function comment'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.5.6') . $error, $stackPtr); return; } else { if ($code !== T_DOC_COMMENT) { $error = 'Missing function doc comment'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.5.1') . $error, $stackPtr); 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($this->getReqPrefix('REQ.PHP.4.5.1') . 'Missing function doc comment', $stackPtr); 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; } } // If the first T_OPEN_TAG is right before the comment, it is probably // a file comment. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { // Is this the first open tag? if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, $prevToken - 1) === false) { $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.5.1') . 'Missing function doc comment', $stackPtr); return; } } $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { $this->commentParser = new XLite_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Function doc comment is empty'; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.5.2') . $error, $commentStart); return; } $this->checkAccess($stackPtr, $commentStart, $commentEnd); $this->processTags($commentStart, $commentEnd); // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = $newlineSpan > 1 ? 'newlines' : 'newline'; $error = "Extra {$line} found before function comment short description"; $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, $commentStart + 1); } $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($this->getReqPrefix('REQ.PHP.4.1.19') . $error, $commentStart + $newlineCount + 1); } $newlineCount += $newlineBetween; } // 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($this->getReqPrefix('REQ.PHP.4.1.18') . $error, $commentStart + $newlineCount); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } }