Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStan ignore errors regex

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.

like image 312
Reflexecute Avatar asked Feb 01 '26 07:02

Reflexecute


2 Answers

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

like image 137
Antoine Lamirault Avatar answered Feb 02 '26 21:02

Antoine Lamirault


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.

like image 23
Can Vural Avatar answered Feb 02 '26 22:02

Can Vural



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!