Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cawllec/console request #398

Merged
merged 15 commits into from
Aug 29, 2017
Merged

Cawllec/console request #398

merged 15 commits into from
Aug 29, 2017

Conversation

Cawllec
Copy link
Contributor

@Cawllec Cawllec commented Aug 24, 2017

Re this issue in bugsnag-laravel this implements a basic console-request class that will take an array of the console commands and attach them as meta-data.

This gives a handler for framework implementations, while leaving it framework-specific on how console input is acquired.

/**
* Create a new console request instance.
*
* @param array $commandArray An array of the command line input
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment bug. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something StyleCI could lint for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, but it is planned. I think it's actually been planned for a very long time, since I think you mentioned this feature last year too. :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, explains the deja vu I felt when I typed that :p

/**
* The unformated console command.
*
* @var array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string[] I think is the type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're allegedly the same, but string[] is more explicit to the expected array elements

*
* @return void
*/
public function __construct(array $commandArray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conventional to match the internal property name, so name the param $command?

@@ -12,7 +12,11 @@ class BasicResolver implements ResolverInterface
public function resolve()
{
if (!isset($_SERVER['REQUEST_METHOD'])) {
return new NullRequest();
if ((PHP_SAPI == 'cli') && (isset($_SERVER['argv']))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra brackets are not needed, and === should be used for the string comparison.

*/
public function getContext()
{
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return implode(' ', $this->command) perhaps would be a good context, or perhaps, limit this to only the first upto 2 parts of the array?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first two parts would be good, running this with artisan commands as well as plain php produces useful results, moreso than the default which is the top line of the stacktrace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will return at most the first 2 parts of the command, concatenated together with a space. If the array is smaller than 2 elements, it is tolerant to that, and will just return the singleton array, or the empty array.

return implode(' ', array_slice($this->command, 0, 2));

@GrahamCampbell
Copy link
Contributor

Reviewed this in some detail. :)

*/
public function isRequest()
{
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably has to be false, otherwise, this pull request would be considered a breaking change. Previously, "request" meant "HTTP request", not just "invocation context", or whatever name we wanna give to this. :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, to avoid potential issues :)

*/
public function getMetaData()
{
if (is_array($this->command)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the is_array check. It is guaranteed to be true. PHP doesn't allow null through an array type.

$arguments = [];
$options = [];
foreach (array_slice($this->command, 1) as $arg) {
$arg[0] == '-' ? $options[] = $arg : $arguments[] = $arg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment inside ternaries is somewhat unusual. Perhaps consider just a plain old if. :)

@@ -12,7 +12,11 @@ class BasicResolver implements ResolverInterface
public function resolve()
{
if (!isset($_SERVER['REQUEST_METHOD'])) {
return new NullRequest();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method could be updated to avoid the nested ifs:

    /**
     * Resolve the current request.
     *
     * @return \Bugsnag\Request\RequestInterface
     */
    public function resolve()
    {
        if (isset($_SERVER['REQUEST_METHOD'])) {
            return new PhpRequest(
                $_SERVER,
                empty($_SESSION) ? [] : $_SESSION,
                empty($_COOKIE) ? [] : $_COOKIE,
                static::getRequestHeaders($_SERVER),
                static::getInputParams($_SERVER, $_POST)
            );
        }

        if (PHP_SAPI === 'cli' && isset($_SERVER['argv'])) {
            return new ConsoleRequest($_SERVER['argv']);
        }

        return new NullRequest();
    }

*/
public function getContext()
{
return implode(' ', array_slice($this->command, 0, 2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized that php doesn't include the executable in the arguments list. Maybe we should just take the first part of the array then, or implode the whole thing. Ping @kattrali - what do you think is the most sensible option here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think first few args still works well enough. The cases I can try/think of:

  • php [filename] => [filename]
  • php [filename] foo bar => [filename] foo
  • php [filename] --foo bar --baz red => [filename] --foo <-- this one is a lil odd
  • php artisan migrate => artisan migrate

I could see the value in imploding the whole thing, but also don't want context to get arbitrarily long. We could max four args for now and revisit with feedback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but also don't want context to get arbitrarily long

Yeh, totally. We need to find the balance where it is useful but not unbounded.

$arguments = [];
$options = [];
foreach (array_slice($this->command, 1) as $arg) {
if ($arg[0] == '-') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safest option is to do: if (isset($arg[0]) && $arg[0] === '-') {.

'Command' => 'some',
'Arguments' => ['test', 'command'],
'Options' => ['--opt'],
]], $report->getMetaData());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth also doing an assertion on the report context too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrahamCampbell Should there be a test which is different from this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ha. No, that looks great. ;)

@GrahamCampbell
Copy link
Contributor

Left 2 more tiny comments. Otherwise, looks good. 🚀 🚀 🚀

'Command' => 'some',
'Arguments' => ['test', 'command'],
'Options' => ['--opt'],
]], $report->getMetaData());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ha. No, that looks great. ;)

@kattrali kattrali merged commit f234045 into master Aug 29, 2017
@kattrali kattrali deleted the cawllec/console-request branch August 29, 2017 21:46
@kattrali
Copy link
Contributor

🚢 🚢 🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants