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

Change subject: Add sniff to prevent against using PHP 7's Unicode escape syntax
......................................................................

Add sniff to prevent against using PHP 7's Unicode escape syntax

Bug: T168669
Change-Id: I2c41e31d7749cd98f86089eb0af0d45354ac5091
---
A MediaWiki/Sniffs/AlternativeSyntax/PHP7UnicodeSyntaxSniff.php
A MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php
A MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.expect
A MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.fixed
4 files changed, 103 insertions(+), 0 deletions(-)


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

diff --git a/MediaWiki/Sniffs/AlternativeSyntax/PHP7UnicodeSyntaxSniff.php 
b/MediaWiki/Sniffs/AlternativeSyntax/PHP7UnicodeSyntaxSniff.php
new file mode 100644
index 0000000..ed38020
--- /dev/null
+++ b/MediaWiki/Sniffs/AlternativeSyntax/PHP7UnicodeSyntaxSniff.php
@@ -0,0 +1,65 @@
+<?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\AlternativeSyntax;
+
+use PHP_CodeSniffer\Files\File;
+use PHP_CodeSniffer\Sniffs\Sniff;
+
+/**
+ * Prevent usage of the new PHP 7 unicode escape syntax
+ *
+ * @see https://wiki.php.net/rfc/unicode_escape
+ */
+class PHP7UnicodeSyntaxSniff implements Sniff {
+       /**
+        * @return array
+        */
+       public function register() {
+               return [
+                       T_CONSTANT_ENCAPSED_STRING,
+                       T_DOUBLE_QUOTED_STRING,
+                       T_HEREDOC
+               ];
+       }
+
+       /**
+        * @param File $phpcsFile File object.
+        * @param int $stackPtr The current token index.
+        * @return void
+        */
+       public function process( File $phpcsFile, $stackPtr ) {
+               $tokens = $phpcsFile->getTokens();
+               $info = $tokens[$stackPtr];
+               if ( $info['code'] == T_CONSTANT_ENCAPSED_STRING && 
$info['content'][0] === "'" ) {
+                       // Single quoted string
+                       return;
+               }
+
+               $matched = preg_match( '/\\\u\{[0-9A-Fa-f]*\}/', 
$info['content'] );
+               if ( $matched ) {
+                       $phpcsFile->addError(
+                               'PHP 7 Unicode escape syntax not allowed',
+                               $stackPtr,
+                               'NotAllowed'
+                       );
+               }
+       }
+}
diff --git a/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php 
b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php
new file mode 100644
index 0000000..05d9549
--- /dev/null
+++ b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php
@@ -0,0 +1,13 @@
+<?php
+
+echo "\u{aa}";
+echo "\u{0000Aa}";
+echo "\u{9999}";
+echo "ma\u{00F1}ana";
+echo "man\u{0303}$ana";
+echo '\u{9999}';
+echo '';
+echo <<<HTML
+
+<small>\u{0303}</small>
+HTML;
diff --git 
a/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.expect 
b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.expect
new file mode 100644
index 0000000..005e292
--- /dev/null
+++ b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.expect
@@ -0,0 +1,12 @@
+  3 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
+  4 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
+  5 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
+  6 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
+  7 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
+ 12 | ERROR | PHP 7 Unicode escape syntax not allowed
+    |       | (MediaWiki.AlternativeSyntax.PHP7UnicodeSyntax.NotAllowed)
diff --git 
a/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.fixed 
b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.fixed
new file mode 100644
index 0000000..05d9549
--- /dev/null
+++ b/MediaWiki/Tests/files/AlternativeSyntax/php7unicodesyntax.php.fixed
@@ -0,0 +1,13 @@
+<?php
+
+echo "\u{aa}";
+echo "\u{0000Aa}";
+echo "\u{9999}";
+echo "ma\u{00F1}ana";
+echo "man\u{0303}$ana";
+echo '\u{9999}';
+echo '';
+echo <<<HTML
+
+<small>\u{0303}</small>
+HTML;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2c41e31d7749cd98f86089eb0af0d45354ac5091
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to