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

Change subject: Introduce sniff to detect unprefixed global functions
......................................................................


Introduce sniff to detect unprefixed global functions

MediaWiki's global function naming convention requires prefixing with
'wf' for top level functions and 'ef' for global functions in extensions
(not a very common occurence, though).

Bug: T92749
Change-Id: I271dbef2ffe8ec6c30157c3b72542269b4db84dd
---
A MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
M 
MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_fail.php
M 
MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_pass.php
A MediaWiki/Tests/files/NamingConventions/wf_global_function_fail.php
A MediaWiki/Tests/files/NamingConventions/wf_global_function_pass.php
M 
MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_heredoc_pass.php
M MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
M 
MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
8 files changed, 48 insertions(+), 5 deletions(-)

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



diff --git 
a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php 
b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
new file mode 100644
index 0000000..0e12a8c
--- /dev/null
+++ b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Verify MediaWiki global function naming convention.
+ * A global function's name must be prefixed with 'wf' or 'ef'.
+ * Per https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Naming
+ */
+class MediaWiki_Sniffs_NamingConventions_PrefixedGlobalFunctionsSniff 
implements PHP_CodeSniffer_Sniff {
+       public function register() {
+               return array( T_FUNCTION );
+       }
+
+       public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $token = $tokens[$stackPtr];
+
+               //Name of function
+               $name = $tokens[$stackPtr + 2]['content'];
+
+               //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
+                               $expected = 'wf' . ucfirst( $name );
+
+                               $error = 'Global function "%s" is lacking a 
\'wf\' prefix. Should be "%s".';
+                               $data = array( $name, $expected );
+                               $phpcsFile->addError( $error, $stackPtr, 
'wfPrefix', $data );
+                       }
+               }
+       }
+}
diff --git 
a/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_fail.php
 
b/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_fail.php
index c3f3f18..5ada4d8 100644
--- 
a/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_fail.php
+++ 
b/MediaWiki/Tests/files/ExtraCharacters/extra_characters_before_phpopen_tag_fail.php
@@ -1,6 +1,6 @@
 someText<?php
 // text before first php open
-function foo() {
+function wfFoo() {
 
 }
 ?>
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 ba88365..9e9588a 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
@@ -1,6 +1,6 @@
 <?php
 // Valid php open tag
-function foo() {
+function wfFoo() {
 
 }
 ?>
diff --git 
a/MediaWiki/Tests/files/NamingConventions/wf_global_function_fail.php 
b/MediaWiki/Tests/files/NamingConventions/wf_global_function_fail.php
new file mode 100644
index 0000000..0514cf7
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/wf_global_function_fail.php
@@ -0,0 +1,5 @@
+<?php
+
+function foo() {
+
+}
diff --git 
a/MediaWiki/Tests/files/NamingConventions/wf_global_function_pass.php 
b/MediaWiki/Tests/files/NamingConventions/wf_global_function_pass.php
new file mode 100644
index 0000000..c8b93eb
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/wf_global_function_pass.php
@@ -0,0 +1,5 @@
+<?php
+
+function wfFoo() {
+
+}
diff --git 
a/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_heredoc_pass.php
 
b/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_heredoc_pass.php
index 213d01f..b91e935 100644
--- 
a/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_heredoc_pass.php
+++ 
b/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_heredoc_pass.php
@@ -1,6 +1,6 @@
 <?php
 
-function fooFoo () {
+function wfFooFoo () {
        global $wgSomething;
        $foo = <<<PHP
 /**
diff --git 
a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php 
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
index aa16ff6..480abda 100644
--- a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
+++ b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
@@ -1,6 +1,6 @@
 <?php
 
-function fooFoo() {
+function wfFooFoo() {
        global $wgSomething;
        $foo = $wgSomething + 2;
 }
diff --git 
a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
 
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
index 33ecdc7..27018a3 100644
--- 
a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
+++ 
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
@@ -1,6 +1,6 @@
 <?php
 
-function fooFoo() {
+function wfFooFoo() {
        global $wgSomething;
        $foo = "foo$wgSomething";
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I271dbef2ffe8ec6c30157c3b72542269b4db84dd
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Polybuildr <[email protected]>
Gerrit-Reviewer: Addshore <[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