/** * Called to process class member vars. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $this->helper = new M2_Sniffs_Annotations_Helper($phpcsFile); // if we should skip this type we should do that if ($this->helper->shouldFilter()) { return; } $tokens = $phpcsFile->getTokens(); $commentToken = [T_COMMENT, T_DOC_COMMENT]; // Extract the var comment docblock. $commentEnd = $this->extractVarDocBlock($tokens, $commentToken, $stackPtr); if ($commentEnd === false) { return; } $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, $commentEnd - 1, null, true) + 1; $commentString = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); // Parse the header comment docblock. try { $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($commentString, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $data = [$e->getMessage()]; $this->helper->addMessage($line, M2_Sniffs_Annotations_Helper::ERROR_PARSING, $data); return; } $comment = $this->commentParser->getComment(); if (($comment === null) === true) { $this->helper->addMessage($commentStart, M2_Sniffs_Annotations_Helper::EMPTY_DOC, ['Variable']); return; } // The first line of the comment should just be the /** code. $eolPos = strpos($commentString, $phpcsFile->eolChar); $firstLine = substr($commentString, 0, $eolPos); if ($firstLine !== '/**') { $this->helper->addMessage($commentStart, M2_Sniffs_Annotations_Helper::CONTENT_AFTER_OPEN); } // Check for a comment description. $this->checkForDescription($comment, $commentStart); // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { // Unknown tags are not parsed, do not process further. $data = [$errorTag['tag']]; $this->helper->addMessage($commentStart + $errorTag['line'], M2_Sniffs_Annotations_Helper::TAG_NOT_ALLOWED, $data); } // Check each tag. $this->processVar($commentStart, $commentEnd); $this->processSees($commentStart); // The last content should be a newline and the content before // that should not be blank. If there is more blank space // then they have additional blank lines at the end of the comment. $words = $this->commentParser->getWords(); $lastPos = count($words) - 1; if (trim($words[$lastPos - 1]) !== '' || strpos($words[$lastPos - 1], $this->currentFile->eolChar) === false || trim($words[$lastPos - 2]) === '') { $this->helper->addMessage($commentEnd, M2_Sniffs_Annotations_Helper::SPACING_AFTER, ['variable']); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $this->currentFile = $phpcsFile; // We are only interested if this is the first open tag. if ($stackPtr !== 0) { if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) { return; } } $tokens = $phpcsFile->getTokens(); // Find the next non whitespace token. $commentStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); // Allow declare() statements at the top of the file. if ($tokens[$commentStart]['code'] === T_DECLARE) { $semicolon = $phpcsFile->findNext(T_SEMICOLON, $commentStart + 1); $commentStart = $phpcsFile->findNext(T_WHITESPACE, $semicolon + 1, null, true); } // Ignore vim header. if ($tokens[$commentStart]['code'] === T_COMMENT) { if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) { $commentStart = $phpcsFile->findNext(T_WHITESPACE, $commentStart + 1, null, true); } } $errorToken = $stackPtr + 1; if (isset($tokens[$errorToken]) === false) { $errorToken--; } if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) { // We are only interested if this is the first open tag. return; } else { if ($tokens[$commentStart]['code'] === T_COMMENT) { $error = 'You must use "/**" style comments for a file comment'; $phpcsFile->addError($error, $errorToken, 'WrongStyle'); return; } else { if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT) { $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing'); return; } else { // Extract the header comment docblock. $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT, $commentStart + 1, null, true); $commentEnd--; // Check if there is only 1 doc comment between the // open tag and class token. $nextToken = array(T_ABSTRACT, T_CLASS, T_FUNCTION, T_DOC_COMMENT); $commentNext = $phpcsFile->findNext($nextToken, $commentEnd + 1); if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) { // Found a class token right after comment doc block. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, $commentNext, false, $phpcsFile->eolChar); if ($newlineToken !== false) { $newlineToken = $phpcsFile->findNext(T_WHITESPACE, $newlineToken + 1, $commentNext, false, $phpcsFile->eolChar); if ($newlineToken === false) { // No blank line between the class token and the doc block. // The doc block is most likely a class comment. $error = 'Missing file doc comment'; $phpcsFile->addError($error, $errorToken, 'Missing'); return; } } } //end if $comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); // Parse the header comment docblock. try { $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($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 = 'File doc comment is empty'; $phpcsFile->addError($error, $commentStart, 'Empty'); return; } // 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 file comment short description'; $phpcsFile->addError($error, $commentStart + 1, 'SpacingBefore'); } $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 file comment'; $phpcsFile->addError($error, $commentStart + $newlineCount + 1, 'DescriptionSpacing'); } $newlineCount += $newlineBetween; } // Exactly one blank line before tags if short description is present. $tags = $this->commentParser->getTagOrders(); if (count($tags) > 1 && $short !== '' && $newlineSpan > 0) { $newlineSpan = $comment->getNewlineAfter(); if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in file comment'; if ($long !== '') { $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1; } $phpcsFile->addError($error, $commentStart + $newlineCount, 'SpacingBeforeTags'); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } // // Check the PHP Version. // $this->processPHPVersion($commentStart, $commentEnd, $long); // Check each tag. $this->processTags($commentStart, $commentEnd); } } } //end if }