Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'psalm.xml'
- 'phpstan.neon'

push:
branches: ['master']
Expand All @@ -16,7 +16,7 @@ on:
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'psalm.xml'
- 'phpstan.neon'

name: mutation test

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ permissions:
contents: read

jobs:
psalm:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
phpstan:
uses: yiisoft/actions/.github/workflows/phpstan.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3', '8.4']
['8.1', '8.2', '8.3', '8.4', '8.5']
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## 2.0.1 under development

- Chg #259: Replace Psalm with PHPStan (@vjik)
- Enh #246: Explicitly import functions and constants in "use" section (@mspirkov)
- Enh #259: Remove redundant `instanceof LimitableDataInterface` check in `OffsetPaginator::isSortable()` (@vjik)

## 2.0.0 December 13, 2025

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
[![Code Coverage](https://codecov.io/gh/yiisoft/data/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/data)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdata%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/data/master)
[![static analysis](https://github.com/yiisoft/data/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/data/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/data/coverage.svg)](https://shepherd.dev/github/yiisoft/data)
[![psalm-level](https://shepherd.dev/github/yiisoft/data/level.svg)](https://shepherd.dev/github/yiisoft/data)

The package provides generic data abstractions. The aim is to hide a storage aspect from the operations of reading,
writing and processing data.
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"ext-ctype": "*",
"bamarni/composer-bin-plugin": "^1.8.3",
"friendsofphp/php-cs-fixer": "^3.95.27",
"phpstan/phpstan": "^2.3",
"phpunit/phpunit": "^10.5.60",
"rector/rector": "^2.7.0",
"shipmonk/composer-dependency-analyser": "^1.8",
Expand Down Expand Up @@ -74,6 +75,7 @@
"test-watch": "phpunit-watcher watch",
"cs-fix": "php-cs-fixer fix",
"rector": "rector",
"phpstan": "phpstan",
"mutation": "infection --threads=max --min-covered-msi=100",
"dependency-analyser": "composer-dependency-analyser"
}
Expand Down
4 changes: 2 additions & 2 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ The package tests are checked with [Infection](https://infection.github.io/) mut

## Static analysis

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:
The code is statically analyzed with [PHPStan](https://phpstan.org/). To run static analysis:

```shell
./vendor/bin/psalm
./vendor/bin/phpstan
```

## Code style
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: max
treatPhpDocTypesAsCertain: false
paths:
- src
22 changes: 0 additions & 22 deletions psalm.xml

This file was deleted.

54 changes: 26 additions & 28 deletions src/Paginator/KeysetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,24 @@
*
* @link https://use-the-index-luke.com/no-offset
*
* @template TKey as array-key
* @template TValue as array|object
* @template TKey of array-key
* @template TValue of array|object
*
* @implements PaginatorInterface<TKey, TValue>
*
* @psalm-type FilterCallback = Closure(GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual,KeysetFilterContext):FilterInterface
* @phpstan-type FilterCallback = Closure(GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual,KeysetFilterContext):FilterInterface
*/
final class KeysetPaginator implements PaginatorInterface
{
/**
* Data reader being paginated.
*
* @psalm-var ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface
* @var ReadableDataInterface<TKey, TValue>&LimitableDataInterface<TKey, TValue>&FilterableDataInterface<TKey, TValue>&SortableDataInterface<TKey, TValue>
*/
private ReadableDataInterface $dataReader;

/**
* @var int Maximum number of items per page.
* @psalm-var positive-int
* @var positive-int Maximum number of items per page.
*/
private int $pageSize = self::DEFAULT_PAGE_SIZE;
private ?PageToken $token = null;
Expand All @@ -81,22 +80,20 @@ final class KeysetPaginator implements PaginatorInterface
private bool $hasNextPage = false;

/**
* @psalm-var FilterCallback|null
* @var FilterCallback|null
*/
private ?Closure $filterCallback = null;

/**
* Reader cache against repeated scans.
* See more {@see __clone()} and {@see initialize()}.
*
* @psalm-var null|array<TKey, TValue>
* @var null|array<TKey, TValue>
*/
private ?array $readCache = null;

/**
* @param ReadableDataInterface $dataReader Data reader being paginated.
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader
* @psalm-suppress DocblockTypeContradiction Needed to allow validating `$dataReader`
* @param ReadableDataInterface<TKey, TValue> $dataReader Data reader being paginated.
*/
public function __construct(ReadableDataInterface $dataReader)
{
Expand Down Expand Up @@ -151,7 +148,6 @@ public function getToken(): ?PageToken

public function withPageSize(int $pageSize): static
{
/** @psalm-suppress DocblockTypeContradiction We don't believe in psalm types */
if ($pageSize < 1) {
throw new InvalidArgumentException('Page size should be at least 1.');
}
Expand All @@ -164,14 +160,16 @@ public function withPageSize(int $pageSize): static
/**
* Returns a new instance with defined closure for preparing data reader filters.
*
* @psalm-param FilterCallback|null $callback Closure with signature:
* @param FilterCallback|null $callback Closure with signature:
*
* ```php
* function(
* GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual $filter,
* KeysetFilterContext $context
* ): FilterInterface
* ```
*
* @return static New instance.
*/
public function withFilterCallback(?Closure $callback): self
{
Expand Down Expand Up @@ -322,7 +320,7 @@ public function isPaginationRequired(): bool
}

/**
* @psalm-assert array<TKey, TValue> $this->readCache
* @phpstan-assert array<TKey, TValue> $this->readCache
*/
private function initialize(): void
{
Expand All @@ -341,20 +339,23 @@ private function initialize(): void
}

/**
* @psalm-param ReadableDataInterface<TKey, TValue> $dataReader
* @psalm-return array<TKey, TValue>
* @param ReadableDataInterface<TKey, TValue> $dataReader
*
* @return array<TKey, TValue>
*/
private function readData(ReadableDataInterface $dataReader, Sort $sort): array
{
$data = [];
[$field] = $this->getFieldAndSortingFromSort($sort);

foreach ($dataReader->read() as $key => $item) {
/** @phpstan-ignore cast.string */
$this->currentFirstValue ??= (string) ArrayHelper::getValue($item, $field);

if (count($data) === $this->pageSize) {
$this->hasNextPage = true;
} else {
/** @phpstan-ignore cast.string */
$this->currentLastValue = (string) ArrayHelper::getValue($item, $field);
$data[$key] = $item;
}
Expand All @@ -364,8 +365,9 @@ private function readData(ReadableDataInterface $dataReader, Sort $sort): array
}

/**
* @psalm-param array<TKey, TValue> $data
* @psalm-return array<TKey, TValue>
* @param array<TKey, TValue> $data
*
* @return array<TKey, TValue>
*/
private function reverseData(array $data): array
{
Expand All @@ -375,7 +377,7 @@ private function reverseData(array $data): array
}

/**
* @psalm-param ReadableDataInterface<TKey, TValue>&LimitableDataInterface&FilterableDataInterface&SortableDataInterface $dataReader
* @param ReadableDataInterface<TKey, TValue>&LimitableDataInterface<TKey, TValue>&FilterableDataInterface<TKey, TValue>&SortableDataInterface<TKey, TValue> $dataReader
*/
private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort): bool
{
Expand All @@ -386,9 +388,7 @@ private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort

private function createFilter(Sort $sort): FilterInterface
{
/**
* @psalm-var PageToken $this->token The code calling this method must ensure that page token is not null.
*/
/** @phpstan-ignore property.nonObject (The code calling this method must ensure that page token is not null) */
$value = $this->token->value;
[$field, $sorting] = $this->getFieldAndSortingFromSort($sort);

Expand All @@ -410,9 +410,7 @@ private function createFilter(Sort $sort): FilterInterface

private function getReverseFilter(Sort $sort): FilterInterface
{
/**
* @psalm-var PageToken $this->token The code calling this method must ensure that page token is not null.
*/
/** @phpstan-ignore property.nonObject (The code calling this method must ensure that page token is not null) */
$value = $this->token->value;
[$field, $sorting] = $this->getFieldAndSortingFromSort($sort);

Expand Down Expand Up @@ -444,7 +442,7 @@ private function reverseSort(Sort $sort): Sort
}

/**
* @psalm-return array{0: string, 1: int}
* @return array{0: string, 1: int}
*/
private function getFieldAndSortingFromSort(Sort $sort): array
{
Expand All @@ -457,8 +455,8 @@ private function getFieldAndSortingFromSort(Sort $sort): array
}

/**
* @param FilterableDataInterface&LimitableDataInterface&ReadableDataInterface<TKey, TValue>&SortableDataInterface $dataReader
* @return FilterableDataInterface&LimitableDataInterface&ReadableDataInterface<TKey, TValue>&SortableDataInterface
* @param FilterableDataInterface<TKey, TValue>&LimitableDataInterface<TKey, TValue>&ReadableDataInterface<TKey, TValue>&SortableDataInterface<TKey, TValue> $dataReader
* @return FilterableDataInterface<TKey, TValue>&LimitableDataInterface<TKey, TValue>&ReadableDataInterface<TKey, TValue>&SortableDataInterface<TKey, TValue>
*/
private function prepareSortInDataReader(ReadableDataInterface $dataReader, ?Sort $sort): ReadableDataInterface
{
Expand Down
Loading