Umherirrender has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/370312 )

Change subject: Added OpeningKeywordBraceSniff
......................................................................

Added OpeningKeywordBraceSniff

The OpeningKeywordBraceSniff checks if isset/unset/list does not have a
space before the open parenthesis.
The sniff is autofixable

Change-Id: I3a6682463f94b6962aa2218ad7ae8c7835e6762c
---
A MediaWiki/Sniffs/WhiteSpace/OpeningKeywordBraceSniff.php
A MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php
A MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.expect
A MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.fixed
4 files changed, 145 insertions(+), 0 deletions(-)


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

diff --git a/MediaWiki/Sniffs/WhiteSpace/OpeningKeywordBraceSniff.php 
b/MediaWiki/Sniffs/WhiteSpace/OpeningKeywordBraceSniff.php
new file mode 100644
index 0000000..253589b
--- /dev/null
+++ b/MediaWiki/Sniffs/WhiteSpace/OpeningKeywordBraceSniff.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Sniffs\WhiteSpace;
+
+use PHP_CodeSniffer\Files\File;
+use PHP_CodeSniffer\Sniffs\Sniff;
+
+class OpeningKeywordBraceSniff implements Sniff {
+       /**
+        * @return array
+        */
+       public function register() {
+               return [
+                       T_EMPTY,
+                       T_EVAL,
+                       T_EXIT,
+                       T_ISSET,
+                       T_LIST,
+                       T_UNSET,
+                       // also check for array(), when not replaced with short 
syntax
+                       T_ARRAY,
+               ];
+       }
+
+       /**
+        * @param File $phpcsFile File object
+        * @param int $stackPtr Index of registered keywords
+        */
+       public function process( File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $next = $stackPtr + 1;
+
+               $openParenthesis = $phpcsFile->findNext( T_WHITESPACE, $next, 
null, true );
+               if ( $openParenthesis === false ||
+                       $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS
+               ) {
+                       // no parenthesis found
+                       return;
+               }
+
+               if ( $next === $openParenthesis ) {
+                       // no whitespaces found
+                       return;
+               }
+
+               $whitespaces = $phpcsFile->getTokensAsString( $next, 
$openParenthesis - $next );
+               $fix = $phpcsFile->addFixableError(
+                       'Expected no space before opening brace; found %s',
+                       $openParenthesis,
+                       'WrongWhitespaceBeforeParenthesis',
+                       strlen( $whitespaces )
+               );
+               if ( $fix ) {
+                       $phpcsFile->fixer->beginChangeset();
+                       for ( $i = $next; $i < $openParenthesis; $i++ ) {
+                               $phpcsFile->fixer->replaceToken( $i, '' );
+                       }
+                       $phpcsFile->fixer->endChangeset();
+               }
+       }
+}
diff --git a/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php 
b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php
new file mode 100644
index 0000000..6abc4ad
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * Failed examples.
+ * @param array $arg An arg
+ * @return array
+ */
+function wfFailedExamples( $arg ) {
+       list  ( $one, $two ) = $arg;
+       if ( isset
+               ( $arg['three'] ) ) {
+               unset   ( $arg['test'] );
+               return $one;
+       }
+       return $two;
+}
+
+/**
+ * Passed examples.
+ * @param array $arg An arg
+ * @return array
+ */
+function wfPassedExamples( array $arg ) {
+       list( $one, $two ) = $arg;
+       if ( isset( $arg['three'] ) ) {
+               unset( $arg['test'] );
+               return $one;
+       }
+       return $two;
+}
diff --git a/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.expect 
b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.expect
new file mode 100644
index 0000000..aff9ba8
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.expect
@@ -0,0 +1,7 @@
+  9 | ERROR | [x] Expected no space before opening brace; found 2
+    |       |     
(MediaWiki.WhiteSpace.OpeningKeywordBrace.WrongWhitespaceBeforeParenthesis)
+ 11 | ERROR | [x] Expected no space before opening brace; found 3
+    |       |     
(MediaWiki.WhiteSpace.OpeningKeywordBrace.WrongWhitespaceBeforeParenthesis)
+ 12 | ERROR | [x] Expected no space before opening brace; found 1
+    |       |     
(MediaWiki.WhiteSpace.OpeningKeywordBrace.WrongWhitespaceBeforeParenthesis)
+PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
diff --git a/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.fixed 
b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.fixed
new file mode 100644
index 0000000..046008f
--- /dev/null
+++ b/MediaWiki/Tests/files/WhiteSpace/opening_keyword_braces.php.fixed
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * Failed examples.
+ * @param array $arg An arg
+ * @return array
+ */
+function wfFailedExamples( $arg ) {
+       list( $one, $two ) = $arg;
+       if ( isset( $arg['three'] ) ) {
+               unset( $arg['test'] );
+               return $one;
+       }
+       return $two;
+}
+
+/**
+ * Passed examples.
+ * @param array $arg An arg
+ * @return array
+ */
+function wfPassedExamples( array $arg ) {
+       list( $one, $two ) = $arg;
+       if ( isset( $arg['three'] ) ) {
+               unset( $arg['test'] );
+               return $one;
+       }
+       return $two;
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3a6682463f94b6962aa2218ad7ae8c7835e6762c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <umherirrender_de...@web.de>

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

Reply via email to