Skip to content

Commit

Permalink
Fix #28: Correct work iterable processors with non-exists fields + Fi…
Browse files Browse the repository at this point in the history
…x psalm errors (#78)
  • Loading branch information
vjik committed Jul 26, 2021
1 parent f0db33e commit d4ff655
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/Paginator/KeysetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public function read(): iterable
*
* @psalm-mutation-free
*/
public function withPreviousPageToken(?string $value): self
public function withPreviousPageToken(?string $token): self
{
$new = clone $this;
$new->firstValue = $value;
$new->firstValue = $token;
$new->lastValue = null;
return $new;
}
Expand All @@ -137,11 +137,11 @@ public function withPreviousPageToken(?string $value): self
*
* @psalm-mutation-free
*/
public function withNextPageToken(?string $value): self
public function withNextPageToken(?string $token): self
{
$new = clone $this;
$new->firstValue = null;
$new->lastValue = $value;
$new->lastValue = $token;
return $new;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public function withCurrentPage(int $page): self
*
* @psalm-mutation-free
*/
public function withPageSize(int $size): self
public function withPageSize(int $pageSize): self
{
if ($size < 1) {
if ($pageSize < 1) {
throw new PaginatorException('Page size should be at least 1');
}
$new = clone $this;
$new->pageSize = $size;
$new->pageSize = $pageSize;
$new->cachedReader = null;
return $new;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Paginator/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function withPreviousPageToken(?string $token): self;
*
* @psalm-mutation-free
*/
public function withPageSize(int $limit): self;
public function withPageSize(int $pageSize): self;

public function getPageSize(): int;

Expand Down
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class Equals implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +21,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return $item[$field] == $value;
return array_key_exists($field, $item) && $item[$field] == $value;
}
}
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class GreaterThan implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +21,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return $item[$field] > $value;
return array_key_exists($field, $item) && $item[$field] > $value;
}
}
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class GreaterThanOrEqual implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +21,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return $item[$field] >= $value;
return array_key_exists($field, $item) && $item[$field] >= $value;
}
}
2 changes: 1 addition & 1 deletion src/Reader/Iterable/Processor/GroupProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

abstract class GroupProcessor implements IterableProcessorInterface, FilterProcessorInterface
{
abstract protected function checkResults(array $result): bool;
abstract protected function checkResults(array $results): bool;

abstract protected function checkResult($result): ?bool;

Expand Down
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class In implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -22,6 +24,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
if (!is_array($values)) {
throw new \InvalidArgumentException('The values not an array');
}
return in_array($item[$field], $values, false);
return array_key_exists($field, $item) && in_array($item[$field], $values, false);
}
}
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class LessThan implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +21,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return $item[$field] < $value;
return array_key_exists($field, $item) && $item[$field] < $value;
}
}
4 changes: 3 additions & 1 deletion src/Reader/Iterable/Processor/LessThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class LessThanOrEqual implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +21,6 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return $item[$field] <= $value;
return array_key_exists($field, $item) && $item[$field] <= $value;
}
}
7 changes: 6 additions & 1 deletion src/Reader/Iterable/Processor/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;
use function is_string;

class Like implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
Expand All @@ -19,6 +22,8 @@ public function match(array $item, array $arguments, array $filterProcessors): b
throw new \InvalidArgumentException('$arguments should contain exactly two elements');
}
[$field, $value] = $arguments;
return stripos($item[$field], (string)$value) !== false;
return array_key_exists($field, $item) &&
is_string($item[$field]) &&
stripos($item[$field], (string)$value) !== false;
}
}
2 changes: 1 addition & 1 deletion src/Reader/SortableDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface SortableDataInterface
*
* @psalm-mutation-free
*/
public function withSort(?Sort $sorting): self;
public function withSort(?Sort $sort): self;

public function getSort(): ?Sort;
}
35 changes: 35 additions & 0 deletions tests/Reader/Iterable/Processor/EqualsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\Equals;

final class EqualsTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', 45]],
[false, ['weight', 46]],
[false, ['age', 25]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new Equals();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
36 changes: 36 additions & 0 deletions tests/Reader/Iterable/Processor/GreaterThanOrEqualTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThanOrEqual;

final class GreaterThanOrEqualTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', 44]],
[true, ['weight', 45]],
[false, ['weight', 46]],
[false, ['age', 25]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new GreaterThanOrEqual();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
36 changes: 36 additions & 0 deletions tests/Reader/Iterable/Processor/GreaterThanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThan;

final class GreaterThanTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', 44]],
[false, ['weight', 45]],
[false, ['weight', 46]],
[false, ['age', 25]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new GreaterThan();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
35 changes: 35 additions & 0 deletions tests/Reader/Iterable/Processor/InTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\In;

final class InTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', [44, 45, 46]]],
[false, ['weight', [1, 2, 3]]],
[false, ['age', [45]]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new In();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
36 changes: 36 additions & 0 deletions tests/Reader/Iterable/Processor/LessThanOrEqualTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\LessThanOrEqual;

final class LessThanOrEqualTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', 46]],
[true, ['weight', 45]],
[false, ['weight', 44]],
[false, ['age', 25]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new LessThanOrEqual();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
Loading

0 comments on commit d4ff655

Please sign in to comment.