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

Change subject: Add sniff to forbid PHP 7 scalar type hints
......................................................................

Add sniff to forbid PHP 7 scalar type hints

Bug: T171335
Change-Id: I56610e236b4a0caea37c7fcf7e3905b29e5d1859
---
A MediaWiki/Sniffs/Usage/ScalarTypeHintUsageSniff.php
A MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php
A MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.expect
A MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.fixed
4 files changed, 85 insertions(+), 0 deletions(-)


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

diff --git a/MediaWiki/Sniffs/Usage/ScalarTypeHintUsageSniff.php 
b/MediaWiki/Sniffs/Usage/ScalarTypeHintUsageSniff.php
new file mode 100644
index 0000000..2705621
--- /dev/null
+++ b/MediaWiki/Sniffs/Usage/ScalarTypeHintUsageSniff.php
@@ -0,0 +1,61 @@
+<?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\Usage;
+
+use PHP_CodeSniffer\Files\File;
+use PHP_CodeSniffer\Sniffs\Sniff;
+
+class ScalarTypeHintUsageSniff implements Sniff {
+
+       private static $bad = [ 'string', 'int', 'float', 'bool' ];
+
+       /**
+        * Run on functions
+        *
+        * @return array
+        */
+       public function register() {
+               return [
+                       T_FUNCTION
+               ];
+       }
+
+       /**
+        * If the type-hint is bad, raise an error
+        *
+        * @param File $phpcsFile File
+        * @param int $stackPtr position
+        */
+       public function process( File $phpcsFile, $stackPtr ) {
+               $params = $phpcsFile->getMethodParameters( $stackPtr );
+               foreach ( $params as $param ) {
+                       if ( $param['type_hint'] !== ''
+                               && in_array( $param['type_hint'], self::$bad )
+                       ) {
+                               $phpcsFile->addError(
+                                       "Scalar type hint of 
'{$param['type_hint']}' cannot be used",
+                                       $param['token'],
+                                       'Found'
+                               );
+                       }
+               }
+       }
+}
diff --git a/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php 
b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php
new file mode 100644
index 0000000..05087a3
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * Lots of stuff
+ *
+ * @param int $a Stuff
+ * @param mixed $b More stuff
+ */
+function wfFoo( int $a, $b ) {
+       $b = $a;
+}
diff --git a/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.expect 
b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.expect
new file mode 100644
index 0000000..f076050
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.expect
@@ -0,0 +1,2 @@
+ 9 | ERROR | Scalar type hint of 'int' cannot be used
+   |       | (MediaWiki.Usage.ScalarTypeHintUsage.Found)
diff --git a/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.fixed 
b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.fixed
new file mode 100644
index 0000000..05087a3
--- /dev/null
+++ b/MediaWiki/Tests/files/Usage/scalar_type_hint_usage.php.fixed
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * Lots of stuff
+ *
+ * @param int $a Stuff
+ * @param mixed $b More stuff
+ */
+function wfFoo( int $a, $b ) {
+       $b = $a;
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I56610e236b4a0caea37c7fcf7e3905b29e5d1859
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>

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

Reply via email to