Core: How can I debug command file?

Created on 4 Jun 2017  路  11Comments  路  Source: php-telegram-bot/core

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.

All 11 comments

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.

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

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?!

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

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);

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)

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

Why? Logically identical. is not?

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

@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.

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?

Sure, Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheSleepySlee picture TheSleepySlee  路  3Comments

Zoha picture Zoha  路  3Comments

marcolino7 picture marcolino7  路  3Comments

smaznet picture smaznet  路  4Comments

ttvd94 picture ttvd94  路  4Comments