Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IncorrectValueException that throws if there are definitely no entries for the filter #172

Merged
merged 2 commits into from
Mar 2, 2024
Merged
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
12 changes: 10 additions & 2 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,17 @@
}

/**
* @psalm-return list{FilterInterface[],ValidationResult}
* @return array The array with format:
* ```
* [
* FilterInterface[]|null, // Array of filters or `null` if there are definitely no entries for the current filter
* ValidationResult, // Validation result of the filter
* ]
* ```
*
* @psalm-return list{FilterInterface[]|null,ValidationResult}
*/
protected function makeFilters(): array

Check warning on line 256 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ * * @psalm-return list{FilterInterface[]|null,ValidationResult} */ - protected function makeFilters() : array + private function makeFilters() : array { return [[], new ValidationResult()]; }
{
return [[], new ValidationResult()];
}
Expand Down Expand Up @@ -323,7 +331,7 @@

if (!$dataReader instanceof PaginatorInterface) {
if (
$dataReader instanceof OffsetableDataInterface

Check warning on line 334 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ { $dataReader = $this->getDataReader(); if (!$dataReader instanceof PaginatorInterface) { - if ($dataReader instanceof OffsetableDataInterface && $dataReader instanceof CountableDataInterface && $dataReader instanceof LimitableDataInterface) { + if (($dataReader instanceof OffsetableDataInterface || $dataReader instanceof CountableDataInterface) && $dataReader instanceof LimitableDataInterface) { $dataReader = new OffsetPaginator($dataReader); } elseif ($dataReader instanceof FilterableDataInterface && $dataReader instanceof SortableDataInterface && $dataReader instanceof LimitableDataInterface) { if ($dataReader->getSort() !== null) {
&& $dataReader instanceof CountableDataInterface
&& $dataReader instanceof LimitableDataInterface
) {
Expand All @@ -343,7 +351,7 @@
}
}

if ($dataReader->isPaginationRequired()) {

Check warning on line 354 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ return $dataReader; } } - if ($dataReader->isPaginationRequired()) { + if (!$dataReader->isPaginationRequired()) { if ($pageSize !== null) { $dataReader = $dataReader->withPageSize((int) $pageSize); }
if ($pageSize !== null) {
$dataReader = $dataReader->withPageSize((int) $pageSize);
}
Expand All @@ -355,14 +363,14 @@
}
}

if ($dataReader->isSortable() && !empty($sort)) {

Check warning on line 366 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withToken(PageToken::previous($previousPage)); } } - if ($dataReader->isSortable() && !empty($sort)) { + if (!$dataReader->isSortable() && !empty($sort)) { $sortObject = $dataReader->getSort(); if ($sortObject !== null) { $dataReader = $dataReader->withSort($sortObject->withOrderString($sort));
$sortObject = $dataReader->getSort();
if ($sortObject !== null) {
$dataReader = $dataReader->withSort($sortObject->withOrderString($sort));
}
}

if ($dataReader->isFilterable() && !empty($filters)) {

Check warning on line 373 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrderString($sort)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if ($dataReader->isFilterable() || !empty($filters)) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 373 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrderString($sort)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if (!($dataReader->isFilterable() && !empty($filters))) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 373 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrderString($sort)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if (!$dataReader->isFilterable() && !empty($filters)) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;
$dataReader = $dataReader->withFilter(new All(...$filters));
}

Expand Down Expand Up @@ -550,7 +558,7 @@
public function render(): string
{
[$filters, $filterValidationResult] = $this->makeFilters();
$items = $this->prepareDataReaderAndGetItems($filters);
$items = $filters === null ? [] : $this->prepareDataReaderAndGetItems($filters);

$content = trim(
strtr(
Expand Down Expand Up @@ -644,7 +652,7 @@
$currentPage = $dataReader->getCurrentPage();

// The starting row number (1-based) currently being displayed
$begin = ($currentPage - 1) * $dataReader->getPageSize() + 1;

Check warning on line 655 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ // The page number (1-based) current being displayed $currentPage = $dataReader->getCurrentPage(); // The starting row number (1-based) currently being displayed - $begin = ($currentPage - 1) * $dataReader->getPageSize() + 1; + $begin = ($currentPage - 0) * $dataReader->getPageSize() + 1; // The number of rows currently being displayed $count = $dataReader->getCurrentPageSize(); // The ending row number (1-based) currently being displayed

Check warning on line 655 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ // The page number (1-based) current being displayed $currentPage = $dataReader->getCurrentPage(); // The starting row number (1-based) currently being displayed - $begin = ($currentPage - 1) * $dataReader->getPageSize() + 1; + $begin = ($currentPage - 2) * $dataReader->getPageSize() + 1; // The number of rows currently being displayed $count = $dataReader->getCurrentPageSize(); // The ending row number (1-based) currently being displayed

Check warning on line 655 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- Original +++ New @@ @@ // The page number (1-based) current being displayed $currentPage = $dataReader->getCurrentPage(); // The starting row number (1-based) currently being displayed - $begin = ($currentPage - 1) * $dataReader->getPageSize() + 1; + $begin = ($currentPage + 1) * $dataReader->getPageSize() + 1; // The number of rows currently being displayed $count = $dataReader->getCurrentPageSize(); // The ending row number (1-based) currently being displayed

// The number of rows currently being displayed
$count = $dataReader->getCurrentPageSize();
Expand Down
4 changes: 4 additions & 0 deletions src/Column/FilterableColumnRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\FilterContext;
use Yiisoft\Yii\DataView\Column\Base\MakeFilterContext;
use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException;

interface FilterableColumnRendererInterface extends ColumnRendererInterface
{
public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell;

/**
* @throws IncorrectValueException
*/
public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface;
}
3 changes: 3 additions & 0 deletions src/Filter/Factory/FilterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

interface FilterFactoryInterface
{
/**
* @throws IncorrectValueException
*/
public function create(string $property, string $value): ?FilterInterface;
}
11 changes: 11 additions & 0 deletions src/Filter/Factory/IncorrectValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView\Filter\Factory;

use Exception;

final class IncorrectValueException extends Exception
{
}
8 changes: 7 additions & 1 deletion src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Yiisoft\Yii\DataView\Column\ColumnInterface;
use Yiisoft\Yii\DataView\Column\ColumnRendererInterface;
use Yiisoft\Yii\DataView\Column\FilterableColumnRendererInterface;
use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException;

/**
* The GridView widget is used to display data in a grid.
Expand Down Expand Up @@ -649,7 +650,12 @@
$filters = [];
foreach ($columns as $i => $column) {
if ($renderers[$i] instanceof FilterableColumnRendererInterface) {
$filter = $renderers[$i]->makeFilter($column, $context);
try {
$filter = $renderers[$i]->makeFilter($column, $context);
} catch (IncorrectValueException) {
$filters = null;
break;

Check warning on line 657 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L655-L657

Added lines #L655 - L657 were not covered by tests
}
if ($filter !== null) {
$filters[] = $filter;
}
Expand Down
Loading