php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 955 forks source link

How can I debug command file? #517

Closed NabiKAZ closed 7 years ago

NabiKAZ commented 7 years ago

How can I debug command file? For example if we have:

<?php
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;

class HelloCommand extends UserCommand
{
    protected $name = 'hello';
    protected $description = '';
    protected $usage = '/hello';
    protected $version = '1.0.0';

    public function execute()
    {
        aaaaaaaaaaaaaaaaaaaaaaa
    }
}

I configure error, update, debug log file. But does not show any Syntax error into that.

jacklul commented 7 years ago

That's far from debugging, merely just troubleshooting and error finding.

It depends on server's PHP configuration where the error log is stored.

Or manually in hook script:

ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php-error.log');

For real debbuging you need other libraries specifically made for it, I can't name them as it's not really my specialization.

NabiKAZ commented 7 years ago

Thanks. There are an extra ) at end of line :wink:

NabiKAZ commented 7 years ago

And what is your suggestion for var dump variables and test output? I use it and see output in robot!

return Request::sendMessage(['chat_id' => $this->getMessage()->getChat()->getId(), 'text' => print_r($var, true)]);

Do you have any better and faster idea?!

noplanman commented 7 years ago

Instead of sending yourself a message through Telegram, just use the TelegramLog for logging!

TelegramLog::debug($whatever);

Or, if you really want to send yourself messages through Telegram, this helper function might come in handy: https://github.com/php-telegram-bot/core/wiki/Snippets#dumper

NabiKAZ commented 7 years ago

It's not bad:

\Longman\TelegramBot\TelegramLog::debug($whatever);

And so can see real time clear output with this command:

tailf SampleBot_debug.log | grep bot_log.DEBUG

And so other way is not bad:

\Longman\TelegramBot\Helpers::dump($whatever, $chat_id);

But I write a simple function for this:

    /**
     * Var dump variable for debuging
     *
     * @return
     */
    public function d($var, $die = false)
    {
        $data = [
            'chat_id' => $this->getMessage()->getChat()->getId(),
            'text' => print_r($var, true),
        ];
        Request::sendMessage($data);
        if ($die == true) {
            die;
        }
    }

And for use, just can use this command:

$this->d($whatever);
noplanman commented 7 years ago

Right, but with this solution you'll have to add it to every command you'd like to use it with, thus having duplicate (or worse) code.

But if you only need it in the single command, then it's a perfect solution 👍

(note, instead of $die == true, just use $die)

NabiKAZ commented 7 years ago

(note, instead of $die == true, just use $die)

Why? Logically identical. is not?

noplanman commented 7 years ago

Yes it is, it's just not necessary and adds a tiny bit of difficulty to read, as it could mean something else. The less unnecessary code the better.

If you really need to test for true (the boolean type), then you should use === and it would be perfectly legitimate.

Also, since we're on the topic, are you aware of the difference between == and ===? If not, look here: https://secure.php.net/manual/en/language.operators.comparison.php

NabiKAZ commented 7 years ago

@noplanman I understand you, and I thank you for your finesse, I'm always in love with the nuances of the code. But I thought The == true there was more to the readability of the code helps. And I know about === (It's my question before!), but I thinks not important exactly boolean compare in here.

noplanman commented 7 years ago

It's actually up to you to do what feels best and is most understandable for you! That's what defines each programmers own style 👍

There are just some "style guides" that try to help make code understandable and easy to read for the majority of developers.

In your personal code, you do what seems the nicest to you and enjoy watching your code evolve over time 😃

Can we close off this issue here?

NabiKAZ commented 7 years ago

Sure, Thanks.