/** * 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->currentFile = $phpcsFile; $tokens = $phpcsFile->getTokens(); $commentToken = array(T_COMMENT, T_DOC_COMMENT); // Extract the var comment docblock. $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr - 3); if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) { $phpcsFile->addError('You must use "/**" style comments for a variable comment', $stackPtr, 'WrongStyle'); return; } else { if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) { $phpcsFile->addError('Missing variable doc comment', $stackPtr, 'Missing'); return; } else { // Make sure the comment we have found belongs to us. $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), $commentEnd + 1); if ($commentFor !== $stackPtr) { $phpcsFile->addError('Missing variable doc comment', $stackPtr, 'Missing'); 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; $phpcsFile->addError($e->getMessage(), $line, 'ErrorParsing'); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'Variable 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'); } // Check for a comment description. $short = $comment->getShortComment(); $long = ''; if (trim($short) === '') { $error = 'Missing short description in variable doc comment'; $phpcsFile->addError($error, $commentStart, 'MissingShort'); $newlineCount = 1; } else { // No extra newline before short description. $newlineCount = 0; $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $error = 'Extra newline(s) found before variable 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 variable comment'; $phpcsFile->addError($error, $commentStart + $newlineCount + 1, 'SpacingBetween'); } $newlineCount += $newlineBetween; $testLong = trim($long); if (preg_match('|[A-Z]|', $testLong[0]) === 0) { $error = 'Variable comment long description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + $newlineCount, 'LongNotCapital'); } } //end if // 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 = 'Variable comment short description must be on a single line'; $phpcsFile->addError($error, $commentStart + 1, 'ShortSingleLine'); } if (preg_match('|[A-Z]|', $testShort[0]) === 0) { $error = 'Variable comment short description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + 1, 'ShortNotCapital'); } if ($lastChar !== '.') { $error = 'Variable comment short description must end with a full stop'; $phpcsFile->addError($error, $commentStart + 1, 'ShortFullStop'); } } //end if // Exactly one blank line before tags. $tags = $this->commentParser->getTagOrders(); if (count($tags) > 1) { $newlineSpan = $comment->getNewlineAfter(); if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in variable comment'; if ($long !== '') { $newlineCount += substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1; } $phpcsFile->addError($error, $commentStart + $newlineCount, 'SpacingBeforeTags'); $short = rtrim($short, $phpcsFile->eolChar . ' '); } } // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { // Unknown tags are not parsed, do not process further. $error = '@%s tag is not allowed in variable comment'; $data = array($errorTag['tag']); $phpcsFile->addWarning($error, $commentStart + $errorTag['line'], 'TagNotAllowed', $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]) === '') { $error = 'Additional blank lines found at end of variable 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 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(); $errorToken = $stackPtr + 1; if (isset($tokens[$errorToken]) === false) { $errorToken--; } // Find the next non whitespace token. $commentStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); 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) { $phpcsFile->addError('You must use "/**" style comments for a file comment', $errorToken, 'WrongStyle'); return; } else { if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT) { $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing'); return; } } } // Extract the header comment docblock. $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT, $commentStart + 1, null, true) - 1; // Check if there is only 1 doc comment between the open tag and class token. $nextToken = array(T_ABSTRACT, T_CLASS, 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. $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing'); return; } } } // No blank line between the open tag and the file comment. $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, false, $phpcsFile->eolChar); if ($blankLineBefore !== false && $blankLineBefore < $commentStart) { $error = 'Extra newline found after the open tag'; $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); } // Exactly one blank line after the file comment. $nextTokenStart = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); if ($nextTokenStart !== false) { $blankLineAfter = 0; for ($i = $commentEnd + 1; $i < $nextTokenStart; $i++) { if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) { $blankLineAfter++; } } if ($blankLineAfter !== 2) { $error = 'There must be exactly one blank line after the file comment'; $phpcsFile->addError($error, $commentEnd + 1, 'SpacingAfterComment'); } } $commentString = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); // Parse the header comment docblock. try { $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($commentString, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = $e->getLineWithinComment() + $commentStart; $phpcsFile->addError($e->getMessage(), $line, 'Exception'); return; } $comment = $this->commentParser->getComment(); if (is_null($comment) === true) { $error = 'File 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'); } // 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, '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 file comment'; $phpcsFile->addError($error, $commentStart + $newlineCount + 1, 'SpacingBetween'); } $newlineCount += $newlineBetween; $testLong = trim($long); if (preg_match('|[A-Z]|', $testLong[0]) === 0) { $error = 'File comment long description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + $newlineCount, 'LongNotCapital'); } } //end if // Exactly one blank line before tags. $tags = $this->commentParser->getTagOrders(); if (count($tags) > 1) { $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 . ' '); } } // Short description must be single line and end with a full stop. $testShort = trim($short); if ($testShort === '') { $error = 'Missing short description in file comment'; $phpcsFile->addError($error, $commentStart + 1, 'MissingShort'); } else { $lastChar = $testShort[strlen($testShort) - 1]; if (substr_count($testShort, $phpcsFile->eolChar) !== 0) { $error = 'File comment short description must be on a single line'; $phpcsFile->addError($error, $commentStart + 1, 'ShortSingleLine'); } if (preg_match('|[A-Z]|', $testShort[0]) === 0) { $error = 'File comment short description must start with a capital letter'; $phpcsFile->addError($error, $commentStart + 1, 'ShortNotCapital'); } if ($lastChar !== '.') { $error = 'File comment short description must end with a full stop'; $phpcsFile->addError($error, $commentStart + 1, 'ShortFullStop'); } } //end if // Check for unknown/deprecated tags. $unknownTags = $this->commentParser->getUnknown(); foreach ($unknownTags as $errorTag) { // Unknown tags are not parsed, do not process further. $error = '@%s tag is not allowed in file comment'; $data = array($errorTag['tag']); $phpcsFile->addWarning($error, $commentStart + $errorTag['line'], 'TagNotAllowed', $data); } // Check each tag. $this->processTags($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 file comment'; $this->currentFile->addError($error, $commentEnd, 'SpacingAfter'); } }