Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Methods/ModelFactoryMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;

use function array_key_exists;
Expand Down Expand Up @@ -116,7 +116,7 @@ public function getPrototype(): ClassMemberReflection
/** @return ParametersAcceptor[] */
public function getVariants(): array
{
$returnType = new ObjectType($this->classReflection->getName());
$returnType = new StaticType($this->classReflection);
$stateParameter = $this->classReflection->getMethod('state', new OutOfClassScope())->getVariants()[0]->getParameters()[0];
$countParameter = $this->classReflection->getMethod('count', new OutOfClassScope())->getVariants()[0]->getParameters()[0];

Expand Down
31 changes: 29 additions & 2 deletions tests/Type/data/model-factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use App\Post;
use App\User;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

use function PHPStan\Testing\assertType;

function test(?int $foo): void {
Expand Down Expand Up @@ -94,6 +94,12 @@ class Comment extends Model
{
use HasFactory;

/** @return BelongsTo<User> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/** @return CommentFactory */
protected static function newFactory(): Factory
{
Expand All @@ -113,4 +119,25 @@ public function definition(): array
{
return [];
}

public function foo(): static
{
return $this->state(['foo' => 'bar']);
}

private function test(User $user): void
{
$type = 'static(ModelFactories\CommentFactory)';
assertType($type, $this->state([]));
assertType($type, $this->afterMaking(fn ($c) => $c));
assertType($type, $this->afterCreating(fn ($c) => $c));
assertType($type, $this->foo());
assertType($type, $this->recycle($user));
assertType($type, $this->has(User::factory()));
assertType($type, $this->hasAttached(User::factory()));
assertType($type, $this->for(User::factory()));
assertType($type, $this->for($user));
assertType($type, $this->forUser());
assertType($type, $this->hasUser());
}
}