Skip to content

Commit

Permalink
Merge pull request #276 from lptn/pagination-stubs
Browse files Browse the repository at this point in the history
Add Pagination stubs
  • Loading branch information
lptn authored Jan 22, 2023
2 parents 1186dc7 + 2ef41a1 commit 722b929
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 59 deletions.
5 changes: 4 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement

protected function getCommonStubs(): array
{
return glob(dirname(__DIR__) . '/stubs/*.stubphp');
return array_merge(
glob(dirname(__DIR__) . '/stubs/Contracts/*.stubphp'),
glob(dirname(__DIR__) . '/stubs/*.stubphp')
);
}

protected function getStubsForVersion(string $version): array
Expand Down
83 changes: 40 additions & 43 deletions stubs/BelongsToMany.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Illuminate\Database\Eloquent\Relations;

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

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template-extends Relation<TRelatedModel>
Expand All @@ -14,32 +11,6 @@ class BelongsToMany extends Relation
{
use Concerns\InteractsWithPivotTable;

/**
* Create a new belongs to many relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $table
* @param string $foreignPivotKey
* @param string $relatedPivotKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relationName
* @return void
*/
public function __construct(Builder $query, Model $parent, $table, $foreignPivotKey,
$relatedPivotKey, $parentKey, $relatedKey, $relationName = null)
{
$this->parentKey = $parentKey;
$this->relatedKey = $relatedKey;
$this->relationName = $relationName;
$this->relatedPivotKey = $relatedPivotKey;
$this->foreignPivotKey = $foreignPivotKey;
$this->table = $this->resolveTableName($table);

parent::__construct($query, $parent);
}

/**
* @template T
* @psalm-param T $id
Expand Down Expand Up @@ -71,8 +42,7 @@ class BelongsToMany extends Relation
* @param array $values
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @psalm-return TRelatedModel
* @return TRelatedModel
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true) {}

Expand All @@ -87,7 +57,7 @@ class BelongsToMany extends Relation
public function find($id, $columns = ['*']) {}

/**
* @param mixed $ids
* @param \Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<array-key, int|string> $ids
* @param list<non-empty-string> $columns
* @return \Illuminate\Database\Eloquent\Collection
* @psalm-return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
Expand Down Expand Up @@ -123,27 +93,54 @@ class BelongsToMany extends Relation
*/
public function firstOrFail($columns = ['*']) {}

/**
* @param list<non-empty-string> $columns
* @return \Illuminate\Database\Eloquent\Collection
* @psalm-return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
*/
public function get($columns = ['*']) {}

/**
* @param array<string, mixed> $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @psalm-return TRelatedModel
* @return TRelatedModel
*/
public function create(array $attributes = [], array $joining = [], $touch = true) {}

/**
* @param array $records
* @param array $joinings
* @return array
* @psalm-return list<TRelatedModel>
* @return list<TRelatedModel>
*/
public function createMany(array $records, array $joinings = []) {}

/**
* @return \Traversable<int, TRelatedModel>
*/
public function getResults() {}

/**
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Pagination\LengthAwarePaginator<TRelatedModel>
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) {}

/**
* Paginate the given query into a simple paginator.
*
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Pagination\Paginator<TRelatedModel>
*/
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) {}

/**
* Paginate the given query into a cursor paginator.
*
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $cursorName
* @param string|null $cursor
* @return \Illuminate\Pagination\CursorPaginator<TRelatedModel>
*/
public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null) {}
}
33 changes: 33 additions & 0 deletions stubs/Contracts/Pagination.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Contracts\Pagination;

/**
* @template TItem
*/
interface Paginator
{
/**
* @return array<TItem>
*/
public function items(): array;
}

/**
* @template TItem
*
* @extends Paginator<TItem>
*/
interface LengthAwarePaginator extends Paginator
{}

/**
* @template TItem
*/
interface CursorPaginator
{
/**
* @return array<TItem>
*/
public function items(): array;
}
57 changes: 43 additions & 14 deletions stubs/HasManyThrough.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use Illuminate\Database\Eloquent\Model;
class HasManyThrough extends Relation
{
/**
* Create a new has many through relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $farParent
* @param \Illuminate\Database\Eloquent\Model $throughParent
Expand All @@ -36,6 +34,46 @@ class HasManyThrough extends Relation
parent::__construct($query, $throughParent);
}

/**
* Get the results of the relationship.
*
* @psalm-return \Traversable<int, TRelatedModel>
*/
public function getResults() {}

/**
* Get a paginator for the "select" statement.
*
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Pagination\LengthAwarePaginator<TRelatedModel>
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) {}

/**
* Paginate the given query into a simple paginator.
*
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Pagination\Paginator<TRelatedModel>
*/
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) {}

/**
* Paginate the given query into a cursor paginator.
*
* @param int|null $perPage
* @param array<int, mixed> $columns
* @param string $cursorName
* @param string|null $cursor
* @return \Illuminate\Pagination\CursorPaginator<TRelatedModel>
*/
public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null) {}

/**
* Get the first related model record matching the attributes or instantiate it.
*
Expand Down Expand Up @@ -87,16 +125,6 @@ class HasManyThrough extends Relation
*/
public function find($id, $columns = ['*']) {}

