diff --git a/CHANGELOG.md b/CHANGELOG.md index f2414f8..567d1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ - New #213: Add `nextPage()` and `previousPage()` methods to `PaginatorInterface` (@samdark) - New #200: Add matching mode parameter to `Like` filter (@samdark, @vjik) - New #232: Add `All` and `None` filters (@vjik) +- Chg #233: Remove nullable types from `withFilter()` and `getFilter()` methods of `FilterableDataInterface` (@vjik) ## 1.0.1 January 25, 2023 diff --git a/composer.json b/composer.json index 28c99fe..aad84f8 100644 --- a/composer.json +++ b/composer.json @@ -37,8 +37,8 @@ }, "require-dev": { "maglnet/composer-require-checker": "^4.7.1", - "phpunit/phpunit": "^10.5.48", - "rector/rector": "^2.1.2", + "phpunit/phpunit": "^10.5.52", + "rector/rector": "^2.1.4", "roave/infection-static-analysis-plugin": "^1.35", "spatie/phpunit-watcher": "^1.24", "vimeo/psalm": "^5.26.1 || ^6.10.3" diff --git a/src/Reader/FilterableDataInterface.php b/src/Reader/FilterableDataInterface.php index d6b46b0..769f611 100644 --- a/src/Reader/FilterableDataInterface.php +++ b/src/Reader/FilterableDataInterface.php @@ -25,19 +25,18 @@ interface FilterableDataInterface extends ReadableDataInterface /** * Returns new instance with data reading criteria set. * - * @param ?FilterInterface $filter Data reading criteria. + * @param FilterInterface $filter Data reading criteria. * * @return static New instance. - * @psalm-return $this */ - public function withFilter(?FilterInterface $filter): static; + public function withFilter(FilterInterface $filter): static; /** * Get current data reading criteria. * - * @return FilterInterface|null Data reading criteria. + * @return FilterInterface Data reading criteria. */ - public function getFilter(): ?FilterInterface; + public function getFilter(): FilterInterface; /** * Returns new instance with additional handlers set. @@ -45,7 +44,6 @@ public function getFilter(): ?FilterInterface; * @param FilterHandlerInterface ...$filterHandlers Additional filter handlers. * * @return static New instance. - * @psalm-return $this */ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static; } diff --git a/src/Reader/Iterable/IterableDataReader.php b/src/Reader/Iterable/IterableDataReader.php index b664e27..52e93a8 100644 --- a/src/Reader/Iterable/IterableDataReader.php +++ b/src/Reader/Iterable/IterableDataReader.php @@ -10,6 +10,7 @@ use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Data\Reader\DataReaderException; use Yiisoft\Data\Reader\DataReaderInterface; +use Yiisoft\Data\Reader\Filter\All; use Yiisoft\Data\Reader\FilterHandlerInterface; use Yiisoft\Data\Reader\FilterInterface; use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler; @@ -53,7 +54,7 @@ final class IterableDataReader implements DataReaderInterface { private ?Sort $sort = null; - private ?FilterInterface $filter = null; + private FilterInterface $filter; /** * @psalm-var non-negative-int|null @@ -93,6 +94,7 @@ public function __construct( new NotHandler(), ]); $this->context = new Context($this->coreFilterHandlers, $this->valueReader); + $this->filter = new All(); } /** @@ -111,10 +113,7 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler return $new; } - /** - * @psalm-return $this - */ - public function withFilter(?FilterInterface $filter): static + public function withFilter(FilterInterface $filter): static { $new = clone $this; $new->filter = $filter; @@ -216,8 +215,8 @@ private function internalRead(bool $useLimitAndOffset): array continue; } - // Filter items. - if ($this->filter === null || $this->matchFilter($item, $this->filter)) { + // Filter items + if ($this->matchFilter($item, $this->filter)) { $data[$key] = $item; } } @@ -320,7 +319,7 @@ private function iterableToArray(iterable $iterable): array return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; } - public function getFilter(): ?FilterInterface + public function getFilter(): FilterInterface { return $this->filter; } diff --git a/tests/Paginator/KeysetPaginatorTest.php b/tests/Paginator/KeysetPaginatorTest.php index 2e292e8..d444dcb 100644 --- a/tests/Paginator/KeysetPaginatorTest.php +++ b/tests/Paginator/KeysetPaginatorTest.php @@ -13,6 +13,7 @@ use Yiisoft\Data\Paginator\KeysetFilterContext; use Yiisoft\Data\Paginator\KeysetPaginator; use Yiisoft\Data\Paginator\PageToken; +use Yiisoft\Data\Reader\Filter\All; use Yiisoft\Data\Reader\Filter\Equals; use Yiisoft\Data\Reader\Filter\GreaterThan; use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual; @@ -130,9 +131,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler return clone $this; } - public function getFilter(): ?FilterInterface + public function getFilter(): FilterInterface { - return null; + return new All(); } }; @@ -670,9 +671,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler return clone $this; } - public function getFilter(): ?FilterInterface + public function getFilter(): FilterInterface { - return null; + return new All(); } public function getLimit(): int diff --git a/tests/Reader/Iterable/IterableDataReaderTest.php b/tests/Reader/Iterable/IterableDataReaderTest.php index fd81608..c64f88b 100644 --- a/tests/Reader/Iterable/IterableDataReaderTest.php +++ b/tests/Reader/Iterable/IterableDataReaderTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use LogicException; use Yiisoft\Data\Reader\DataReaderException; +use Yiisoft\Data\Reader\Filter\All; use Yiisoft\Data\Reader\Filter\AndX; use Yiisoft\Data\Reader\Filter\OrX; use Yiisoft\Data\Reader\Filter\Equals; @@ -69,7 +70,7 @@ public function testImmutability(): void $reader = new IterableDataReader([]); $this->assertNotSame($reader, $reader->withAddedFilterHandlers()); - $this->assertNotSame($reader, $reader->withFilter(null)); + $this->assertNotSame($reader, $reader->withFilter(new All())); $this->assertNotSame($reader, $reader->withSort(null)); $this->assertNotSame($reader, $reader->withOffset(1)); $this->assertNotSame($reader, $reader->withLimit(1)); diff --git a/tests/Support/MutationDataReader.php b/tests/Support/MutationDataReader.php index 5ab606b..7f969d0 100644 --- a/tests/Support/MutationDataReader.php +++ b/tests/Support/MutationDataReader.php @@ -26,7 +26,7 @@ public function __construct( ) { } - public function withFilter(?FilterInterface $filter): static + public function withFilter(FilterInterface $filter): static { $new = clone $this; $new->decorated = $this->decorated->withFilter($filter); @@ -69,7 +69,7 @@ public function getSort(): ?Sort return $this->decorated->getSort(); } - public function getFilter(): ?FilterInterface + public function getFilter(): FilterInterface { return $this->decorated->getFilter(); }