Lethexie has uploaded a new change for review.

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

Change subject: Add sniff to confirm function name using lower camel case.
......................................................................

Add sniff to confirm function name using lower camel case.

The original version doesn't check function name as the conventions
page mentioned.

Now it can detect whether functions name use lower camel.

Change-Id: I2b7916317ce00bf2b390a84c596364996faba69b
---
A MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php
A MediaWiki/Tests/files/NamingConventions/function_name_fail.php
A MediaWiki/Tests/files/NamingConventions/function_name_fail.php.expect
A MediaWiki/Tests/files/NamingConventions/function_name_pass.php
A MediaWiki/Tests/files/NamingConventions/function_name_pass.php.expect
5 files changed, 145 insertions(+), 0 deletions(-)


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

diff --git 
a/MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php 
b/MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php
new file mode 100644
index 0000000..f85ecb8
--- /dev/null
+++ b/MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * Make sure lower camel function name.
+ */
+// @codingStandardsIgnoreStart
+class MediaWiki_Sniffs_NamingConventions_LowerCamelFunctionsNameSniff
+       implements PHP_CodeSniffer_Sniff {
+       // @codingStandardsIgnoreEnd
+
+       // Magic methods.
+       private static $magicMethods = [
+                       '__construct' => true,
+                       '__destruct' => true,
+                       '__call' => true,
+                       '__callstatic' => true,
+                       '__get' => true,
+                       '__set' => true,
+                       '__isset' => true,
+                       '__unset' => true,
+                       '__sleep' => true,
+                       '__wakeup' => true,
+                       '__tostring' => true,
+                       '__set_state' => true,
+                       '__clone' => true,
+                       '__invoke' => true,
+                       '__debuginfo' => true
+               ];
+
+       // A list of non-magic methods with double underscore.
+       private static $methodsDoubleUnderscore = [
+                       '__soapcall' => true,
+                       '__getlastrequest' => true,
+                       '__getlastresponse' => true,
+                       '__getlastrequestheaders' => true,
+                       '__getlastresponseheaders' => true,
+                       '__getfunctions' => true,
+                       '__gettypes' => true,
+                       '__dorequest' => true,
+                       '__setcookie' => true,
+                       '__setlocation' => true,
+                       '__setsoapheaders' => true,
+               ];
+
+       // Scope list.
+       private static $scopeList = [
+                       T_CLASS => true,
+                       T_INTERFACE => true,
+                       T_TRAIT => true
+               ];
+
+       /**
+        * @return array
+        */
+       public function register() {
+               return [ T_FUNCTION ];
+       }
+
+       /**
+        * @param  PHP_CodeSniffer_File $phpcsFile PHP_CodeSniffer_File object.
+        * @param  int $stackPtr The current token index.
+        * @return void
+        */
+       public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $functionContent = $tokens[$stackPtr+2]['content'];
+               $lowerFunctionName = strtolower( $functionContent );
+               foreach ( $tokens[$stackPtr]['conditions'] as $scope => $code ) 
{
+                       if ( isset( self::$scopeList[$code] ) === true &&
+                               isset( 
self::$methodsDoubleUnderscore[$lowerFunctionName] ) !== true &&
+                               isset( self::$magicMethods[$lowerFunctionName] 
) !== true
+                       ) {
+                               $pos = strpos( $functionContent, '_' );
+                               if ( $pos !== false ||
+                                       $functionContent[0] !== 
$lowerFunctionName[0]
+                               ) {
+                                       $error = 'Function name "%s" should use 
lower camel case.';
+                                       $fix = $phpcsFile->addError(
+                                                       $error,
+                                                       $stackPtr,
+                                                       'FunctionName',
+                                                       [ $functionContent ]
+                                       );
+                               }
+                       }
+               }
+       }
+}
diff --git a/MediaWiki/Tests/files/NamingConventions/function_name_fail.php 
b/MediaWiki/Tests/files/NamingConventions/function_name_fail.php
new file mode 100644
index 0000000..0be9bb3
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name_fail.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * Just for test.
+ */
+class Test{
+       /**
+        * Lower camel case.
+        * @return void
+        */
+       public function MyTest() {
+               $this->say();
+       }
+
+       /**
+        * Lower camel case without under score.
+        * @return void
+        */
+       public function say_Test() {
+       }
+}
diff --git 
a/MediaWiki/Tests/files/NamingConventions/function_name_fail.php.expect 
b/MediaWiki/Tests/files/NamingConventions/function_name_fail.php.expect
new file mode 100644
index 0000000..8e9596e
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name_fail.php.expect
@@ -0,0 +1,11 @@
+
+FILE: ...r/MediaWiki/Tests/files/NamingConventions/function_name_fail.php
+----------------------------------------------------------------------
+FOUND 2 ERRORS AFFECTING 2 LINES
+----------------------------------------------------------------------
+ 10 | ERROR | Function name "MyTest" should use lower camel case.
+ 18 | ERROR | Function name "say_Test" should use lower camel case.
+----------------------------------------------------------------------
+
+Time: 19ms; Memory: 3.5Mb
+
diff --git a/MediaWiki/Tests/files/NamingConventions/function_name_pass.php 
b/MediaWiki/Tests/files/NamingConventions/function_name_pass.php
new file mode 100644
index 0000000..859b91e
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name_pass.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * Just for test.
+ */
+class Test {
+
+       /**
+        * magic method with double under score.
+        */
+       public function __construct() {
+       }
+       /**
+        * Lower camel case.
+        * @return void
+        */
+       public function test() {
+               $this->say();
+       }
+
+       /**
+        * Lower camel case without under score.
+        * @return void
+        */
+       public function sayTest() {
+       }
+
+}
diff --git 
a/MediaWiki/Tests/files/NamingConventions/function_name_pass.php.expect 
b/MediaWiki/Tests/files/NamingConventions/function_name_pass.php.expect
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name_pass.php.expect

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2b7916317ce00bf2b390a84c596364996faba69b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Lethexie <xietaoe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to