core icon indicating copy to clipboard operation
core copied to clipboard

namespaces incorrect check in custon commands

Open sedovserge opened this issue 5 years ago • 5 comments

🐞 Bug Report

Please fix regex check namespaces in command custom files: in https://github.com/php-telegram-bot/core/blob/9ca6158f9fe6cf5aa06f9c46377b3c1164bdf444/src/Telegram.php#L350

if you set a tab or a space before 'namespace ...', this regex does not work as expected

this solution fixed issue: if (preg_match('#^\s+namespace\s+(.+?);#m', $content, $m)) {

? !
Operating system Ubuntu
PHP Telegram Bot version last
PHP version 7.4
MySQL version none
Update Method Webhook / getUpdates

sedovserge avatar Apr 01 '21 23:04 sedovserge

Hi @alligator77, thanks for reporting!

We just need to make sure that there are no false positives, so we try to keep that regex as "strict" as possible.

Out of curiosity, why do you have your namespace declaration indented?

noplanman avatar Apr 01 '21 23:04 noplanman

Hi @alligator77, thanks for reporting!

We just need to make sure that there are no false positives, so we try to keep that regex as "strict" as possible.

Out of curiosity, why do you have your namespace declaration indented?

#^\s+namespace\s+(.+?);#m works fine with and without spaces or tabs PHPStorm indented this by default

sedovserge avatar Apr 01 '21 23:04 sedovserge

Counter question, why isn't PSR4 used to include command classes?

Minimum php version in composer 7.x, but still use require_once? Moreover, in such a strange way of double loading, the first time to load class, and the second is get the namespace

sedovserge avatar Apr 01 '21 23:04 sedovserge

@alligator77 Very valid question. The whole structure of the commands is very not-pretty, I know. The initial idea of how commands should be loaded is an outdated way of thinking but the new way hasn't been implemented yet.

There are a few issues about this but nobody (including me) has had the time to have a go at it 😕 (Check the "Commands" column here: https://github.com/php-telegram-bot/core/projects/1)

Absolutely it should be managed better though, which will hopefully happen at some point!

noplanman avatar Apr 02 '21 16:04 noplanman

@alligator77 Very valid question. The whole structure of the commands is very not-pretty, I know. The initial idea of how commands should be loaded is an outdated way of thinking but the new way hasn't been implemented yet.

There are a few issues about this but nobody (including me) has had the time to have a go at it 😕 (Check the "Commands" column here: https://github.com/php-telegram-bot/core/projects/1)

Absolutely it should be managed better though, which will hopefully happen at some point!

I think, need to predefined commands list (These class files are in fact already present and rarely change in production) commans in config by name in array key, like:

[
  'User' => [
     'start' => 'name\space\to\StartCommand',
  ],
  'Admin' => [], //etc
]

and then load classes if need via psr.

Just need to refactoring method getCommandObject and add property for commands list.. Essentially, there are few changes and old logic with path`s will work too.

I can fork the bot and try to implement this improvement, then pull request?

sedovserge avatar Apr 02 '21 19:04 sedovserge