You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When Laravel code calls the factory static function provided by the Illuminate\Database\Eloquent\Factories\HasFactory trait on a model an internal error is thrown.
This circumstance only happens when a model has both a legacy factory and a new factory. This can arise when legacy codebases that started on older versions of Laravel as the laravel/legacy-factories were introduced to help migrate codebases that referenced deprecated factories in a large volume of locations.
Steps to reproduce
The PR/repository provided above contains a code base that can be used to create the issue
Here are the code steps to reproduce the bug. Assuming you already have a codebase installed with Laravel + Larastan
Install laravel/legacy-factories
Create a new model
Create a legacy factory for the model
Create a regular factory for the model
Add the Illuminate\Database\Eloquent\Factories\HasFactory trait to the model
Create a static function in the model called newFactory which returns the newly created regular factory
Call the static function factory on the model in another PHP file which will be analysed by phpstan
Run phpstan
Laravel code where the issue was found
<?phpdeclare(strict_types=1);
namespaceDatabase\Factories\User;
useApp\Models\Post;
useApp\User;
useIlluminate\Database\Eloquent\Factories\Factory;
useIlluminate\Support\Str;
classPostFactoryextendsFactory
{
protected$models = Post::class;
publicfunctiondefinition(): array
{
return [
'user_id' => User::factory(), # If this line is commented out, then phpstan will not error.
];
}
}
Ah, I found the issue---Larastan does not actually take into account the newFactory method and only looks for factories that match the expected pattern:
Given this is an edge case since it only happens when a model has both a legacy factory and a new factory, also nobody else reported it, I think we will not fix it for now. Thank you!
Versions
Larastan Version: 2.8.0
Laravel Version: 10.39.0
PHP Version: 8.2.13
Legacy Factories Version: 1.3.2
--level
used: 8Repository: https://github.com/jamiewood678/larastan-legacy-factories-exception
Pull request with failing analyse: Recreated Internal Error jamiewood678/larastan-legacy-factories-exception#1
Other Notes
Description
When Laravel code calls the
factory
static function provided by theIlluminate\Database\Eloquent\Factories\HasFactory
trait on a model an internal error is thrown.This circumstance only happens when a model has both a legacy factory and a new factory. This can arise when legacy codebases that started on older versions of Laravel as the
laravel/legacy-factories
were introduced to help migrate codebases that referenced deprecated factories in a large volume of locations.Steps to reproduce
The PR/repository provided above contains a code base that can be used to create the issue
Here are the code steps to reproduce the bug. Assuming you already have a codebase installed with Laravel + Larastan
laravel/legacy-factories
Illuminate\Database\Eloquent\Factories\HasFactory
trait to the modelnewFactory
which returns the newly created regular factoryfactory
on the model in another PHP file which will be analysed by phpstanLaravel code where the issue was found
Internal Error Thrown
The text was updated successfully, but these errors were encountered: