jenkins-bot has submitted this change and it was merged.

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.

Also added tests for the sniff, and corrected two files that had errors
as reported by the sniff.

Bug: T101872
Change-Id: Ic1d8ba41dd8032c7feccd30f40d1de4f335e2577
---
M MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
A MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
M 
MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
A MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
A MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_pass.php
5 files changed, 73 insertions(+), 4 deletions(-)

Approvals:
  Polybuildr: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php 
b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
index de514e4..ff95a33 100644
--- a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
+++ b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
@@ -25,15 +25,15 @@
                }
                $token = $tokens[$stackPtr];
 
-               //Name of function
+               // Name of function
                $name = $tokens[$stackPtr + 2]['content'];
 
-               //Check if function is global
+               // Check if function is global
                if ( $token['level'] == 0 ) {
                        $prefix = substr( $name, 0, 2 );
 
                        if ( $prefix !== 'wf' && $prefix !== 'ef' ) {
-                               //Forge a valid global function name
+                               // Forge a valid global function name
                                $expected = 'wf' . ucfirst( $name );
 
                                $error = 'Global function "%s" is lacking a 
\'wf\' prefix. Should be "%s".';
diff --git a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php 
b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
new file mode 100644
index 0000000..e98865b
--- /dev/null
+++ b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
@@ -0,0 +1,57 @@
+<?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];
+               if ( $currToken['code'] === T_COMMENT ) {
+                       // Accounting for multiple line comments, as single 
line comments
+                       // use only '//' and '#'
+                       // Also ignoring phpdoc comments starting with '///',
+                       // as there are no coding standards documented for these
+                       if ( substr( $currToken['content'], 0, 2 ) === '/*'
+                               || substr( $currToken['content'], 0, 3 ) === 
'///'
+                       ) {
+                               return;
+                       }
+                       // Checking whether the comment is an empty one
+                       if ( ( substr( $currToken['content'], 0, 2 ) === '//'
+                                       && rtrim( $currToken['content'] ) === 
'//' )
+                               || ( $currToken['content'][0] === '#'
+                                       && rtrim( $currToken['content'] ) === 
'#' )
+                       ) {
+                               $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 ) !== ' '
+                       ) {
+                               $error = 'Single space expected between "//" 
and comment';
+                               $phpcsFile->addWarning( $error, $stackPtr,
+                                       'SingleSpaceBeforeSingleLineComment'
+                               );
+                       } elseif ( substr( $currToken['content'], 0, 1 ) === '#'
+                               && substr( $currToken['content'], 1, 1 ) !== ' '
+                       ) {
+                               $error = 'Single space expected between "#" and 
comment';
+                               $phpcsFile->addWarning( $error, $stackPtr,
+                                       'SingleSpaceBeforeSingleLineComment'
+                               );
+                       }
+               }
+       }
+}
diff --git 
a/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
 
b/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
index 9e9588a..22a2a8c 100644
--- 
a/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
+++ 
b/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
@@ -4,4 +4,4 @@
 
 }
 ?>
- <?php //this php open tag will in any case be ignored
+ <?php // this php open tag will in any case be ignored
diff --git 
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
 
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
new file mode 100644
index 0000000..c5016dc
--- /dev/null
+++ 
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
@@ -0,0 +1,8 @@
+<?php
+//             a comment with tabs instead of a space
+//                     
+#              yet another comment with tabs instead of a space.
+#                                              
+//A comment without a space
+#Yup, no spaces.
+
diff --git 
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_pass.php
 
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_pass.php
new file mode 100644
index 0000000..3d4e9b8
--- /dev/null
+++ 
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_pass.php
@@ -0,0 +1,4 @@
+<?php
+// a valid comment.
+# another valid comment.
+/* a comment no warnings would be raised on */

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic1d8ba41dd8032c7feccd30f40d1de4f335e2577
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Galorefitz <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Galorefitz <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Polybuildr <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to