[MediaWiki-commits] [Gerrit] mediawiki...codesniffer[master]: Validate doc syntax
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/383004 ) Change subject: Validate doc syntax .. Validate doc syntax Check for /**, *, */, aligned * and spacing around all of them Added the following checks: - SyntaxOpenTag: Comment open tag must be '/**' - SyntaxDocStar: Comment star must be '*' - SyntaxMultiDocStar: Comment star must be a single '*' - SpacingDocStar: Expected at least %s spaces after doc star; %s found - SpacingDocStarSingleLine: Expected %s spaces after doc star on single line; %s found - SpacingDocTag: Expected %s spaces before %s; %s found - SyntaxAlignedDocStar: Comment star tag not aligned with open tag - SyntaxAlignedDocClose: Comment close tag not aligned with open tag - SyntaxCloseTag: Comment close tag must be '*/' - CloseTagOwnLine: Comment close tag should have own line - SpacingSingleLineCloseTag: Expected %s spaces before close comment tag on single line; %s found Change-Id: I8fe953c1f83b75c9b4f024173c46bc02e6bcc8d4 --- M MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php M MediaWiki/Tests/files/Commenting/commenting_function.php M MediaWiki/Tests/files/Commenting/commenting_function.php.expect M MediaWiki/Tests/files/Commenting/commenting_function.php.fixed 4 files changed, 409 insertions(+), 3 deletions(-) Approvals: Legoktm: Looks good to me, approved jenkins-bot: Verified diff --git a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php index 8bd2db2..0debd9e 100644 --- a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php +++ b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php @@ -159,6 +159,9 @@ $skipDoc = true; } } + + $this->validateDocSyntax( $phpcsFile, $stackPtr, $commentStart, $commentEnd ); + if ( $skipDoc ) { // Don't need to validate anything else return; @@ -665,5 +668,251 @@ $content .= $fixParam['comment_first']; $phpcsFile->fixer->replaceToken( ( $fixParam['tag'] + 2 ), $content ); } + + /** +* Check the doc syntax like start or end tags +* +* @param File $phpcsFile The file being scanned. +* @param int $stackPtr The position of the current token in the stack passed in $tokens. +* @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 validateDocSyntax( File $phpcsFile, $stackPtr, $commentStart, $commentEnd ) { + $tokens = $phpcsFile->getTokens(); + $isMultiLineDoc = ( $tokens[$commentStart]['line'] !== $tokens[$commentEnd]['line'] ); + + // Start token should exact /** + if ( $tokens[$commentStart]['code'] === T_DOC_COMMENT_OPEN_TAG && + $tokens[$commentStart]['content'] !== '/**' + ) { + $error = 'Comment open tag must be \'/**\''; + $fix = $phpcsFile->addFixableError( $error, $commentStart, 'SyntaxOpenTag' ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $commentStart, '/**' ); + } + } + // Calculate the column to align all doc stars. Use column of /**, add 1 to skip char / + $columnDocStar = $tokens[$commentStart]['column'] + 1; + $prevLineDocStar = $tokens[$commentStart]['line']; + + for ( $i = $commentStart; $i <= $commentEnd; $i++ ) { + $initialStarChars = 0; + + // Star token should exact * + if ( $tokens[$i]['code'] === T_DOC_COMMENT_STAR ) { + if ( $tokens[$i]['content'] !== '*' ) { + $error = 'Comment star must be \'*\''; + $fix = $phpcsFile->addFixableError( $error, $i, 'SyntaxDocStar' ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $i, '*' ); + } + } + // Multi stars in a line are parsed as a new token + $initialStarChars = strspn( $tokens[$i + 1]['content'], '*' ); + if ( $initialStarChars > 0 ) { + $error = 'Comment star must be a single \'*\''; + $fix = $phpcsFile->addFixableError( $error, $i, 'SyntaxMultiDocStar' ); +
[MediaWiki-commits] [Gerrit] mediawiki...codesniffer[master]: Validate doc syntax
Umherirrender has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/383004 ) Change subject: Validate doc syntax .. Validate doc syntax Check for /**, *, */, aligned * and spacing around all of them Added the following checks: - SyntaxOpenTag: Comment open tag must be '/**' - SyntaxDocStar: Comment star must be '*' - SyntaxMultiDocStar: Comment star must be a single '*' - SpacingDocStar: Expected at least %s spaces after doc star; %s found - SpacingDocStarSingleLine: Expected %s spaces after doc star on single line; %s found - SpacingDocTag: Expected %s spaces before %s; %s found - SyntaxAlignedDocStar: Comment star tag not aligned with open tag - SyntaxAlignedDocClose: Comment close tag not aligned with open tag - SyntaxCloseTag: Comment close tag must be '*/' - CloseTagOwnLine: Comment close tag should have own line - SpacingSingleLineCloseTag: Expected %s spaces before close comment tag on single line; %s found Change-Id: I8fe953c1f83b75c9b4f024173c46bc02e6bcc8d4 --- M MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php M MediaWiki/Tests/files/Commenting/commenting_function.php M MediaWiki/Tests/files/Commenting/commenting_function.php.expect M MediaWiki/Tests/files/Commenting/commenting_function.php.fixed 4 files changed, 409 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/04/383004/1 diff --git a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php index 8bd2db2..0debd9e 100644 --- a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php +++ b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php @@ -159,6 +159,9 @@ $skipDoc = true; } } + + $this->validateDocSyntax( $phpcsFile, $stackPtr, $commentStart, $commentEnd ); + if ( $skipDoc ) { // Don't need to validate anything else return; @@ -665,5 +668,251 @@ $content .= $fixParam['comment_first']; $phpcsFile->fixer->replaceToken( ( $fixParam['tag'] + 2 ), $content ); } + + /** +* Check the doc syntax like start or end tags +* +* @param File $phpcsFile The file being scanned. +* @param int $stackPtr The position of the current token in the stack passed in $tokens. +* @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 validateDocSyntax( File $phpcsFile, $stackPtr, $commentStart, $commentEnd ) { + $tokens = $phpcsFile->getTokens(); + $isMultiLineDoc = ( $tokens[$commentStart]['line'] !== $tokens[$commentEnd]['line'] ); + + // Start token should exact /** + if ( $tokens[$commentStart]['code'] === T_DOC_COMMENT_OPEN_TAG && + $tokens[$commentStart]['content'] !== '/**' + ) { + $error = 'Comment open tag must be \'/**\''; + $fix = $phpcsFile->addFixableError( $error, $commentStart, 'SyntaxOpenTag' ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $commentStart, '/**' ); + } + } + // Calculate the column to align all doc stars. Use column of /**, add 1 to skip char / + $columnDocStar = $tokens[$commentStart]['column'] + 1; + $prevLineDocStar = $tokens[$commentStart]['line']; + + for ( $i = $commentStart; $i <= $commentEnd; $i++ ) { + $initialStarChars = 0; + + // Star token should exact * + if ( $tokens[$i]['code'] === T_DOC_COMMENT_STAR ) { + if ( $tokens[$i]['content'] !== '*' ) { + $error = 'Comment star must be \'*\''; + $fix = $phpcsFile->addFixableError( $error, $i, 'SyntaxDocStar' ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $i, '*' ); + } + } + // Multi stars in a line are parsed as a new token + $initialStarChars = strspn( $tokens[$i + 1]['content'], '*' ); + if ( $initialStarChars > 0 ) { + $error = 'Comment star must be a single \'*\''; + $fix = $phpcsFile->addFixableError( $error, $i, 'SyntaxMultiDocStar' ); +