From 392f9490193abb12ed566d817a33eed4ef800768 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Thu, 27 Oct 2022 16:42:49 +0200 Subject: [PATCH] Simplify Telegram::classNameToCommandName() --- src/Telegram.php | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/Telegram.php b/src/Telegram.php index ec4ab39e..013f51d2 100644 --- a/src/Telegram.php +++ b/src/Telegram.php @@ -1298,29 +1298,7 @@ protected function classNameToCommandName(string $class): string if (substr($class, -7) !== 'Command') { return ''; } - $temp = substr($class, 0, -7); - $chunks = []; - $currentUpperCaseLetter = ''; - while ($temp !== '') { - if (!preg_match('/\p{Lu}/u', $temp, $match, PREG_OFFSET_CAPTURE)) { - break; - } - // $match[0][0] contains first upper case character - // $match[0][1] contains the start position (in bytes) of the first upper case character - [$upperCaseLetter, $upperCaseLetterOffset] = $match[0]; - if ($upperCaseLetterOffset > 0) { - $chunks[] = $currentUpperCaseLetter . substr($temp, 0, $upperCaseLetterOffset); - } elseif ($currentUpperCaseLetter !== '') { - $chunks[] = $currentUpperCaseLetter; - } - $temp = substr($temp, $upperCaseLetterOffset + strlen($upperCaseLetter)); - $currentUpperCaseLetter = $upperCaseLetter; - } - $lastChunk = $currentUpperCaseLetter . $temp; - if ($lastChunk !== '') { - $chunks[] = $lastChunk; - } - return implode('_', array_map('mb_strtolower', $chunks)); + return mb_strtolower(preg_replace('/(.)(?=[\p{Lu}])/u', '$1_', substr($class, 0, -7))); } /**