8000 Wrong conversion to snake case from upper snake case · Issue #47054 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Wrong conversion to snake case from upper snake case #47054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simPod opened this issue Jul 25, 2022 · 4 comments · Fixed by #47185
Closed

Wrong conversion to snake case from upper snake case #47054

simPod opened this issue Jul 25, 2022 · 4 comments · Fixed by #47185

Comments

@simPod
Copy link
Contributor
simPod commented Jul 25, 2022

Symfony version(s) affected

6.1.2

Description

Symfony String converts X_Y into xy while I expect it to do X_Y. X_Y is in upper-snake case, the underscore should not be thrown away.

How to reproduce

\Symfony\Component\String\u('X_Y')->snake()

Possible Solution

No response

Additional Context

No response

@derrabus
Copy link
Member

Thank you. Can you work on a PR?

@RePrete
Copy link
RePrete commented Jul 27, 2022

I think I got the issue, in AbstractUnicodeString::snake the first thing done is

$str = $this->camel()->title();

Inside AbstractUnicodeString::camel there is a replace of all the non-letter or non-digit chars with a space (inner preg_replace) and then replace the space with empty string.
So this is why the underscore is gone.

I am trying to understand if the camel call is correct in this case.

@RePrete
Copy link
RePrete commented Jul 28, 2022

After some other investigations, it looks like it's an extreme edge case.
Currently, it's happening only with strings ending with an underscore followed by all (one or more) capital letters.
Basically:

A_A  => aa    //wrong
A_Aa => a_aa  //correct
A_AA => aaa   //wrong

To be honest I don't know how and if it's worth to change the implementation.

@simPod
Copy link
Contributor Author
simPod commented Aug 4, 2022

No, it's a bug. snakecase should convert all strings to snakecase not just some.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0