Skip to content

Commit bbb36bc

Browse files
authored
Merge pull request #12 from calebdw/static_models
fix: properly handle static called on type for query
2 parents c5aee7b + 3c15b82 commit bbb36bc

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

src/ReturnTypes/ModelDynamicStaticMethodReturnTypeExtension.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
2323
use PHPStan\Type\Generic\GenericObjectType;
2424
use PHPStan\Type\ObjectType;
25+
use PHPStan\Type\StaticType;
26+
use PHPStan\Type\ThisType;
2527
use PHPStan\Type\Type;
2628
use PHPStan\Type\TypeCombinator;
2729

@@ -84,9 +86,15 @@ public function getTypeFromStaticMethodCall(
8486

8587
if (count(array_intersect([EloquentBuilder::class], $returnType->getReferencedClasses())) > 0) {
8688
if ($methodCall->class instanceof Name) {
89+
$type = $scope->resolveTypeByName($methodCall->class);
90+
91+
if ($type instanceof ThisType) {
92+
$type = new StaticType($type->getClassReflection());
93+
}
94+
8795
$returnType = new GenericObjectType(
88-
$this->builderHelper->determineBuilderName($scope->resolveName($methodCall->class)),
89-
[new ObjectType($scope->resolveName($methodCall->class))],
96+
$this->builderHelper->determineBuilderName($type->getClassName()),
97+
[$type],
9098
);
9199
} elseif ($methodCall->class instanceof Expr) {
92100
$type = $scope->getType($methodCall->class);

tests/Integration/IntegrationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function dataIntegrationTests(): iterable
2222
self::getContainer();
2323

2424
yield [__DIR__ . '/data/test-case-extension.php'];
25+
yield [__DIR__ . '/data/model-builder.php'];
2526
yield [__DIR__ . '/data/model-properties.php'];
2627
yield [__DIR__ . '/data/blade-view.php'];
2728
yield [__DIR__ . '/data/helpers.php'];
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ModelBuilder;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class User extends Model
9+
{
10+
/** @return Builder<static> */
11+
public static function testQueryStatic(): Builder
12+
{
13+
return static::query();
14+
}
15+
16+
public static function testCreateStatic(): static
17+
{
18+
return static::query()->create();
19+
}
20+
21+
public static function testCreateSelf(): static
22+
{
23+
return self::query()->create();
24+
}
25+
}

tests/Type/data/eloquent-builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class TestModel extends Model
353353
public function test(): void
354354
{
355355
assertType('Illuminate\Database\Eloquent\Collection<int, EloquentBuilder\TestModel>', $this->where('email', 1)->get());
356-
assertType('Illuminate\Database\Eloquent\Builder<EloquentBuilder\TestModel>', static::query()->where('email', 'bar'));
356+
assertType('Illuminate\Database\Eloquent\Builder<static(EloquentBuilder\TestModel)>', static::query()->where('email', 'bar'));
357357
assertType('Illuminate\Database\Eloquent\Builder<EloquentBuilder\TestModel>', $this->where('email', 'bar'));
358358
}
359359
}

tests/Type/data/model.php

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public function __construct(private User $user)
2424
class Bar extends Model
2525
{
2626
use HasBar;
27+
28+
public function test(): void
29+
{
30+
assertType('Illuminate\Database\Eloquent\Builder<static(Model\Bar)>', self::query());
31+
assertType('Illuminate\Database\Eloquent\Builder<static(Model\Bar)>', static::query());
32+
33+
assertType('static(Model\Bar)|null', self::query()->first());
34+
assertType('static(Model\Bar)|null', static::query()->first());
35+
}
2736
}
2837

2938
trait HasBar

0 commit comments

Comments
 (0)