Skip to content

Commit 3c0e136

Browse files
authored
Remove nullable types from withFilter() and getFilter() methods of FilterableDataInterface (#233)
1 parent 0ca75df commit 3c0e136

File tree

7 files changed

+23
-23
lines changed

7 files changed

+23
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- New #213: Add `nextPage()` and `previousPage()` methods to `PaginatorInterface` (@samdark)
6161
- New #200: Add matching mode parameter to `Like` filter (@samdark, @vjik)
6262
- New #232: Add `All` and `None` filters (@vjik)
63+
- Chg #233: Remove nullable types from `withFilter()` and `getFilter()` methods of `FilterableDataInterface` (@vjik)
6364

6465
## 1.0.1 January 25, 2023
6566

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
},
3838
"require-dev": {
3939
"maglnet/composer-require-checker": "^4.7.1",
40-
"phpunit/phpunit": "^10.5.48",
41-
"rector/rector": "^2.1.2",
40+
"phpunit/phpunit": "^10.5.52",
41+
"rector/rector": "^2.1.4",
4242
"roave/infection-static-analysis-plugin": "^1.35",
4343
"spatie/phpunit-watcher": "^1.24",
4444
"vimeo/psalm": "^5.26.1 || ^6.10.3"

src/Reader/FilterableDataInterface.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,25 @@ interface FilterableDataInterface extends ReadableDataInterface
2525
/**
2626
* Returns new instance with data reading criteria set.
2727
*
28-
* @param ?FilterInterface $filter Data reading criteria.
28+
* @param FilterInterface $filter Data reading criteria.
2929
*
3030
* @return static New instance.
31-
* @psalm-return $this
3231
*/
33-
public function withFilter(?FilterInterface $filter): static;
32+
public function withFilter(FilterInterface $filter): static;
3433

3534
/**
3635
* Get current data reading criteria.
3736
*
38-
* @return FilterInterface|null Data reading criteria.
37+
* @return FilterInterface Data reading criteria.
3938
*/
40-
public function getFilter(): ?FilterInterface;
39+
public function getFilter(): FilterInterface;
4140

4241
/**
4342
* Returns new instance with additional handlers set.
4443
*
4544
* @param FilterHandlerInterface ...$filterHandlers Additional filter handlers.
4645
*
4746
* @return static New instance.
48-
* @psalm-return $this
4947
*/
5048
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static;
5149
}

src/Reader/Iterable/IterableDataReader.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Yiisoft\Arrays\ArrayHelper;
1111
use Yiisoft\Data\Reader\DataReaderException;
1212
use Yiisoft\Data\Reader\DataReaderInterface;
13+
use Yiisoft\Data\Reader\Filter\All;
1314
use Yiisoft\Data\Reader\FilterHandlerInterface;
1415
use Yiisoft\Data\Reader\FilterInterface;
1516
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
@@ -53,7 +54,7 @@
5354
final class IterableDataReader implements DataReaderInterface
5455
{
5556
private ?Sort $sort = null;
56-
private ?FilterInterface $filter = null;
57+
private FilterInterface $filter;
5758

5859
/**
5960
* @psalm-var non-negative-int|null
@@ -93,6 +94,7 @@ public function __construct(
9394
new NotHandler(),
9495
]);
9596
$this->context = new Context($this->coreFilterHandlers, $this->valueReader);
97+
$this->filter = new All();
9698
}
9799

98100
/**
@@ -111,10 +113,7 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
111113
return $new;
112114
}
113115

114-
/**
115-
* @psalm-return $this
116-
*/
117-
public function withFilter(?FilterInterface $filter): static
116+
public function withFilter(FilterInterface $filter): static
118117
{
119118
$new = clone $this;
120119
$new->filter = $filter;
@@ -216,8 +215,8 @@ private function internalRead(bool $useLimitAndOffset): array
216215
continue;
217216
}
218217

219-
// Filter items.
220-
if ($this->filter === null || $this->matchFilter($item, $this->filter)) {
218+
// Filter items
219+
if ($this->matchFilter($item, $this->filter)) {
221220
$data[$key] = $item;
222221
}
223222
}
@@ -320,7 +319,7 @@ private function iterableToArray(iterable $iterable): array
320319
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
321320
}
322321

323-
public function getFilter(): ?FilterInterface
322+
public function getFilter(): FilterInterface
324323
{
325324
return $this->filter;
326325
}

tests/Paginator/KeysetPaginatorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Yiisoft\Data\Paginator\KeysetFilterContext;
1414
use Yiisoft\Data\Paginator\KeysetPaginator;
1515
use Yiisoft\Data\Paginator\PageToken;
16+
use Yiisoft\Data\Reader\Filter\All;
1617
use Yiisoft\Data\Reader\Filter\Equals;
1718
use Yiisoft\Data\Reader\Filter\GreaterThan;
1819
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
@@ -130,9 +131,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
130131
return clone $this;
131132
}
132133

133-
public function getFilter(): ?FilterInterface
134+
public function getFilter(): FilterInterface
134135
{
135-
return null;
136+
return new All();
136137
}
137138
};
138139

@@ -670,9 +671,9 @@ public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandler
670671
return clone $this;
671672
}
672673

673-
public function getFilter(): ?FilterInterface
674+
public function getFilter(): FilterInterface
674675
{
675-
return null;
676+
return new All();
676677
}
677678

678679
public function getLimit(): int

tests/Reader/Iterable/IterableDataReaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use InvalidArgumentException;
1010
use LogicException;
1111
use Yiisoft\Data\Reader\DataReaderException;
12+
use Yiisoft\Data\Reader\Filter\All;
1213
use Yiisoft\Data\Reader\Filter\AndX;
1314
use Yiisoft\Data\Reader\Filter\OrX;
1415
use Yiisoft\Data\Reader\Filter\Equals;
@@ -69,7 +70,7 @@ public function testImmutability(): void
6970
$reader = new IterableDataReader([]);
7071

7172
$this->assertNotSame($reader, $reader->withAddedFilterHandlers());
72-
$this->assertNotSame($reader, $reader->withFilter(null));
73+
$this->assertNotSame($reader, $reader->withFilter(new All()));
7374
$this->assertNotSame($reader, $reader->withSort(null));
7475
$this->assertNotSame($reader, $reader->withOffset(1));
7576
$this->assertNotSame($reader, $reader->withLimit(1));

tests/Support/MutationDataReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
) {
2727
}
2828

29-
public function withFilter(?FilterInterface $filter): static
29+
public function withFilter(FilterInterface $filter): static
3030
{
3131
$new = clone $this;
3232
$new->decorated = $this->decorated->withFilter($filter);
@@ -69,7 +69,7 @@ public function getSort(): ?Sort
6969
return $this->decorated->getSort();
7070
}
7171

72-
public function getFilter(): ?FilterInterface
72+
public function getFilter(): FilterInterface
7373
{
7474
return $this->decorated->getFilter();
7575
}

0 commit comments

Comments
 (0)