Skip to content

Commit

Permalink
Merge pull request #339 from lptn/fix-tests
Browse files Browse the repository at this point in the history
Stubs for Eloquent Builder: Use `$this` instead of `self<TModel>`
  • Loading branch information
lptn authored Jul 13, 2023
2 parents a91791e + 3df84b9 commit 61a1ca4
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 156 deletions.
7 changes: 7 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
<code>null</code>
</TypeDoesNotContainType>
</file>
<file src="src/Handlers/Eloquent/ModelPropertyAccessorHandler.php">
<LessSpecificImplementedReturnType>
<code>array</code>
<code>array</code>
<code>array</code>
</LessSpecificImplementedReturnType>
</file>
</files>
3 changes: 1 addition & 2 deletions src/Fakes/FakeModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function get_class;
use function is_a;
use function in_array;
use function is_string;
use function implode;

/** @psalm-suppress PropertyNotSetInConstructor */
Expand Down Expand Up @@ -128,7 +127,7 @@ public function getPropertiesFromTable($model): void
if ($this->write_model_magic_where) {
$this->setMethod(
Str::camel("where_" . $column_name),
'\Illuminate\Database\Eloquent\Builder|\\' . get_class($model),
'\Illuminate\Database\Eloquent\Builder<static>', // @todo support custom EloquentBuilders
array('$value')
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Handlers/Eloquent/ModelMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@

final class ModelMethodHandler implements MethodReturnTypeProviderInterface, AfterClassLikeVisitInterface
{
/**
* @return array<string>
*/
/** @inheritDoc */
public static function getClassLikeNames(): array
{
return [Model::class];
}

/** @inheritDoc */
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
{
$source = $event->getSource();
Expand Down
5 changes: 2 additions & 3 deletions src/Handlers/Eloquent/ModelPropertyAccessorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

final class ModelPropertyAccessorHandler implements PropertyExistenceProviderInterface, PropertyVisibilityProviderInterface, PropertyTypeProviderInterface
{
/**
* @return list<class-string<\Illuminate\Database\Eloquent\Model>>
*/
/** @inheritDoc */
public static function getClassLikeNames(): array
{
return ModelStubProvider::getModelClasses();
}

/** @inheritDoc */
public static function doesPropertyExist(PropertyExistenceProviderEvent $event): ?bool
{
$source = $event->getSource();
Expand Down
1 change: 1 addition & 0 deletions src/Handlers/Eloquent/ModelRelationshipPropertyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function getClassLikeNames(): array
return ModelStubProvider::getModelClasses();
}

/** @inheritDoc */
public static function doesPropertyExist(PropertyExistenceProviderEvent $event): ?bool
{
$source = $event->getSource();
Expand Down
2 changes: 2 additions & 0 deletions src/Handlers/Helpers/CacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

final class CacheHandler implements FunctionReturnTypeProviderInterface
{
/** @inheritDoc */
public static function getFunctionIds(): array
{
return ['cache'];
}

/** @inheritDoc */
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Type\Union
{
$call_args = $event->getCallArgs();
Expand Down
2 changes: 2 additions & 0 deletions src/Handlers/Helpers/PathHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function getFunctionIds(): array
];
}

/** @inheritDoc */
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
$function_id = $event->getFunctionId();
Expand All @@ -51,6 +52,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
});
}

/** @inheritDoc */
public static function getClassLikeNames(): array
{
return [
Expand Down
2 changes: 2 additions & 0 deletions src/Handlers/Helpers/TransHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

class TransHandler implements FunctionReturnTypeProviderInterface
{
/** @inheritDoc */
public static function getFunctionIds(): array
{
return ['trans'];
}

/** @inheritDoc */
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Type\Union
{
$call_args = $event->getCallArgs();
Expand Down
1 change: 1 addition & 0 deletions src/Providers/ApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function bootApp(): void
if ($app instanceof LaravelApplication) {
/** @var \Illuminate\Contracts\Console\Kernel $consoleApp */
$consoleApp = $app->make(Kernel::class);
// @todo do not bootstrap \Illuminate\Foundation\Bootstrap\HandleExceptions
$consoleApp->bootstrap();
} else { // LumenApplication
$app->boot();
Expand Down
2 changes: 1 addition & 1 deletion stubs/Database/BelongsToMany.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class BelongsToMany extends Relation
* Execute the query and get the first result or throw an exception.
*
* @param list<non-empty-string> $columns
* @return \Illuminate\Database\Eloquent\Model|static
* @return \Illuminate\Database\Eloquent\Model
* @psalm-return TRelatedModel
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Eloquent;

use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Concerns\BuildsQueries;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Traits\ForwardsCalls;
Expand All @@ -12,7 +13,7 @@ use Illuminate\Support\Traits\ForwardsCalls;
*
* @mixin \Illuminate\Database\Query\Builder
*/
class Builder
class Builder implements BuilderContract
{
use BuildsQueries, Concerns\QueriesRelationships, ForwardsCalls;

Expand All @@ -31,13 +32,13 @@ class Builder
/**
* @param string $identifier
* @param \Illuminate\Database\Eloquent\Scope|\Closure $scope
* @return self<TModel>
* @return $this
*/
public function withGlobalScope($identifier, $scope) {}

/**
* @param \Illuminate\Database\Eloquent\Scope|string $scope
* @return self<TModel>
* @return $this
*/
public function withoutGlobalScope($scope) {}

Expand All @@ -54,33 +55,37 @@ class Builder

/**
* @param array|null $scopes
* @return self<TModel>
* @return $this
*/
public function withoutGlobalScopes(array $scopes = null) {}

/**
* @return array
* Get an array of global scopes that were removed from the query.
*
* @return list<non-empty-string>
*/
public function removedScopes() {}

/**
* @param mixed $id
* @return self<TModel>
* @return $this
*/
public function whereKey($id) {}

/**
* @param mixed $id
* @return self<TModel>
* @return $this
*/
public function whereKeyNot($id) {}

/**
* @param string|array|\Closure $column
* Add a basic where clause to the query.
*
* @param (\Closure(static): void)|(\Closure(static): static)|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return self<TModel>
* @return $this
*/
public function where($column, $operator = null, $value = null, $boolean = 'and') {}

Expand All @@ -89,15 +94,15 @@ class Builder
* @param string $operator
* @param \DateTimeInterface|string|null $value
* @param string $boolean
* @return self<TModel>
* @return $this
*/
public function whereDate($column, $operator, $value = null, $boolean = 'and') {}

/**
* @param \Closure|array|string $column
* @param mixed $operator
* @param mixed $value
* @return self<TModel>
* @return $this
*/
public function orWhere($column, $operator = null, $value = null) {}

Expand All @@ -106,7 +111,7 @@ class Builder
* @param \Closure|null $callback
* @param string $operator
* @param int $count
* @return self<TModel>
* @return $this
*/
public function whereHas($relation, \Closure $callback = null, $operator = '>=', $count = 1) {}

Expand All @@ -115,7 +120,7 @@ class Builder
* @param \Closure|string|array<mixed>|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return self<TModel>
* @return $this
*/
public function whereRelation($relation, $column, $operator = null, $value = null) {}

Expand All @@ -131,7 +136,7 @@ class Builder
* @param \Closure|string|array<int, string>|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return self<TModel>
* @return $this
*/
public function orWhereRelation($relation, $column, $operator = null, $value = null) {}

Expand All @@ -140,20 +145,20 @@ class Builder
* @param \Closure|null $callback
* @param string $operator
* @param int $count
* @return self<TModel>
* @return $this
*/
public function orWhereHas($relation, \Closure $callback = null, $operator = '>=', $count = 1) {}

/**
* @template TRelatedModel of Model
* @template TChildModel of Model
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TChildModel of \Illuminate\Database\Eloquent\Model
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, TChildModel>|string $relation
* @param string|array<string> $types
* @param string $operator
* @param int $count
* @param string $boolean
* @param \Closure|null $callback
* @return self<TModel>
* @return $this
*/
public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boolean = 'and', \Closure $callback = null) {}

Expand Down Expand Up @@ -380,13 +385,13 @@ class Builder
public function getModels($columns = ['*']) {}

/**
* @template TRelatedModel of Model
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @param \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel>|string $relation
* @param string $operator
* @param int $count
* @param string $boolean
* @param \Closure|null $callback
* @return static
* @return $this
*
* @throws \RuntimeException
*/
Expand All @@ -396,27 +401,27 @@ class Builder
* @param string $relation
* @param string $operator
* @param int $count
* @return static
* @return $this
*/
public function orHas($relation, $operator = '>=', $count = 1) {}

/**
* @param string $relation
* @param string $boolean
* @param \Closure|null $callback
* @return static
* @return $this
*/
public function doesntHave($relation, $boolean = 'and', \Closure $callback = null) {}

/**
* @param string $relation
* @return static
* @return $this
*/
public function orDoesntHave($relation) {}

/**
* @param array $models
* @return array
* @param array<array-key, \Illuminate\Database\Eloquent\Model> $models
* @return array<array-key, \Illuminate\Database\Eloquent\Model>
*/
public function eagerLoadRelations(array $models) {}

Expand All @@ -427,7 +432,9 @@ class Builder
public function getRelation($name) {}

/**
* @return \Generator
* Get a lazy collection for the given query.
*
* @return \Illuminate\Support\LazyCollection
*/
public function cursor() {}

Expand Down
File renamed without changes.
16 changes: 4 additions & 12 deletions tests/Acceptance/acceptance/CollectionTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ Feature: Collection types
"""
final class CollectionTypes
{
/**
* @return Collection<int, string>
*/
/** @return Collection<int, string> */
public function getCollection(): Collection
{
return new Collection(['hi']);
Expand Down Expand Up @@ -68,9 +66,7 @@ Feature: Collection types
});
}
/**
* @return Collection<string, int>
*/
/** @return Collection<string, int> */
public function testFlip(): Collection
{
return $this->getCollection()->flip();
Expand Down Expand Up @@ -117,9 +113,7 @@ Feature: Collection types
});
}
/**
* @return int|false
*/
/** @return int|false */
public function searchUsingClosureTest()
{
return $this->getCollection()->search(function (string $item) {
Expand All @@ -143,9 +137,7 @@ Feature: Collection types
return $this->getCollection()->shift(2);
}
/**
* @return array<int, string>
*/
/** @return array<int, string> */
public function allTest(): array
{
return $this->getCollection()->all();
Expand Down
4 changes: 1 addition & 3 deletions tests/Acceptance/acceptance/DatabaseBuilderTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ Feature: Database Builder types
final class UserRepository
{
/**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder
*/
/** @param \Illuminate\Database\Eloquent\Builder<User> $builder */
public function firstFromDatabaseBuilderInstance(\Illuminate\Database\Eloquent\Builder $builder): ?User {
return $builder->first();
}
Expand Down
Loading

0 comments on commit 61a1ca4

Please sign in to comment.