From dca640f991754144a902ed1d9c098cf382512aa0 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 10 Apr 2025 09:52:43 +0300 Subject: [PATCH 1/6] improve --- composer.json | 4 ++-- src/Reader/Filter/In.php | 3 +++ src/Reader/Iterable/IterableDataReader.php | 2 -- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index a7c194b1..3530c327 100644 --- a/composer.json +++ b/composer.json @@ -38,10 +38,10 @@ "require-dev": { "maglnet/composer-require-checker": "^4.7.1", "phpunit/phpunit": "^10.5.45", - "rector/rector": "^2.0.9", + "rector/rector": "^2.0.11", "roave/infection-static-analysis-plugin": "^1.35", "spatie/phpunit-watcher": "^1.24", - "vimeo/psalm": "^5.26.1 || ^6.9.1" + "vimeo/psalm": "^5.26.1 || ^6.10" }, "autoload": { "psr-4": { diff --git a/src/Reader/Filter/In.php b/src/Reader/Filter/In.php index 525b70ff..76c0d430 100644 --- a/src/Reader/Filter/In.php +++ b/src/Reader/Filter/In.php @@ -7,6 +7,9 @@ use InvalidArgumentException; use Yiisoft\Data\Reader\FilterInterface; +use function is_scalar; +use function sprintf; + /** * `In` filter defines a criteria for ensuring field value matches one of the value provided. */ diff --git a/src/Reader/Iterable/IterableDataReader.php b/src/Reader/Iterable/IterableDataReader.php index 0a3911f1..dc3c4906 100644 --- a/src/Reader/Iterable/IterableDataReader.php +++ b/src/Reader/Iterable/IterableDataReader.php @@ -253,9 +253,7 @@ private function sortItems(iterable $items, Sort $sort): iterable $items, static function (array|object $itemA, array|object $itemB) use ($criteria) { foreach ($criteria as $key => $order) { - /** @psalm-var mixed $valueA */ $valueA = ArrayHelper::getValue($itemA, $key); - /** @psalm-var mixed $valueB */ $valueB = ArrayHelper::getValue($itemB, $key); if ($valueB === $valueA) { From f4946dcde4ac4be1eb84a0a8402024754a4b3cab Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 10 May 2025 16:38:29 +0300 Subject: [PATCH 2/6] update dev deps --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 3530c327..9fc01984 100644 --- a/composer.json +++ b/composer.json @@ -37,11 +37,11 @@ }, "require-dev": { "maglnet/composer-require-checker": "^4.7.1", - "phpunit/phpunit": "^10.5.45", - "rector/rector": "^2.0.11", + "phpunit/phpunit": "^10.5.46", + "rector/rector": "^2.0.15", "roave/infection-static-analysis-plugin": "^1.35", "spatie/phpunit-watcher": "^1.24", - "vimeo/psalm": "^5.26.1 || ^6.10" + "vimeo/psalm": "^5.26.1 || ^6.10.3" }, "autoload": { "psr-4": { From 0b6ded79bb26408a3f5b3609888c52a0ccd89b7b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 10 May 2025 16:41:20 +0300 Subject: [PATCH 3/6] test --- tests/TestCase.php | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 63cf0e0d..a55f7495 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -87,28 +87,19 @@ protected function getInaccessibleProperty(object $object, string $propertyName) $class = $class->getParentClass(); } - $property = $class->getProperty($propertyName); - $property->setAccessible(true); - $result = $property->getValue($object); - $property->setAccessible(false); - - return $result; + return $class + ->getProperty($propertyName) + ->getValue($object); } /** * Invokes an inaccessible method. - * - * @throws ReflectionException */ protected function invokeMethod(object $object, string $method, array $args = []): mixed { - $reflection = new ReflectionObject($object); - $method = $reflection->getMethod($method); - $method->setAccessible(true); - $result = $method->invokeArgs($object, $args); - $method->setAccessible(false); - - return $result; + return (new ReflectionObject($object)) + ->getMethod($method) + ->invokeArgs($object, $args); } protected function iterableToArray(iterable $iterable): array From d409af50fc22a7f9f7c41dca721d3a914e825d4d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 10 May 2025 16:45:24 +0300 Subject: [PATCH 4/6] cleanup --- src/Reader/Iterable/FilterHandler/AllHandler.php | 6 +++++- src/Reader/Iterable/FilterHandler/AnyHandler.php | 6 +++++- src/Reader/Iterable/IterableDataReader.php | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Reader/Iterable/FilterHandler/AllHandler.php b/src/Reader/Iterable/FilterHandler/AllHandler.php index ca6c3a5c..4fa14f80 100644 --- a/src/Reader/Iterable/FilterHandler/AllHandler.php +++ b/src/Reader/Iterable/FilterHandler/AllHandler.php @@ -9,6 +9,8 @@ use Yiisoft\Data\Reader\FilterInterface; use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface; +use function sprintf; + /** * `All` iterable filter handler allows combining multiple sub-filters. * The filter matches only if all the sub-filters match. @@ -27,7 +29,9 @@ public function match(object|array $item, FilterInterface $filter, array $iterab foreach ($filter->getFilters() as $subFilter) { $filterHandler = $iterableFilterHandlers[$subFilter::class] ?? null; if ($filterHandler === null) { - throw new LogicException(sprintf('Filter "%s" is not supported.', $subFilter::class)); + throw new LogicException( + sprintf('Filter "%s" is not supported.', $subFilter::class), + ); } if (!$filterHandler->match($item, $subFilter, $iterableFilterHandlers)) { return false; diff --git a/src/Reader/Iterable/FilterHandler/AnyHandler.php b/src/Reader/Iterable/FilterHandler/AnyHandler.php index c26708dd..1f5349e2 100644 --- a/src/Reader/Iterable/FilterHandler/AnyHandler.php +++ b/src/Reader/Iterable/FilterHandler/AnyHandler.php @@ -9,6 +9,8 @@ use Yiisoft\Data\Reader\FilterInterface; use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface; +use function sprintf; + /** * `Any` iterable filter handler allows combining multiple sub-filters. * The filter matches if any of the sub-filters match. @@ -27,7 +29,9 @@ public function match(object|array $item, FilterInterface $filter, array $iterab foreach ($filter->getFilters() as $subFilter) { $filterHandler = $iterableFilterHandlers[$subFilter::class] ?? null; if ($filterHandler === null) { - throw new LogicException(sprintf('Filter "%s" is not supported.', $subFilter::class)); + throw new LogicException( + sprintf('Filter "%s" is not supported.', $subFilter::class), + ); } if ($filterHandler->match($item, $subFilter, $iterableFilterHandlers)) { return true; diff --git a/src/Reader/Iterable/IterableDataReader.php b/src/Reader/Iterable/IterableDataReader.php index dc3c4906..54cb77de 100644 --- a/src/Reader/Iterable/IterableDataReader.php +++ b/src/Reader/Iterable/IterableDataReader.php @@ -309,7 +309,7 @@ private function prepareFilterHandlers(array $filterHandlers): array */ private function iterableToArray(iterable $iterable): array { - return $iterable instanceof Traversable ? iterator_to_array($iterable, true) : $iterable; + return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; } public function getFilter(): ?FilterInterface From 06f5cc5b731b8bf5957c338859b2a382f2317197 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 10 May 2025 16:50:05 +0300 Subject: [PATCH 5/6] test coverage 100% --- tests/Paginator/OffsetPaginatorTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Paginator/OffsetPaginatorTest.php b/tests/Paginator/OffsetPaginatorTest.php index 5989d018..5a0021a2 100644 --- a/tests/Paginator/OffsetPaginatorTest.php +++ b/tests/Paginator/OffsetPaginatorTest.php @@ -7,6 +7,7 @@ use InvalidArgumentException; use LogicException; use PHPUnit\Framework\Attributes\DataProvider; +use Yiisoft\Data\Paginator\InvalidPageException; use Yiisoft\Data\Paginator\OffsetPaginator; use Yiisoft\Data\Paginator\PageNotFoundException; use Yiisoft\Data\Paginator\PageToken; @@ -601,6 +602,16 @@ public function testWithNulledPageToken(): void $this->assertFalse($token->isPrevious); } + public function testWithNegativeToken(): void + { + $paginator = new OffsetPaginator(new IterableDataReader([])); + $token = PageToken::next('-1'); + + $this->expectException(InvalidPageException::class); + $this->expectExceptionMessage('Current page should be at least 1.'); + $paginator->withToken($token); + } + public function testLimitedDataReaderTotalItems(): void { $dataReader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(3); From 9cf20cb4144762ac546ae9c859ea54b54ae97724 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 10 May 2025 13:50:22 +0000 Subject: [PATCH 6/6] Apply fixes from StyleCI --- tests/Paginator/KeysetPaginatorTest.php | 2 +- tests/TestCase.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Paginator/KeysetPaginatorTest.php b/tests/Paginator/KeysetPaginatorTest.php index f0b6781a..8a26ee36 100644 --- a/tests/Paginator/KeysetPaginatorTest.php +++ b/tests/Paginator/KeysetPaginatorTest.php @@ -34,7 +34,7 @@ use function reset; use function sprintf; -final class KeysetPaginatorTest extends Testcase +final class KeysetPaginatorTest extends TestCase { use PageTokenAssertTrait; diff --git a/tests/TestCase.php b/tests/TestCase.php index a55f7495..09b11a26 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,7 +6,6 @@ use DateTimeImmutable; use DateTimeInterface; -use ReflectionException; use ReflectionObject; use stdClass; use Traversable;