Skip to content

Commit

Permalink
fix: use class name to construct new ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Jul 1, 2022
1 parent fbb8313 commit ab0cca9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;

Expand Down Expand Up @@ -46,6 +47,6 @@ public function getTypeFromMethodCall(

$builderName = $this->builderHelper->determineBuilderName($calledOnType->getClassName());

return new GenericObjectType($builderName, [$calledOnType]);
return new GenericObjectType($builderName, [new ObjectType($calledOnType->getClassName())]);
}
}
17 changes: 17 additions & 0 deletions tests/Type/data/eloquent-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\PostBuilder;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use function PHPStan\Testing\assertType;

User::query()->has('accounts', '=', 1, 'and', function (Builder $query) {
Expand Down Expand Up @@ -160,3 +161,19 @@ function doFoo(User $user, Post $post): void
assertType('App\PostBuilder<App\Post>', $post->newQueryWithoutScope('foo'));
assertType('App\PostBuilder<App\Post>', $post->newQueryForRestoration([1]));
};

class Foo extends Model
{
/** @phpstan-use FooTrait<Foo> */
use FooTrait;
}

/** @template TModelClass of Model */
trait FooTrait
{
/** @return Builder<TModelClass> */
public function doFoo(): Builder
{
return $this->newQuery();
}
}

0 comments on commit ab0cca9

Please sign in to comment.