/**
* Find multiple related models by their primary keys.
*
* @param mixed $ids
* @param list<non-empty-string> $columns
* @return \Illuminate\Database\Eloquent\Collection
* @psalm-return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
*/
public function findMany($ids, $columns = ['*']) {}

/**
* Find a related model by its primary key or throw an exception.
* @template T
Expand All @@ -111,11 +139,12 @@ class HasManyThrough extends Relation
public function findOrFail($id, $columns = ['*']) {}

/**
* Execute the query as a "select" statement.
* Find multiple related models by their primary keys.
*
* @param mixed $ids
* @param list<non-empty-string> $columns
* @return \Illuminate\Database\Eloquent\Collection
* @psalm-return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
*/
public function get($columns = ['*']) {}
public function findMany($ids, $columns = ['*']) {}
}
122 changes: 122 additions & 0 deletions stubs/Pagination.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Illuminate\Pagination;

/**
* @template TValue
*
* @mixin \Illuminate\Support\Collection<array-key, TValue>
*/
abstract class AbstractPaginator implements \Illuminate\Contracts\Support\Htmlable
{
/**
* @return array<TValue>
*/
public function items(): array {}

/**
* @return \Illuminate\Support\Collection<array-key, TValue>
*/
public function getCollection(): \Illuminate\Support\Collection {}

/**
* @return \ArrayIterator<array-key, TValue>
*/
public function getIterator(): \Traversable {}

public function offsetExists(mixed $offset): bool {}

/**
* @return TValue|null
*/
public function offsetGet(mixed $offset): mixed {}

/**
* @param TValue $value
*/
public function offsetSet(mixed $offset, $value): void {}

public function offsetUnset(mixed $offset): void {}
}

/**
* @template TValue
*
* @implements \ArrayAccess<array-key, TValue>
* @implements \IteratorAggregate<array-key, TValue>
* @implements \Illuminate\Contracts\Support\Arrayable<array-key, TValue>
* @implements \Illuminate\Contracts\Pagination\Paginator<TValue>
*
* @extends AbstractPaginator<TValue>
*/
class Paginator extends AbstractPaginator implements \Illuminate\Contracts\Support\Arrayable, \ArrayAccess, \Countable, \IteratorAggregate, \Illuminate\Contracts\Support\Jsonable, \JsonSerializable, \Illuminate\Contracts\Pagination\Paginator
{}

/**
* @template TValue
*
* @implements \ArrayAccess<array-key, TValue>
* @implements \IteratorAggregate<array-key, TValue>
* @implements \Illuminate\Contracts\Support\Arrayable<array-key, TValue>
* @implements \Illuminate\Contracts\Pagination\LengthAwarePaginator<TValue>
*
* @extends AbstractPaginator<TValue>
*/
class LengthAwarePaginator extends AbstractPaginator implements \Illuminate\Contracts\Support\Arrayable, \ArrayAccess, \Countable, \IteratorAggregate, \Illuminate\Contracts\Support\Jsonable, \JsonSerializable, \Illuminate\Contracts\Pagination\LengthAwarePaginator
{}

/**
* @template TValue
*
* @mixin \Illuminate\Support\Collection<mixed, TValue>
*/
abstract class AbstractCursorPaginator implements \Illuminate\Contracts\Support\Htmlable
{
/**
* @return array<TValue>
*/
public function items(): array {}

/**
* @return \Illuminate\Support\Collection<array-key, TValue>
*/
public function getCollection(): \Illuminate\Support\Collection {}

/**
* @return \ArrayIterator<array-key, TValue>
*/
public function getIterator(): \Traversable {}

public function offsetExists(mixed $offset): bool {}

/**
* @return TValue|null
*/
public function offsetGet(mixed $offset): mixed {}

/**
* @param TValue $value
*/
public function offsetSet(mixed $offset, $value): void {}

public function offsetUnset(mixed $offset): void {}
}

/**
* @template TValue
*
* @implements \ArrayAccess<array-key, TValue>
* @implements \IteratorAggregate<array-key, TValue>
* @implements \Illuminate\Contracts\Support\Arrayable<array-key, TValue>
* @implements \Illuminate\Contracts\Pagination\CursorPaginator<TValue>
*
* @extends AbstractCursorPaginator<TValue>
*/
class CursorPaginator extends AbstractCursorPaginator implements \Illuminate\Contracts\Support\Arrayable, \ArrayAccess, \Countable, \IteratorAggregate, \Illuminate\Contracts\Support\Jsonable, \JsonSerializable, \Illuminate\Contracts\Pagination\CursorPaginator
{}

/**
* @implements \Illuminate\Contracts\Support\Arrayable<array-key, mixed>
*/
class Cursor implements \Illuminate\Contracts\Support\Arrayable
{}
2 changes: 1 addition & 1 deletion stubs/helpers.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function rescue(callable $callback, $rescue = null, $report = true) {}
* @param callable(int): TValue $callback
* @param int<0, max> $sleep
* @param null|callable(\Exception): bool $when
* @phpstan-return TValue
* @psalm-return TValue
*
* @throws \Exception
*/
Expand Down
Loading

0 comments on commit 722b929

Please sign in to comment.