Galorefitz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/224648

Change subject: Sniff to check for space in single line comments
......................................................................

Sniff to check for space in single line comments

The new sniff added checks for a space between comment delimiters and
the comment itself in single line comments. Also checks for empty
single line comments.

Bug: T101872
Change-Id: Ic1d8ba41dd8032c7feccd30f40d1de4f335e2577
---
A MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
1 file changed, 50 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer 
refs/changes/48/224648/1

diff --git a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php 
b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
new file mode 100644
index 0000000..dee2fd0
--- /dev/null
+++ b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
@@ -0,0 +1,50 @@
+<?php
+/**
+* Verify comments are preceeded by a single space.
+*/
+// @codingStandardsIgnoreStart
+class MediaWiki_Sniffs_WhiteSpace_SpaceBeforeSingleLineCommentSniff
+       implements PHP_CodeSniffer_Sniff {
+       // @codingStandardsIgnoreEnd
+       public function register() {
+               return array(
+                       T_COMMENT
+               );
+       }
+
+       public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $currToken = $tokens[$stackPtr];
+               // Checking if the current token is a comment
+               if ( $currToken['code'] === T_COMMENT ) {
+                       // Accounting for multiple line comments, as single 
line comments
+                       // use only '//' and '#'
+                       if ( substr( $currToken['content'], 0, 2 ) === '/*' ) {
+                               return;
+                       }
+                       // Checking whether the comment is an empty one
+                       if ( ( substr( $currToken['content'], 0, 2 ) === '//'
+                                       && ( in_array( substr( 
$currToken['content'], 2 ),
+                                               array( "", " ", "\n", "\t" ) ) 
) )
+                               || ( $currToken['content'][0] === '#'
+                                       && ( in_array( substr( 
$currToken['content'], 1 ),
+                                               array( "", " ", "\n", "\t" ) ) 
) ) ) {
+                               $phpcsFile->addWarning( 'Unnecessary empty 
comment found',
+                                       $stackPtr,
+                                       'EmptyComment'
+                               );
+                       // Checking whether there is a space between the 
comment delimiter
+                       // and the comment
+                       } elseif ( ( substr( $currToken['content'], 0, 2 ) === 
'//'
+                                                       && substr( 
$currToken['content'], 2, 1 ) !== ' ' )
+                                               || ( substr( 
$currToken['content'], 0, 1 ) === '#'
+                                                       && substr( 
$currToken['content'], 1, 1 ) !== ' ' ) ) {
+                               $error = 'Single space expected between "//" or 
"#" and comment';
+                               $phpcsFile->addWarning( $error, $stackPtr,
+                                       'SingleSpaceBeforeSingleLineComment'
+                               );
+                       }
+               }
+
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/224648
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic1d8ba41dd8032c7feccd30f40d1de4f335e2577
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Galorefitz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to