Larastan: Artisan::command false positive

Created on 14 Sep 2018  Â·  11Comments  Â·  Source: nunomaduro/larastan

  • Larastan Version: 0.2.10
  • --level used: max

Description:

The $this context inside an Artisan callable is expected to be a Command, but larastan reports the following:

------ ------------------------------
  Line   routes/console.php
 ------ ------------------------------
  19     Undefined variable: $this
  19     Using $this outside a class.
 ------ ------------------------------


 [ERROR] Found 2 errors


Script php artisan code:analyse --level=max --paths="app,routes" handling the test event returned with error code 1

Laravel code where the issue was found:

routes/console.php:

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
false positive

Most helpful comment

I’ll try to improve this and reduce false positives for a future version of
PHPStan

On Mon, 24 Sep 2018 at 15:05, Derk-Jan Hartman notifications@github.com
wrote:

FYI, I have the same problem with Laravel macros.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/140#issuecomment-423967409,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuGf7nrHfQxSQBzulJ3REXgc_5SEiks5ueNh-gaJpZM4WpaaX
.

>

Ondřej Mirtes

All 11 comments

It will not only be reported by larastan. I get this message on Scrutinizer CI as well.

@ondrejmirtes There is a way to fix this?

I think the only way is to teach Larastan to handle

require base_path('routes/console.php'); in app/Console/Kernel.php

@szepeviktor Nah. I think there is a way of instruct PHPStan that $this corresponds to the class where the callback will be executed.

This area is hard, I don’t think there’s currently a way to fix this,
methods have no way of telling “this passed closure will be bound to class
X”.

On Mon, 24 Sep 2018 at 09:47, Nuno Maduro notifications@github.com wrote:

@szepeviktor https://github.com/szepeviktor Nah. I think there is a way
of instruct PHPStan that $this corresponds to the class where the callback
will be executed.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/140#issuecomment-423897532,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuOSMMxtCJTKB4v2pdg0o-KEiZF9Yks5ueI33gaJpZM4WpaaX
.

>

Ondřej Mirtes

@ondrejmirtes Any plans of adding this to PHPStan? And can create a Feature Request on phpstan repository if you want.

I’m accepting suggestions because I don’t know how to do it, there’s
certainly no expression in phpDoc that would enable this.

The only thing I could do would be to turn off all errors about $this in
closures but that’s not always neccesary.

On Mon, 24 Sep 2018 at 10:03, Nuno Maduro notifications@github.com wrote:

@ondrejmirtes https://github.com/ondrejmirtes Any plans of adding this
to PHPStan? And can create a Feature Request on phpstan repository if you
want.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/140#issuecomment-423900794,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuHEwLyTknWjOrdvEejOz13l_Kgreks5ueJHpgaJpZM4WpaaX
.

>

Ondřej Mirtes

Maybe this kind of feature is too much of black magic. I don't think IDE's would ever autocomplete methods based on $this inside a callable... It'd be easier for everyone if every part of Laravel was IDE friendly (remove facades in favor of helper methods, the $this context change, etc).

FYI, I have the same problem with Laravel macros.

I’ll try to improve this and reduce false positives for a future version of
PHPStan

On Mon, 24 Sep 2018 at 15:05, Derk-Jan Hartman notifications@github.com
wrote:

FYI, I have the same problem with Laravel macros.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/140#issuecomment-423967409,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuGf7nrHfQxSQBzulJ3REXgc_5SEiks5ueNh-gaJpZM4WpaaX
.

>

Ondřej Mirtes

Duplicated of #42

Was this page helpful?
0 / 5 - 0 ratings