Skip to content

Command name resolution depends on namespace and loading method #1508

Open

Description

@akalongman

🐞 Bug Report

Required Information

? !
Operating system n/a
PHP Telegram Bot version dev after 0.84.0 (4daf9a6)
PHP version 8.5
MySQL version none
Update Method n/a
Self-signed certificate n/a
RAW update (if available) n/a

Summary

Since 0.80.0 (#1365) a class FooBarCommand is meant to answer /foo_bar. Which name actually reaches a command class still depends on two things that should not matter: the namespace of the class, and whether it was loaded from a commands path or added with addCommandClass().

Current behaviour

For a class loaded from a commands path:

  • getCommandsList() registers it under classNameToCommandName() of the file name, so FooBarCommand is foo_bar. The $name property is not used.
  • /foobar also reaches FooBarCommand, but only when the class is in a default Longman\TelegramBot\Commands\*Commands namespace: the fallback in getCommandObject() builds ...\UserCommands\FoobarCommand, and PHP class names are case-insensitive, so class_exists() finds FooBarCommand.
  • In a custom namespace there is no such fallback, so a class BazQuxCommand with protected $name = 'bazqux' answers only /baz_qux, and /bazqux falls through to GenericCommand.

For a class added with addCommandClass(), the $name property is the command name, so the same BazQuxCommand answers /bazqux.

How to reproduce

Two command files, one per directory:

// default/FooBarCommand.php
namespace Longman\TelegramBot\Commands\UserCommands;
class FooBarCommand extends UserCommand { protected $name = 'foobar'; protected $usage = '/foobar'; /* execute() */ }

// custom/BazQuxCommand.php
namespace My\Bot;
class BazQuxCommand extends UserCommand { protected $name = 'bazqux'; protected $usage = '/bazqux'; /* execute() */ }
$telegram->addCommandsPaths([__DIR__ . '/default', __DIR__ . '/custom']);
$list = $telegram->getCommandsList();
foreach (['foo_bar', 'foobar', 'baz_qux', 'bazqux'] as $c) {
    $o = $list[$c] ?? $telegram->getCommandObject($c);
    echo $c, ' -> ', $o ? get_class($o) : 'null', PHP_EOL;
}

Output:

foo_bar  -> Longman\TelegramBot\Commands\UserCommands\FooBarCommand
foobar   -> Longman\TelegramBot\Commands\UserCommands\FooBarCommand
baz_qux  -> My\Bot\BazQuxCommand
bazqux   -> null

Expected behaviour

One rule for the command name, whatever the namespace and however the class was loaded. Possible directions:

  • register path-loaded classes under getName() when $name is set, matching addCommandClass(), and fall back to the class name otherwise;
  • or make the class name the only source and stop the case-insensitive fallback from matching /foobar to FooBarCommand, documenting $name as display-only.

Either is a behaviour change for some existing bots, so it needs a changelog note. Related: #1312 (types resolving to command classes), #1364 and #1365 (underscore support), #1507 (CLAUDE.md wording).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions