Skip to content

Commit

Permalink
Merge pull request #12 from calebdw/static_models
Browse files Browse the repository at this point in the history
fix: properly handle static called on type for query
  • Loading branch information
calebdw authored Sep 14, 2024
2 parents c5aee7b + 3c15b82 commit bbb36bc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/ReturnTypes/ModelDynamicStaticMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand Down Expand Up @@ -84,9 +86,15 @@ public function getTypeFromStaticMethodCall(

if (count(array_intersect([EloquentBuilder::class], $returnType->getReferencedClasses())) > 0) {
if ($methodCall->class instanceof Name) {
$type = $scope->resolveTypeByName($methodCall->class);

if ($type instanceof ThisType) {
$type = new StaticType($type->getClassReflection());
}

$returnType = new GenericObjectType(
$this->builderHelper->determineBuilderName($scope->resolveName($methodCall->class)),
[new ObjectType($scope->resolveName($methodCall->class))],
$this->builderHelper->determineBuilderName($type->getClassName()),
[$type],
);
} elseif ($methodCall->class instanceof Expr) {
$type = $scope->getType($methodCall->class);
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function dataIntegrationTests(): iterable
self::getContainer();

yield [__DIR__ . '/data/test-case-extension.php'];
yield [__DIR__ . '/data/model-builder.php'];
yield [__DIR__ . '/data/model-properties.php'];
yield [__DIR__ . '/data/blade-view.php'];
yield [__DIR__ . '/data/helpers.php'];
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/data/model-builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ModelBuilder;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
/** @return Builder<static> */
public static function testQueryStatic(): Builder
{
return static::query();
}

public static function testCreateStatic(): static
{
return static::query()->create();
}

public static function testCreateSelf(): static
{
return self::query()->create();
}
}
2 changes: 1 addition & 1 deletion tests/Type/data/eloquent-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class TestModel extends Model
public function test(): void
{
assertType('Illuminate\Database\Eloquent\Collection<int, EloquentBuilder\TestModel>', $this->where('email', 1)->get());
assertType('Illuminate\Database\Eloquent\Builder<EloquentBuilder\TestModel>', static::query()->where('email', 'bar'));
assertType('Illuminate\Database\Eloquent\Builder<static(EloquentBuilder\TestModel)>', static::query()->where('email', 'bar'));
assertType('Illuminate\Database\Eloquent\Builder<EloquentBuilder\TestModel>', $this->where('email', 'bar'));
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Type/data/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public function __construct(private User $user)
class Bar extends Model
{
use HasBar;

public function test(): void
{
assertType('Illuminate\Database\Eloquent\Builder<static(Model\Bar)>', self::query());
assertType('Illuminate\Database\Eloquent\Builder<static(Model\Bar)>', static::query());

assertType('static(Model\Bar)|null', self::query()->first());
assertType('static(Model\Bar)|null', static::query()->first());
}
}

trait HasBar
Expand Down

0 comments on commit bbb36bc

Please sign in to comment.