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

garrettmassey's avatar

PHPStan / LaraStan can't find magic methods and properties

There are a few instances in my Factories, Actions, and Middleware where LaraStan and PHPStan can't seem to find the magic methods or properties for certain things.

For instance, I use the spatie/laravel-onboard package ((laravel-onboarding)[https://github.com/spatie/laravel-onboard], and the middleware looks like this:

<?php

namespace App\Core\Middleware;

use Closure;

class RedirectToUnfinishedOnboardingStep
{
    public function handle($request, Closure $next)
    {
        if (auth()->user()->onboarding()->inProgress()) {
            $link = auth()->user()->onboarding()->nextUnfinishedStep()->link;
            return redirect()->to(
                $link['link']
            );
        }

        return $next($request);
    }
}

and Larastan shows this error:

 ------ ----------------------------------------------------------------------- 
 Line   Core/Middleware/RedirectToUnfinishedOnboardingStep.php                 
 ------ ----------------------------------------------------------------------- 
  12     Access to an undefined property Spatie\Onboard\OnboardingStep::$link.  
 ------ -----------------------------------------------------------------------

But I don't want to go in and change the underlying class because it's a package.

And with my factories, for example, I have this:

<?php

namespace App\Scopes\Locations\Factories\States;

use App\Scopes\Locations\Models\States\State;
use Illuminate\Database\Eloquent\Factories\Factory;

class StateFactory extends Factory
{
    protected $model = State::class;

    public function definition()
    {
        return [
            'name' => fake()->state,
            'code' => fake()->stateAbbr,
            'active' => true,
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }
}

and Larastan gives this error:

 ------ -------------------------------------------------------------- 
  Line   Scopes/Locations/Factories/States/StateFactory.php            
 ------ -------------------------------------------------------------- 
  15     Access to an undefined property Faker\Generator::$state.      
  16     Access to an undefined property Faker\Generator::$stateAbbr.  
 ------ --------------------------------------------------------------

I'm not sure how to get Larastan to understand these methods and properties. They're the only errors left in my Larastan results.

0 likes
2 replies
LaryAI's avatar
Level 58

One solution to this issue is to create stub files for the classes that Larastan is having trouble with. These stub files will provide the necessary information for Larastan to understand the magic methods and properties.

For example, to create a stub file for the Spatie\Onboard\OnboardingStep class, create a new file called OnboardingStep.php in the stubs directory of your project (if it doesn't exist, create it). In this file, define the missing property like this:

<?php

namespace Spatie\Onboard;

/**
 * @property mixed $link
 */
class OnboardingStep
{
    // ...
}

This tells Larastan that the OnboardingStep class has a magic property called $link.

Similarly, to create a stub file for the Faker\Generator class, create a new file called Generator.php in the stubs directory of your project. In this file, define the missing properties like this:

<?php

namespace Faker;

/**
 * @property-read string $state
 * @property-read string $stateAbbr
 */
class Generator
{
    // ...
}

This tells Larastan that the Generator class has magic properties called $state and $stateAbbr.

Once you've created these stub files, you need to tell Larastan to use them. To do this, add the following to your phpstan.neon file:

parameters:
    autoload_files:
        - /path/to/project/stubs/OnboardingStep.php
        - /path/to/project/stubs/Generator.php

Replace /path/to/project with the actual path to your project.

With these stub files in place, Larastan should be able to find the missing magic methods and properties and the errors should disappear.

2 likes
garrettmassey's avatar

@LaryAI instead of using autoload_files I used stubFiles and following the rest of the suggestions seems to have worked.

2 likes

Please or to participate in this conversation.