[Utf8] New component with Bytes, CodePoints and Graphemes implementations of string objects

by @nicolas-grekas

Some issues have been detected in this pull request

Issues that can be fixed by applying a patch

Review the proposed patch then download it to apply it manually or execute the following command from the repository root directory:

curl https://fabbot.io/patch/symfony/symfony/22184/6bc725aed9c9af708f0a2f32af7fadd49b6002b4/cs.diff | patch -p0
diff -ru src/Symfony/Component/Utf8/Bytes.php src/Symfony/Component/Utf8/Bytes.php
--- src/Symfony/Component/Utf8/Bytes.php	2019-09-11 10:21:24.842873078 +0000
+++ src/Symfony/Component/Utf8/Bytes.php	2019-09-11 10:21:29.135831415 +0000
@@ -46,7 +46,7 @@
      */
     public function count(): int
     {
-        return strlen($this->string);
+        return \strlen($this->string);
     }
 
     /**
@@ -54,7 +54,7 @@
      */
     public function length(): int
     {
-        return strlen($this->string);
+        return \strlen($this->string);
     }
 
     /**
@@ -243,14 +243,14 @@
             return null;
         }
 
-        if (0 <= $offset || PHP_VERSION_ID >= 70100) {
+        if (0 <= $offset || \PHP_VERSION_ID >= 70100) {
             $result = strpos($this->string, $needle, $offset);
 
             return false === $result ? null : $result;
         }
 
         // Workaround to support negative offsets with strpos() in PHP < 7.1
-        $start = $offset + strlen($this->string);
+        $start = $offset + \strlen($this->string);
         $offset = strpos(substr($this->string, $start), $needle);
 
         return false === $offset ? null : $start + $offset;
@@ -269,12 +269,12 @@
         setlocale(LC_CTYPE, 'C');
 
         try {
-            if (0 <= $offset || PHP_VERSION_ID >= 70100) {
+            if (0 <= $offset || \PHP_VERSION_ID >= 70100) {
                 $result = stripos($this->string, $needle, $offset);
                 $result = false === $result ? null : $result;
             } else {
                 // Workaround to support negative offsets with stripos() in PHP < 7.1
-                $start = $offset + strlen($this->string);
+                $start = $offset + \strlen($this->string);
                 $offset = stripos(substr($this->string, $start), $needle);
                 $result = false === $offset ? null : $start + $offset;
             }
@@ -453,7 +453,7 @@
                 $s = preg_replace('/\x1B\[[\d;]*m/', '', $s);
             }
 
-            if ($width < $c = strlen($s)) {
+            if ($width < $c = \strlen($s)) {
                 $width = $c;
             }
         }
@@ -477,18 +477,18 @@
      */
     public function replaceAll(array $from, array $to, int &$count = null): self
     {
-        if (count($from) !== count($to)) {
+        if (\count($from) !== \count($to)) {
             throw new InvalidArgumentException('The number of search patterns does not match the number of pattern replacements.');
         }
 
         foreach ($from as $k => $pattern) {
-            if (!is_string($pattern)) {
+            if (!\is_string($pattern)) {
                 throw new InvalidArgumentException(sprintf('Search pattern at key %s must be a valid string.', $k));
             }
         }
 
         foreach ($to as $k => $replacement) {
-            if (!is_string($replacement)) {
+            if (!\is_string($replacement)) {
                 throw new InvalidArgumentException(sprintf('Pattern replacement at key %s must be a valid string.', $k));
             }
         }
@@ -523,18 +523,18 @@
      */
     public function replaceAllIgnoreCase(array $from, array $to, int &$count = null): self
     {
-        if (count($from) !== count($to)) {
+        if (\count($from) !== \count($to)) {
             throw new InvalidArgumentException('The number of search patterns does not match the number of pattern replacements.');
         }
 
         foreach ($from as $k => $pattern) {
-            if (!is_string($pattern)) {
+            if (!\is_string($pattern)) {
                 throw new InvalidArgumentException(sprintf('Search pattern at key %s must be a valid string.', $k));
             }
         }
 
         foreach ($to as $k => $replacement) {
-            if (!is_string($replacement)) {
+            if (!\is_string($replacement)) {
                 throw new InvalidArgumentException(sprintf('Pattern replacement at key %s must be a valid string.', $k));
             }
         }
diff -ru src/Symfony/Component/Utf8/CodePoints.php src/Symfony/Component/Utf8/CodePoints.php
--- src/Symfony/Component/Utf8/CodePoints.php	2019-09-11 10:21:25.196869642 +0000
+++ src/Symfony/Component/Utf8/CodePoints.php	2019-09-11 10:21:29.233830464 +0000
@@ -77,7 +77,7 @@
             return null;
         }
 
-        if (0 <= $offset || PHP_VERSION_ID >= 70100) {
+        if (0 <= $offset || \PHP_VERSION_ID >= 70100) {
             $result = mb_strpos($this->string, $needle, $offset);
 
             return false === $result ? null : $result;
@@ -99,7 +99,7 @@
             return null;
         }
 
-        if (0 <= $offset || PHP_VERSION_ID >= 70100) {
+        if (0 <= $offset || \PHP_VERSION_ID >= 70100) {
             $result = mb_stripos($this->string, $needle, $offset);
 
             return false === $result ? null : $result;
diff -ru src/Symfony/Component/Utf8/Graphemes.php src/Symfony/Component/Utf8/Graphemes.php
--- src/Symfony/Component/Utf8/Graphemes.php	2019-09-11 10:21:25.932862500 +0000
+++ src/Symfony/Component/Utf8/Graphemes.php	2019-09-11 10:21:29.384828999 +0000
@@ -64,11 +64,11 @@
         }
 
         if (null === self::$hasIntl) {
-            self::$hasIntl = extension_loaded('intl');
+            self::$hasIntl = \extension_loaded('intl');
         }
 
         if (self::$hasIntl) {
-            $length = strlen($this->string);
+            $length = \strlen($this->string);
             $i = 0;
 
             while ($i < $length) {
diff -ru src/Symfony/Component/Utf8/Tests/AbstractAsciiTestCase.php src/Symfony/Component/Utf8/Tests/AbstractAsciiTestCase.php
--- src/Symfony/Component/Utf8/Tests/AbstractAsciiTestCase.php	2019-09-11 10:21:26.875853348 +0000
+++ src/Symfony/Component/Utf8/Tests/AbstractAsciiTestCase.php	2019-09-11 10:21:29.837824602 +0000
@@ -45,7 +45,7 @@
         $instance = static::createFromString($string);
 
         $this->assertSame($length, $instance->length());
-        $this->assertSame($length, count($instance));
+        $this->assertSame($length, \count($instance));
     }
 
     public static function provideLength()
diff -ru src/Symfony/Component/Utf8/Tests/CodePointsTest.php src/Symfony/Component/Utf8/Tests/CodePointsTest.php
--- src/Symfony/Component/Utf8/Tests/CodePointsTest.php	2019-09-11 10:21:27.415848107 +0000
+++ src/Symfony/Component/Utf8/Tests/CodePointsTest.php	2019-09-11 10:21:30.087822176 +0000
@@ -30,7 +30,7 @@
      */
     public function testCreateFromCodePoint(string $expected, array $codePoint)
     {
-        $this->assertEquals(CodePoints::fromString($expected), call_user_func_array(array(CodePoints::class, 'fromCodePoint'), $codePoint));
+        $this->assertEquals(CodePoints::fromString($expected), \call_user_func_array(array(CodePoints::class, 'fromCodePoint'), $codePoint));
     }
 
     public static function provideLength()
diff -ru src/Symfony/Component/Utf8/Tests/GraphemesTest.php src/Symfony/Component/Utf8/Tests/GraphemesTest.php
--- src/Symfony/Component/Utf8/Tests/GraphemesTest.php	2019-09-11 10:21:27.591846399 +0000
+++ src/Symfony/Component/Utf8/Tests/GraphemesTest.php	2019-09-11 10:21:30.227820818 +0000
@@ -22,7 +22,7 @@
 {
     protected function setUp()
     {
-        if (!function_exists('grapheme_strlen')) {
+        if (!\function_exists('grapheme_strlen')) {
             $this->markTestSkipped('Intl extension with Grapheme support is required to run this test.');
         }
     }
@@ -37,7 +37,7 @@
      */
     public function testCreateFromCodePoint(string $expected, array $codePoint)
     {
-        $this->assertEquals(Graphemes::fromString($expected), call_user_func_array(array(Graphemes::class, 'fromCodePoint'), $codePoint));
+        $this->assertEquals(Graphemes::fromString($expected), \call_user_func_array(array(Graphemes::class, 'fromCodePoint'), $codePoint));
     }
 
     public static function provideWidthData()
diff -ru src/Symfony/Component/Utf8/Utf8Trait.php src/Symfony/Component/Utf8/Utf8Trait.php
--- src/Symfony/Component/Utf8/Utf8Trait.php	2019-09-11 10:21:27.781844556 +0000
+++ src/Symfony/Component/Utf8/Utf8Trait.php	2019-09-11 10:21:30.404819100 +0000
@@ -42,13 +42,13 @@
         $string = '';
         foreach ($codes as $code) {
             if (0x80 > $code %= 0x200000) {
-                $string .= chr($code);
+                $string .= \chr($code);
             } elseif (0x800 > $code) {
-                $string .= chr(0xC0 | $code >> 6).chr(0x80 | $code & 0x3F);
+                $string .= \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
             } elseif (0x10000 > $code) {
-                $string .= chr(0xE0 | $code >> 12).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
+                $string .= \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
             } else {
-                $string .= chr(0xF0 | $code >> 18).chr(0x80 | $code >> 12 & 0x3F).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
+                $string .= \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
             }
         }
 
@@ -127,7 +127,7 @@
             $string = preg_replace_callback('/\b(.)/u', $callback, $this->string);
         } else {
             $capitalLetter = mb_substr($this->string, 0, 1, 'UTF-8');
-            $string = preg_replace_callback('/\b(.)/u', $callback, $capitalLetter).substr($this->string, strlen($capitalLetter));
+            $string = preg_replace_callback('/\b(.)/u', $callback, $capitalLetter).substr($this->string, \strlen($capitalLetter));
         }
 
         $result = clone $this;
@@ -309,12 +309,12 @@
      */
     public function replaceAll(array $from, array $to, int &$count = null): self
     {
-        if (count($from) !== count($to)) {
+        if (\count($from) !== \count($to)) {
             throw new InvalidArgumentException('The number of search patterns does not match the number of pattern replacements.');
         }
 
         foreach ($from as $k => $pattern) {
-            if (!is_string($pattern)) {
+            if (!\is_string($pattern)) {
                 throw new InvalidArgumentException(sprintf('Search pattern at key %s must be a valid string.', $k));
             }
 
@@ -324,7 +324,7 @@
         }
 
         foreach ($to as $k => $replacement) {
-            if (!is_string($replacement)) {
+            if (!\is_string($replacement)) {
                 throw new InvalidArgumentException(sprintf('Pattern replacement at key %s must be a valid string.', $k));
             }
 
@@ -363,12 +363,12 @@
      */
     public function replaceAllIgnoreCase(array $from, array $to, int &$count = null): self
     {
-        if (count($from) !== count($to)) {
+        if (\count($from) !== \count($to)) {
             throw new InvalidArgumentException('The number of search patterns does not match the number of pattern replacements.');
         }
 
         foreach ($from as $k => $pattern) {
-            if (!is_string($pattern)) {
+            if (!\is_string($pattern)) {
                 throw new InvalidArgumentException(sprintf('Search pattern at key %s must be a valid string.', $k));
             }
 
@@ -380,7 +380,7 @@
         }
 
         foreach ($to as $k => $replacement) {
-            if (!is_string($replacement)) {
+            if (!\is_string($replacement)) {
                 throw new InvalidArgumentException(sprintf('Pattern replacement at key %s must be a valid string.', $k));
             }
 

0
Common Typos

0
License Headers

0
Pull Request Contributor Headers

0
JSON Files Syntax

0
File Permissions

0
Merge Commits