jenkins-bot has submitted this change and it was merged.
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.php
A MediaWiki/Tests/files/NamingConventions/function_name.php.expect
3 files changed, 141 insertions(+), 0 deletions(-)
Approvals:
EBernhardson: Looks good to me, approved
jenkins-bot: Verified
diff --git
a/MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php
b/MediaWiki/Sniffs/NamingConventions/LowerCamelFunctionsNameSniff.php
new file mode 100644
index 0000000..333e0ad
--- /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.php
b/MediaWiki/Tests/files/NamingConventions/function_name.php
new file mode 100644
index 0000000..6228c18
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Just for test.
+ */
+class Test{
+ /**
+ * Lower camel case.
+ * @return void
+ */
+ public function FailedExamples() {
+ $this->say();
+ }
+
+ /**
+ * Lower camel case without under score.
+ * @return void
+ */
+ public function failed_Examples() {
+ }
+
+ /**
+ * 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.php.expect
b/MediaWiki/Tests/files/NamingConventions/function_name.php.expect
new file mode 100644
index 0000000..65696f2
--- /dev/null
+++ b/MediaWiki/Tests/files/NamingConventions/function_name.php.expect
@@ -0,0 +1,13 @@
+
+FILE: ...niffer/MediaWiki/Tests/files/NamingConventions/function_name.php
+----------------------------------------------------------------------
+FOUND 2 ERRORS AFFECTING 2 LINES
+----------------------------------------------------------------------
+ 10 | ERROR | Function name "FailedExamples" should use lower camel
+ | | case.
+ 18 | ERROR | Function name "failed_Examples" should use lower camel
+ | | case.
+----------------------------------------------------------------------
+
+Time: 36ms; Memory: 3.5Mb
+
--
To view, visit https://gerrit.wikimedia.org/r/295115
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2b7916317ce00bf2b390a84c596364996faba69b
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Lethexie <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits