/** * 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; 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); // 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) { $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->addwarning($error, $commentstart + $newlinecount); $short = rtrim($short, $phpcsfile->eolChar . ' '); } } }