Skip to content

Commit 8e8dc64

Browse files
committed
Changed return types of class name <=> file name methods to null, if something's wrong
1 parent f79c799 commit 8e8dc64

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/Telegram.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,15 @@ public function getCommandsList(): array
282282
);
283283

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

288+
// Invalid Classname
289+
if (is_null($command)) {
290+
continue;
291+
}
292+
293+
// Already registered
288294
if (array_key_exists($command, $commands)) {
289295
continue;
290296
}
@@ -318,6 +324,12 @@ public function getCommandsList(): array
318324
public function getCommandClassName(string $auth, string $command, string $filepath = ''): ?string
319325
{
320326
$command = mb_strtolower($command);
327+
328+
// Invalid command
329+
if (trim($command) === '') {
330+
return null;
331+
}
332+
321333
$auth = $this->ucFirstUnicode($auth);
322334

323335
// First, check for directly assigned command class.
@@ -1290,14 +1302,15 @@ public function getUpdateFilter(): ?callable
12901302
*
12911303
* @param string $class For example FooBarCommand
12921304
*
1293-
* @return string for example foo_bar. In case of errors, returns an empty string
1305+
* @return string|null for example foo_bar. In case of errors, returns null.
12941306
*/
1295-
protected function classNameToCommandName(string $class): string
1307+
protected function classNameToCommandName(string $class): ?string
12961308
{
1297-
// 7 is the length of 'Command'
1309+
// If $class doesn't end with 'Command'
12981310
if (substr($class, -7) !== 'Command') {
1299-
return '';
1311+
return null;
13001312
}
1313+
13011314
return mb_strtolower(preg_replace('/(.)(?=[\p{Lu}])/u', '$1_', substr($class, 0, -7)));
13021315
}
13031316

@@ -1306,13 +1319,14 @@ protected function classNameToCommandName(string $class): string
13061319
*
13071320
* @param string $command For example foo_bar
13081321
*
1309-
* @return string for example FooBarCommand. In case of errors, returns an empty string
1322+
* @return string|null for example FooBarCommand. In case of errors, returns null.
13101323
*/
1311-
protected function commandNameToClassName(string $command): string
1324+
protected function commandNameToClassName(string $command): ?string
13121325
{
1313-
if ($command === '') {
1314-
return '';
1326+
if (trim($command) === '') {
1327+
return null;
13151328
}
1329+
13161330
return str_replace(' ', '', $this->ucWordsUnicode(str_replace('_', ' ', $command))) . 'Command';
13171331
}
13181332
}

0 commit comments

Comments
 (0)