Use PHP 7 "\u{NNNN}" Unicode codepoint escapes in string literals (part 2)
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 8 Jan 2018 22:18:00 +0000 (23:18 +0100)
committerJforrester <jforrester@wikimedia.org>
Mon, 4 Jun 2018 16:40:48 +0000 (16:40 +0000)
This is a follow-up to Idc3dee3a7fb5ebfaef395754d8859b18f1f8769a
containing some less trivial changes.

Change-Id: Ia7af2c1d000307d43278cde4a246df413d4ef263

includes/installer/Installer.php
includes/preferences/DefaultPreferencesFactory.php
tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php

index 00bd4d0..f5fd3f2 100644 (file)
@@ -1107,29 +1107,6 @@ abstract class Installer {
                return true;
        }
 
-       /**
-        * Convert a hex string representing a Unicode code point to that code point.
-        * @param string $c
-        * @return string|false
-        */
-       protected function unicodeChar( $c ) {
-               $c = hexdec( $c );
-               if ( $c <= 0x7F ) {
-                       return chr( $c );
-               } elseif ( $c <= 0x7FF ) {
-                       return chr( 0xC0 | $c >> 6 ) . chr( 0x80 | $c & 0x3F );
-               } elseif ( $c <= 0xFFFF ) {
-                       return chr( 0xE0 | $c >> 12 ) . chr( 0x80 | $c >> 6 & 0x3F ) .
-                               chr( 0x80 | $c & 0x3F );
-               } elseif ( $c <= 0x10FFFF ) {
-                       return chr( 0xF0 | $c >> 18 ) . chr( 0x80 | $c >> 12 & 0x3F ) .
-                               chr( 0x80 | $c >> 6 & 0x3F ) .
-                               chr( 0x80 | $c & 0x3F );
-               } else {
-                       return false;
-               }
-       }
-
        /**
         * Check the libicu version
         */
@@ -1141,8 +1118,8 @@ abstract class Installer {
                 * Note that we use the hex representation to create the code
                 * points in order to avoid any Unicode-destroying during transit.
                 */
-               $not_normal_c = $this->unicodeChar( "FA6C" );
-               $normal_c = $this->unicodeChar( "242EE" );
+               $not_normal_c = "\u{FA6C}";
+               $normal_c = "\u{242EE}";
 
                $useNormalizer = 'php';
                $needsUpdate = false;
index 0a9e9c8..2b497f6 100644 (file)
@@ -1409,8 +1409,8 @@ class DefaultPreferencesFactory implements PreferencesFactory {
                $pixels = $l10n->msg( 'unit-pixel' )->text();
 
                foreach ( $this->config->get( 'ImageLimits' ) as $index => $limits ) {
-                       // Note: A left-to-right marker (\u200e) is inserted, see T144386
-                       $display = "{$limits[0]}" . json_decode( '"\u200e"' ) . "×{$limits[1]}" . $pixels;
+                       // Note: A left-to-right marker (U+200E) is inserted, see T144386
+                       $display = "{$limits[0]}\u{200E}×{$limits[1]}$pixels";
                        $ret[$display] = $index;
                }
 
index 93192d0..a86a1c9 100644 (file)
@@ -79,12 +79,12 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
 
                        // unicode chars
                        [
-                               self::createUnicodeString( '`\u0001a\uFFFFb`' ),
-                               self::createUnicodeString( '\u0001a\uFFFFb' )
+                               "`\u{0001}a\u{FFFF}b`",
+                               "\u{0001}a\u{FFFF}b"
                        ],
                        [
-                               self::createUnicodeString( '`\u0001\uFFFF`' ),
-                               self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
+                               "`\u{0001}\u{FFFF}`",
+                               "\u{0001}\u{0000}\u{FFFF}\u{0000}"
                        ],
                        [ '`☃`', '☃' ],
                        [ '`メインページ`', 'メインページ' ],
@@ -97,10 +97,6 @@ class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
                ];
        }
 
-       private static function createUnicodeString( $str ) {
-               return json_decode( '"' . $str . '"' );
-       }
-
        private function getMockForViews() {
                $db = $this->getMockBuilder( DatabaseMysqli::class )
                        ->disableOriginalConstructor()