Skip to content

Commit f4db331

Browse files
committed
minor #21676 [Serializer] Reduce complexity of NameConverter (gadelat)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Serializer] Reduce complexity of NameConverter Cleaner and faster implementation of camelcase normalization. Speed improvement is particularly visible in long string input. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 50ca944 [Serializer] Reduce complexity of NameConverter
2 parents fd65bcc + 50ca944 commit f4db331

File tree

1 file changed

+1
-13
lines changed

1 file changed

+1
-13
lines changed

src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,7 @@ public function __construct(array $attributes = null, $lowerCamelCase = true)
4444
public function normalize($propertyName)
4545
{
4646
if (null === $this->attributes || in_array($propertyName, $this->attributes)) {
47-
$lcPropertyName = lcfirst($propertyName);
48-
$snakeCasedName = '';
49-
50-
$len = strlen($lcPropertyName);
51-
for ($i = 0; $i < $len; ++$i) {
52-
if (ctype_upper($lcPropertyName[$i])) {
53-
$snakeCasedName .= '_'.strtolower($lcPropertyName[$i]);
54-
} else {
55-
$snakeCasedName .= strtolower($lcPropertyName[$i]);
56-
}
57-
}
58-
59-
return $snakeCasedName;
47+
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
6048
}
6149

6250
return $propertyName;

0 commit comments

Comments
 (0)