-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[String] Introduce underscore()
method
#34781
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
Conversation
f38cae4
to
c1876cd
Compare
public function underscore(): parent | ||
{ | ||
$str = $this->camel()->snake(); | ||
$str->string = mb_strtolower(preg_replace('~(?<=[a-z])([0-9])~u', '_$1', $str->string), 'UTF-8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doing camel -> snake -> regexp replacement -> lowercase
, where snake
itself is already doing camel -> title -> regex replacement ->lowercase
with a regex keeping numbers in the previous word ? This duplicates the camelcase conversion, performs 2 regexp replacement where one could be enough, and converts to lowercase twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently doing camel -> title -> regex -> lowercase
. Good?
I think I addressed all comments, thank you. Did I miss to test something? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm genuinely wondering about the difference with snake()
Yes, I can personnally understand it by looking at the code and maybe the tests, but for anyone else after (future me included), when should I use snake or underscore? Is that obvious to anyone?
We clearly need some phpdoc explanation the difference (or maybe we should not have both ?) |
I have not found any "string manipulation standards". But looking through internet, I found there's generally consensus on that snake does not consider numbers as another "word". But difference should be documented for sure. |
I agree that this may be confusing. I can think of two possible solutions:
|
cb51e19
to
c773e1f
Compare
|
Are you sure?
|
@gharlan you're correct, the strategy changes |
is "underscore case" some widely adopted pattern? I cant find any reference about it ... as opposed to "snake case": https://en.wikipedia.org/wiki/Snake_case Nor does "snake numeric case" seem to exist. Im 👎 for inventing our own cases :) |
I'm sorry but I'm 👎 too: looking for prior art on the topic, it's not clear at all that there is anything named "underscore case" that differs from "snake case". |
Ok, thanks for feedback! |
It's used by Symfony for the template name https://github.com/symfony/symfony/tree/7.1/src/Symfony/Bridge/Twig/Resources/views/Form instead of "pure" snake case. (Which is weird when symfony recommend snake case https://symfony.com/doc/current/best_practices.html#use-snake-case-for-template-names-and-variables) Should Symfony rename these templates then ? |
Provides
underscore()
method to String component. Similar tosnake()
but also adds_
before numbers.This pattern is used eg. by Doctrine ORM UnderscoreNamingStrategy with number aware enabled.