3

Problem

I recently started using PHPStan to analyse my code for my Laravel projects but I keep getting the following error for all my models:

 ------ --------------------------------------------------------------------
  Line   Models/Fund.php
 ------ --------------------------------------------------------------------
  14     PHPDoc tag @mixin contains unknown class App\Models\IdeHelperFund.
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ --------------------------------------------------------------------

Code

Here's the code that triggers the errors:

<?php

namespace App\Models;

/**
 * Class Fund
 *
 * @package App\Models
 * @mixin IdeHelperFund
 */
class Fund extends Model
{
    //...
}

This mixin is added through running the command php artisan ide-helper:models -M which is needed for the Laravel IDE-helper package which as the name suggests adds docblocks for the IDE to typehint magic methods of Laravel.

So I tried to solve this with Ignore errors with the following piece of code:

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

parameters:

    paths:
        - app

    # The level 8 is the highest level
    level: 5

    ignoreErrors:
        -
            message: '#PHPDoc tag @mixin contains unknown class App\\Models\\IdeHelper(.*)+\.#'
            path: Models/*

    excludePaths:

    checkMissingIterableValueType: false

But somehow the errors are not matched while testing the regex at Regexr work fine.

Any suggestions what I'm doing wrong? I've noticed in the ignore errors docs that they add # at the beginning and the end of the regex so I've added that to my phpstan configuration but I'm not sure if this is required, but without them I get the following error (so I left them in):

 -- ------------------------------------------------------------------------------------------------------------------------------------
     Error
 -- ------------------------------------------------------------------------------------------------------------------------------------
     Delimiter must not be alphanumeric or backslash in pattern: PHPDoc tag @mixin contains unknown class App\\Models\\IdeHelper(.*)+\.
 -- ------------------------------------------------------------------------------------------------------------------------------------

Additional information

I'm using the default larastan version 0.7.12 extension.

2 Answers 2

2

In order to have good regex you can generate baseline file with flag --generate-baseline A file will be generated with actuals errors.

You can copy pattern and add it to your neon configuration.

Don't forgot to delete baseline file before run again phpstan

Sign up to request clarification or add additional context in comments.

Comments

1

PHPStan is right here. IdeHelperFund class is not in App\Models namespace.

You need to let PHPStan know where is the file. You can do that by scanning extra files. Read about it here: https://phpstan.org/user-guide/discovering-symbols#third-party-code-outside-of-composer-dependencies

And as the maintainer of the Larastan, I suggest you to write all of your ide-helper stuff to files. Like _ide_helper.php, _ide_helper_models.php, .phpstorm.meta.php This mixin tags for the ide-helper can interfere with the analysis of PHPStan.

5 Comments

So I should add them to the various files and then remove the @mixin from the classes?
You don't need to manually add anything. You can run php artisan ide-helper:models -N and it will create a file that will help you with the auto-completion.
The problem with php artisan ide-helper:models -N and add it to my php stan config it reports errors like: Providers/TelescopeServiceProvider.php Reflection error: Eloquent not found. And therefore need to include _ide_helper.php, which then throws a lot of errors related to Illuminate\Http\Request like $request->input('...') and many others like $request->user()->can('...')
So far the only good result is when I write the ide helpers directly in the models through with php artisan ide-helper:models -W but that adds a lot of bloat to the beginning of my models which also is not preferred in our opinion.
You don't need to add the generated file to PHPStan config. The generated file is just for IDE auto-completion. PHPStan/Larastan already smart enough to analyse your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.