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

Change subject: Make sure no empty line at the begin of the function.
......................................................................


Make sure no empty line at the begin of the function.

After fixing the sing whitespace between open brace
and close parenthesis, unexpect empty line found. To
make the code clean,no empty line at the begin of
function.

Test examples at
Test/WhiteSpace/empty_line_begin_function.php

Change-Id: Ib799f87fb9c57399e5175e3ad3d6f9cdbc51011f
---
M MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php
M MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
A MediaWiki/Sniffs/WhiteSpace/DisallowEmptyLineFunctionsSniff.php
A MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php
A MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.expect
A MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.fixed
6 files changed, 103 insertions(+), 7 deletions(-)

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



diff --git a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php 
b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php
index e408180..ac0fe7d 100644
--- a/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php
+++ b/MediaWiki/Sniffs/Commenting/FunctionCommentSniff.php
@@ -25,7 +25,6 @@
         * @return array
         */
        public function register() {
-
                return [ T_FUNCTION ];
        }
        // end register()
@@ -40,7 +39,6 @@
         * @return void
         */
        public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
-
                $tokens = $phpcsFile->getTokens();
                $find   = PHP_CodeSniffer_Tokens::$methodPrefixes;
                $find[] = T_WHITESPACE;
@@ -100,7 +98,6 @@
         * @return void
         */
        protected function processReturn( PHP_CodeSniffer_File $phpcsFile, 
$stackPtr, $commentStart ) {
-
                $tokens = $phpcsFile->getTokens();
                // Skip constructor and destructor.
                $methodName      = $phpcsFile->getDeclarationName( $stackPtr );
@@ -143,7 +140,6 @@
         * @return void
         */
        protected function processThrows( PHP_CodeSniffer_File $phpcsFile, 
$stackPtr, $commentStart ) {
-
                $tokens = $phpcsFile->getTokens();
                $throws = [];
                foreach ( $tokens[$commentStart]['comment_tags'] as $tag ) {
@@ -179,7 +175,6 @@
         * @return void
         */
        protected function processParams( PHP_CodeSniffer_File $phpcsFile, 
$stackPtr, $commentStart ) {
-
                $tokens = $phpcsFile->getTokens();
                $params  = [];
                $maxType = 0;
diff --git 
a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php 
b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
index 6307ba0..a6ff2c5 100644
--- a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
+++ b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
@@ -36,7 +36,6 @@
         * @return bool Does a namespace statement exist before this position 
in the file?
         */
        private function tokenIsNamespaced( PHP_CodeSniffer_File $phpcsFile, 
$ptr ) {
-
                $fileName = $phpcsFile->getFilename();
                // Check if we already know if the token is namespaced or not 
and return early if possible.
                if (
@@ -80,7 +79,6 @@
         * @return void
         */
        public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
-
                if ( $this->tokenIsNamespaced( $phpcsFile, $stackPtr ) ) {
                        return;
                }
diff --git a/MediaWiki/Sniffs/WhiteSpace/DisallowEmptyLineFunctionsSniff.php 
b/MediaWiki/Sniffs/WhiteSpace/DisallowEmptyLineFunctionsSniff.php
new file mode 100644
index 0000000..9f892ee
--- /dev/null
+++ b/MediaWiki/Sniffs/WhiteSpace/DisallowEmptyLineFunctionsSniff.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Disallow empty line at the begin of function.
+ */
+// @codingStandardsIgnoreStart
+class MediaWiki_Sniffs_WhiteSpace_DisallowEmptyLineFunctionsSniff
+       implements PHP_CodeSniffer_Sniff {
+       // @codingStandardsIgnoreEnd
+
+       /**
+        * @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();
+               $current = $tokens[$stackPtr];
+               if ( isset( $current['scope_opener'] ) === false ||
+                       isset( $current['parenthesis_closer'] ) === false
+               ) {
+                       return;
+               }
+               $openBrace = $current['scope_opener'];
+               $next = $phpcsFile->findNext( T_WHITESPACE, $openBrace + 1, 
null, true );
+               if ( $next === false ) {
+                       return;
+               }
+               if ( $tokens[$next]['line'] > ( $tokens[$openBrace]['line'] + 1 
) ) {
+                       $error = 'Unexpected empty line at the begin of 
function.';
+                       $fix = $phpcsFile->addFixableError(
+                               $error,
+                               $stackPtr,
+                               'NoEmptyLine'
+                       );
+                       if ( $fix === true ) {
+                               $phpcsFile->fixer->beginChangeset();
+                               $i = $openBrace + 1;
+                               while ( $tokens[$i]['line'] !== 
$tokens[$next]['line'] ) {
+                                       $phpcsFile->fixer->replaceToken( $i, '' 
);
+                                       $i++;
+                               }
+                               $phpcsFile->fixer->addNewlineBefore( $i );
+                               $phpcsFile->fixer->endChangeset();
+                       }
+               }
+       }
+}
diff --git a/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php 
b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php
new file mode 100644
index 0000000..085b607
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php
@@ -0,0 +1,18 @@
+<?php
+
+/**
+ * Failed examples.
+ * @return void
+ */
+function wfFailedExamples() {
+
+       # code...
+}
+
+/**
+ * Passed examples.
+ * @return void
+ */
+function wfPassedExamples() {
+       # code...
+}
diff --git 
a/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.expect 
b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.expect
new file mode 100644
index 0000000..a9905ef
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.expect
@@ -0,0 +1,12 @@
+
+FILE: ...r/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php
+----------------------------------------------------------------------
+FOUND 1 ERROR AFFECTING 1 LINE
+----------------------------------------------------------------------
+ 7 | ERROR | [x] Unexpected empty line at the begin of function.
+----------------------------------------------------------------------
+PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
+----------------------------------------------------------------------
+
+Time: 27ms; Memory: 3.5Mb
+
diff --git 
a/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.fixed 
b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.fixed
new file mode 100644
index 0000000..180b952
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/empty_line_begin_function.php.fixed
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * Failed examples.
+ * @return void
+ */
+function wfFailedExamples() {
+       # code...
+}
+
+/**
+ * Passed examples.
+ * @return void
+ */
+function wfPassedExamples() {
+       # code...
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib799f87fb9c57399e5175e3ad3d6f9cdbc51011f
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

Reply via email to