Larastan: Larastan is not working with database factories

Created on 25 Sep 2018  路  4Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.2.11
  • --level used: 0

Description:

The magic side of larastan is not working with database factories and the error below is shown when analysing this kind of file:

 ------ -------------------------------------------------- 
  Line   database/factories/MyModel.php  
 ------ -------------------------------------------------- 
  6      Undefined variable: $factory                      
 ------ -------------------------------------------------- 

Laravel code where the issue was found:

use Faker\Generator as Faker;
use App\MyModel;

$factory->define(MyModel::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'description' => $faker->sentence(6, true),
    ];
});

Is there any way to analyse or simply ignore these files temporarily?

Most helpful comment

Don't forget to to use includes: to keep Larastan's configuration.

But the ideal would be to teach Larastan how to read this code.
See #140

All 4 comments

parameters:
    excludes_analyse:
        - %rootDir%/../../../database/factories/MyModel.php

You may exclude files and suppress errors too. Please see the readme: https://github.com/phpstan/phpstan

Don't forget to to use includes: to keep Larastan's configuration.

But the ideal would be to teach Larastan how to read this code.
See #140

Great! While this analysis is not provided by larastan itself, it's working with this snippet of configuration:

includes:
    - vendor/nunomaduro/larastan/extension.neon

parameters:
    excludes_analyse:
        - %rootDir%/../../../*/factories/*.php

And the command to use the custom conf file is:

$ ./artisan code:analyse --paths='app,modules' --level=0 --configuration=/path/to/app/phpstan.neon

Thank you very much!

You're welcome.

Was this page helpful?
0 / 5 - 0 ratings