Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f93113e

Browse files
committed
[String] Fix *String::snake methods
The ByteString::snake and AbstractUnitcodeString::snake methods had a bug that caused incorrect string conversion results for all uppercase words separated by space or "_" character. Ex. "GREAT SYMFONY" was converted to "greatsymfony" instead of "great_symfony"
1 parent e79eea1 commit f93113e

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ public function reverse(): parent
366366

367367
public function snake(): parent
368368
{
369-
$str = $this->camel();
370-
$str->string = mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1_\2', $str->string), 'UTF-8');
369+
$str = clone $this;
370+
$str->string = str_replace(' ', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));
371371

372372
return $str;
373373
}

src/Symfony/Component/String/ByteString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ public function slice(int $start = 0, ?int $length = null): parent
366366

367367
public function snake(): parent
368368
{
369-
$str = $this->camel();
370-
$str->string = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $str->string));
369+
$str = clone $this;
370+
$str->string = str_replace(' ', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));
371371

372372
return $str;
373373
}

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,8 @@ public static function provideSnake()
10771077
['symfony_is_great', 'symfonyIsGREAT'],
10781078
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
10791079
['symfony', 'SYMFONY'],
1080+
['symfony_is_great', 'SYMFONY IS GREAT'],
1081+
['symfony_is_great', 'SYMFONY_IS_GREAT'],
10801082
];
10811083
}
10821084

0 commit comments

Comments
 (0)