Skip to content

Commit

Permalink
Simplify Telegram::classNameToCommandName()
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Oct 27, 2022
1 parent 63ba67d commit 392f949
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

/**
Expand Down

0 comments on commit 392f949

Please sign in to comment.