Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

flynnmj's avatar

Larastan has errors when it gets to my model files.

Trying to clean up some code in a project so I installed nunomaduro/larastan and barryvdh/laravel-ide-helper. When I run ./vendor/bin/phpstan analyze I get errors about Cannot instantiate abstract class Illuminate\Database\Eloquent\Model. I'm sure this is more an issue with getting this setup. Any pointers for what I might have missed setting this up? Thanks

I get error message along the lines of:

 Internal error: Internal error: Cannot instantiate abstract class Illuminate\Database\Eloquent\Model in file .../src/app/Models/User.php

When I run with the --debug flag:

   Error

  Cannot instantiate abstract class Illuminate\Database\Eloquent\Model

  at vendor/nunomaduro/larastan/src/Properties/ModelPropertyExtension.php:76
     72▕         try {
     73▕             $reflect = new \ReflectionClass($modelName);
     74▕
     75▕             /** @var Model $modelInstance */
  ➜  76▕             $modelInstance = $reflect->newInstanceWithoutConstructor();
     77▕
     78▕             $tableName = $modelInstance->getTable();
     79▕         } catch (\ReflectionException $e) {
     80▕             return false;

My phpstan.neon file:

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

parameters:
    # The level 8 is the highest level
    level: 1

    paths:
        - app

    scanFiles:
        - _ide_helper.php
        - _ide_helper_models.php
        - .phpstorm.meta.php
0 likes
4 replies
Kobayakawa's avatar

Why are you adding ide helper files to scanFiles? That probably causes the issues. You shouldn't need that really.

1 like
flynnmj's avatar

TLDR: Let laravel-ide-helper create the automatic PHPDocs in your model files and don't use _ide_helper_models.php.

I was getting a lot of warnings/errors like: Access to an undefined property App\Models\Model::$property. It seems like barryvdh/laravel-ide-helper usually helps to get rid of those messages. So I installed it and performed the three artisan commands for that package. The ide-helper:models command was run with the -N option. As I'm still testing this out, I didn't want to make changes to the source at this point. I then added those files as scanFiles in the phpstan.neon file.

Given your question, I blew away all the changes to my project and started this process over. Getting the property message again, I installed barryvdh/laravel-ide-helper again and only ran the ide-helper:models command and only added the _ide_helper_models.php to the scanFiles parameter. This results in getting a lot of Reflection error: Eloquent not found. messages. My next step was to run the ide-helper:models command again, but let it modify my model files. Larastan/phpstan is now finishing without issue and have a list of things I need to fix.

1 like
Kobayakawa's avatar

Glad you solved it! Yeah, writing the properties to model files is a nice solution. But Larastan also scans your migrations files to understand your model properties. Does that not work for you?

flynnmj's avatar

I never included the database directory in my path so I don't think Larastan knew about the migrations. Not sure that adding that path would help me long term. I'm planning on pruning the nearly 300 migration files the project has when I do php artisan schema:dump --prune.

Just cause I've had a hard time getting examples of a phpstan.neon file for a Laravel project, here's mine:

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

parameters:
    paths:
        - app

    # The level 8 is the highest level
    level: 7

    ignoreErrors:
        - '#Property [A-Za-z0-9\]+::$[A-Za-z0-9_]+ has no typehint specified#'
        - '#Method [A-Za-z0-9\]+::[A-Za-z0-9_]+\(\) has no return typehint specified#'
        - '#Method [A-Za-z0-9\]+::[A-Za-z0-9_]+\(\) has parameter $[A-Za-z0-9_]+ with no typehint specified\.#'
        - '#Method [A-Za-z0-9\]+::[A-Za-z0-9_]+\(\) should return Illuminate\Http\Response but returns Illuminate\.*\.#'

    checkMissingIterableValueType: false

I had started out increasing the level and working on then new errors. Then I just decided to crank it up to 7 and see what I got. While I can see the value in typehints, they are not (<-edit) a high priority to get fixed right now. Once the code is in better shape, I reevaluate my ignoreErrors in use.

4 likes

Please or to participate in this conversation.