/** * Process a list of unknown tags. * * @param int $commentStart The position in the stack where the comment started. * @param int $commentEnd The position in the stack where the comment ended. * * @return void */ protected function processUnknownTags($commentStart, $commentEnd) { $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { $error = '@%s tag is not allowed in function comment'; $data = array($errorTag['tag']); $this->currentFile->addWarning($error, $commentStart + $errorTag['line'], 'TagNotAllowed', $data); } }
/** * 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']); } }
/** * 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(); $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 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'); } }