Skip to content

Convert Class Name from CamelCase to kebab-or-dash-case

I’m trying to find the logic in Magento (if it exists/is usable) that converts class name from Upper Camel Case -to-> Kebab Case (or spinal case, dash case, lisp case, caterpillar case, hyphen case).

In a module, I’m trying to refactor 9 CLI Command Classes (and more in the future) that extends from SymfonyComponentConsoleCommandCommand :

class MyVeryCustomCommand extends MyAbstractCommand
{
    protected string $commandName = 'my-very-custom-command-';

I’m trying to remove this definition of name in each command by implementing getClassNameInKebabCase():

class MyAbstractCommand extends SymfonyComponentConsoleCommandCommand
{
    protected function configure()
    {
        $this->setName(self::PREFIX . $this->getClassNameInKebabCase());
        // INSTEAD OF
        // $this->setName(self::PREFIX . $this->commandName);
    }

It would be even perfect if the logic is already somewhere in the framework !
(Maybe leveraging some convention that need the same string conversion)

Even if not, I think this function would be useful elsewhere, and is worth the 9*2 lines removed.

Secondary question : for the reverse function, could this php function be used ucfirst() ?