Skip to content

Commit

Permalink
Simplify impossible cases
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Oct 18, 2024
1 parent 712ee0b commit 66f53dc
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
6 changes: 1 addition & 5 deletions src/Paginator/KeysetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,11 @@ public function getNextToken(): ?PageToken

public function isSortable(): bool
{
return !($this->dataReader instanceof LimitableDataInterface && $this->dataReader->getLimit() !== null);
return true;
}

public function withSort(?Sort $sort): static
{
if (!$this->isSortable()) {
throw new InvalidArgumentException('Data reader does not support sorting.');
}

$new = clone $this;
$new->dataReader = $this->dataReader->withSort($sort);
return $new;
Expand Down
3 changes: 1 addition & 2 deletions src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use InvalidArgumentException;
use LogicException;
use Yiisoft\Data\Reader\CountableDataInterface;
use Yiisoft\Data\Reader\FilterableDataInterface;
use Yiisoft\Data\Reader\FilterInterface;
Expand Down Expand Up @@ -253,7 +252,7 @@ public function isFilterable(): bool
public function withFilter(FilterInterface $filter): static
{
if (!$this->isFilterable()) {
throw new LogicException('Data reader does not support filtering.');
throw new InvalidArgumentException('Data reader does not support filtering.');
}

$new = clone $this;
Expand Down
4 changes: 4 additions & 0 deletions tests/Paginator/KeysetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use ArrayIterator;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
use stdClass;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Paginator\KeysetFilterContext;
use Yiisoft\Data\Paginator\KeysetPaginator;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Filter\GreaterThan;
Expand All @@ -27,6 +29,8 @@
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Data\Reader\SortableDataInterface;
use Yiisoft\Data\Tests\Support\MutationDataReader;
use Yiisoft\Data\Tests\Support\StubKeysetData;
use Yiisoft\Data\Tests\Support\StubOffsetData;
use Yiisoft\Data\Tests\TestCase;

use function array_values;
Expand Down
5 changes: 2 additions & 3 deletions tests/Paginator/OffsetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Data\Tests\Paginator;

use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Paginator\PageNotFoundException;
Expand Down Expand Up @@ -561,7 +560,7 @@ public function testWithSortNonSortableData(): void
{
$paginator = new OffsetPaginator(new StubOffsetData());

$this->expectException(LogicException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Data reader does not support sorting.');
$paginator->withSort(null);
}
Expand Down Expand Up @@ -600,7 +599,7 @@ public function testWithFilterNonFilterableData(): void
{
$paginator = new OffsetPaginator(new StubOffsetData());

$this->expectException(LogicException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Data reader does not support filtering.');
$paginator->withFilter(new Equals('id', 2));
}
Expand Down
80 changes: 80 additions & 0 deletions tests/Support/StubKeysetData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Support;

use Yiisoft\Data\Reader\CountableDataInterface;
use Yiisoft\Data\Reader\FilterableDataInterface;
use Yiisoft\Data\Reader\FilterHandlerInterface;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\LimitableDataInterface;
use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Data\Reader\SortableDataInterface;

final class StubKeysetData implements
ReadableDataInterface,
CountableDataInterface,
LimitableDataInterface,
FilterableDataInterface,
SortableDataInterface
{
/**
* @var int|null
* @psalm-param null|non-negative-int
*/
private ?int $limit = null;

public function read(): iterable
{
return [];
}

public function readOne(): array|object|null
{
return null;
}

public function count(): int
{
return 0;
}

public function withLimit(?int $limit): static
{
$new = clone $this;
$new->limit = $limit;
return $new;
}

public function getLimit(): ?int
{
return $this->limit;
}

public function withFilter(?FilterInterface $filter): static
{
return clone $this;
}

public function getFilter(): ?FilterInterface
{
return null;
}

public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
{
return clone $this;
}

public function withSort(?Sort $sort): static
{
return clone $this;
}

public function getSort(): ?Sort
{
return Sort::only(['id'])->withOrderString('id');
}
}
2 changes: 1 addition & 1 deletion tests/Support/StubOffsetData.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function withOffset(int $offset): static
return $this;
}

public function getLimit(): int
public function getLimit(): ?int
{
return 0;
}
Expand Down

0 comments on commit 66f53dc

Please sign in to comment.