/** * 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']); } }