Skip to content

Commit

Permalink
Changed return types of class name <=> file name methods to null, if …
Browse files Browse the repository at this point in the history
…something's wrong
  • Loading branch information
TiiFuchs committed Nov 7, 2022
1 parent f79c799 commit 8e8dc64
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,15 @@ public function getCommandsList(): array
);

foreach ($files as $file) {
//Remove "Command.php" from filename
$command = $this->classNameToCommandName(substr($file->getFilename(), 0, -4));
// Convert filename to command
$command = $this->classNameToCommandName(substr($file->getFilename(), 0, -4));

// Invalid Classname
if (is_null($command)) {
continue;
}

// Already registered
if (array_key_exists($command, $commands)) {
continue;
}
Expand Down Expand Up @@ -318,6 +324,12 @@ public function getCommandsList(): array
public function getCommandClassName(string $auth, string $command, string $filepath = ''): ?string
{
$command = mb_strtolower($command);

// Invalid command
if (trim($command) === '') {
return null;
}

$auth = $this->ucFirstUnicode($auth);

// First, check for directly assigned command class.
Expand Down Expand Up @@ -1290,14 +1302,15 @@ public function getUpdateFilter(): ?callable
*
* @param string $class For example FooBarCommand
*
* @return string for example foo_bar. In case of errors, returns an empty string
* @return string|null for example foo_bar. In case of errors, returns null.
*/
protected function classNameToCommandName(string $class): string
protected function classNameToCommandName(string $class): ?string
{
// 7 is the length of 'Command'
// If $class doesn't end with 'Command'
if (substr($class, -7) !== 'Command') {
return '';
return null;
}

return mb_strtolower(preg_replace('/(.)(?=[\p{Lu}])/u', '$1_', substr($class, 0, -7)));
}

Expand All @@ -1306,13 +1319,14 @@ protected function classNameToCommandName(string $class): string
*
* @param string $command For example foo_bar
*
* @return string for example FooBarCommand. In case of errors, returns an empty string
* @return string|null for example FooBarCommand. In case of errors, returns null.
*/
protected function commandNameToClassName(string $command): string
protected function commandNameToClassName(string $command): ?string
{
if ($command === '') {
return '';
if (trim($command) === '') {
return null;
}

return str_replace(' ', '', $this->ucWordsUnicode(str_replace('_', ' ', $command))) . 'Command';
}
}

0 comments on commit 8e8dc64

Please sign in to comment.