diff --git a/.github/workflows/rector-cs.yml b/.github/workflows/rector-cs.yml new file mode 100644 index 00000000..f0d66198 --- /dev/null +++ b/.github/workflows/rector-cs.yml @@ -0,0 +1,27 @@ +name: Rector + PHP CS Fixer + +on: + pull_request_target: + paths: + - 'src/**' + - 'tests/**' + - '.github/workflows/rector-cs.yml' + - 'composer.json' + - 'rector.php' + - '.php-cs-fixer.dist.php' + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector-cs.yml@master + secrets: + token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + php: '8.4' diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml deleted file mode 100644 index b2294456..00000000 --- a/.github/workflows/rector.yml +++ /dev/null @@ -1,23 +0,0 @@ -on: - pull_request: - paths-ignore: - - 'docs/**' - - 'README.md' - - 'CHANGELOG.md' - - '.gitignore' - - '.gitattributes' - - 'infection.json.dist' - - 'psalm.xml' - -name: rector - -jobs: - rector: - uses: yiisoft/actions/.github/workflows/rector.yml@master - secrets: - token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} - with: - os: >- - ['ubuntu-latest'] - php: >- - ['8.4'] diff --git a/.gitignore b/.gitignore index db6cb742..8c3d8ad2 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ phpunit.phar /phpunit.xml # phpunit cache /.phpunit.cache/* + +# PHP CS Fixer +/.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 00000000..c8091220 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,22 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return (new Config()) + ->setParallelConfig(ParallelConfigFactory::detect()) + ->setRules([ + '@PER-CS3.0' => true, + 'no_unused_imports' => true, + 'ordered_class_elements' => true, + 'class_attributes_separation' => ['elements' => ['method' => 'one']], + ]) + ->setFinder($finder); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 1ab379b4..00000000 --- a/.styleci.yml +++ /dev/null @@ -1,85 +0,0 @@ -preset: psr12 -risky: true - -version: 8.1 - -finder: - exclude: - - docs - - vendor - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - hash_to_slash_comment - - is_null - - logical_operators - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals - - empty_loop_body_braces - - integer_literal_case - - union_type_without_spaces - -disabled: - - function_declaration diff --git a/composer.json b/composer.json index aad84f80..3cb23e46 100644 --- a/composer.json +++ b/composer.json @@ -36,9 +36,10 @@ "yiisoft/arrays": "^3.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.89.2", "maglnet/composer-require-checker": "^4.7.1", "phpunit/phpunit": "^10.5.52", - "rector/rector": "^2.1.4", + "rector/rector": "^2.2.8", "roave/infection-static-analysis-plugin": "^1.35", "spatie/phpunit-watcher": "^1.24", "vimeo/psalm": "^5.26.1 || ^6.10.3" @@ -62,6 +63,7 @@ }, "scripts": { "test": "phpunit --testdox --no-interaction", - "test-watch": "phpunit-watcher watch" + "test-watch": "phpunit-watcher watch", + "cs-fix": "php-cs-fixer fix" } } diff --git a/src/Paginator/InvalidPageException.php b/src/Paginator/InvalidPageException.php index b91eb7b4..6557c518 100644 --- a/src/Paginator/InvalidPageException.php +++ b/src/Paginator/InvalidPageException.php @@ -9,6 +9,4 @@ /** * Thrown when the page is invalid. */ -class InvalidPageException extends LogicException -{ -} +class InvalidPageException extends LogicException {} diff --git a/src/Paginator/KeysetFilterContext.php b/src/Paginator/KeysetFilterContext.php index cb0c1fd9..a8eb6c91 100644 --- a/src/Paginator/KeysetFilterContext.php +++ b/src/Paginator/KeysetFilterContext.php @@ -10,7 +10,6 @@ public function __construct( public readonly string $field, public readonly string $value, public readonly int $sorting, - public readonly bool $isReverse - ) { - } + public readonly bool $isReverse, + ) {} } diff --git a/src/Paginator/KeysetPaginator.php b/src/Paginator/KeysetPaginator.php index bfb63546..a1393472 100644 --- a/src/Paginator/KeysetPaginator.php +++ b/src/Paginator/KeysetPaginator.php @@ -469,7 +469,7 @@ private function prepareSortInDataReader(ReadableDataInterface $dataReader, ?Sor throw new InvalidArgumentException('Data should be always sorted to work with keyset pagination.'); } $sort = $sort->withOrder( - array_slice($defaultOrder, 0, 1, true) + array_slice($defaultOrder, 0, 1, true), ); } diff --git a/src/Paginator/PageNotFoundException.php b/src/Paginator/PageNotFoundException.php index 1341829e..60c4eaf6 100644 --- a/src/Paginator/PageNotFoundException.php +++ b/src/Paginator/PageNotFoundException.php @@ -11,7 +11,7 @@ final class PageNotFoundException extends InvalidPageException public function __construct(int $page) { parent::__construct( - sprintf('Page %d not found.', $page) + sprintf('Page %d not found.', $page), ); } } diff --git a/src/Paginator/PageToken.php b/src/Paginator/PageToken.php index f1f3d770..95931907 100644 --- a/src/Paginator/PageToken.php +++ b/src/Paginator/PageToken.php @@ -9,8 +9,7 @@ final class PageToken private function __construct( public readonly string $value, public readonly bool $isPrevious, - ) { - } + ) {} public static function previous(string $value): self { diff --git a/src/Processor/DataProcessorException.php b/src/Processor/DataProcessorException.php index f7abb545..1a48ae96 100644 --- a/src/Processor/DataProcessorException.php +++ b/src/Processor/DataProcessorException.php @@ -13,6 +13,4 @@ * * @psalm-suppress ClassMustBeFinal We assume that the class may be extended in userland. */ -class DataProcessorException extends RuntimeException -{ -} +class DataProcessorException extends RuntimeException {} diff --git a/src/Reader/DataReaderException.php b/src/Reader/DataReaderException.php index 87f2b35c..d12a8274 100644 --- a/src/Reader/DataReaderException.php +++ b/src/Reader/DataReaderException.php @@ -13,6 +13,4 @@ * * @psalm-suppress ClassMustBeFinal We assume that the class may be extended in userland. */ -class DataReaderException extends RuntimeException -{ -} +class DataReaderException extends RuntimeException {} diff --git a/src/Reader/DataReaderInterface.php b/src/Reader/DataReaderInterface.php index 4cb9f9d6..8449af86 100644 --- a/src/Reader/DataReaderInterface.php +++ b/src/Reader/DataReaderInterface.php @@ -31,6 +31,4 @@ interface DataReaderInterface extends CountableDataInterface, SortableDataInterface, FilterableDataInterface, - IteratorAggregate -{ -} + IteratorAggregate {} diff --git a/src/Reader/Filter/All.php b/src/Reader/Filter/All.php index 106feeb6..3ee92251 100644 --- a/src/Reader/Filter/All.php +++ b/src/Reader/Filter/All.php @@ -10,6 +10,4 @@ * Represents a filter that matches all items. * Used to indicate that no filtering should be applied. */ -final class All implements FilterInterface -{ -} +final class All implements FilterInterface {} diff --git a/src/Reader/Filter/Between.php b/src/Reader/Filter/Between.php index 092f5f45..b15d4926 100644 --- a/src/Reader/Filter/Between.php +++ b/src/Reader/Filter/Between.php @@ -22,7 +22,6 @@ final class Between implements FilterInterface public function __construct( public readonly string $field, public readonly bool|DateTimeInterface|float|int|string|Stringable $minValue, - public readonly bool|DateTimeInterface|float|int|string|Stringable $maxValue - ) { - } + public readonly bool|DateTimeInterface|float|int|string|Stringable $maxValue, + ) {} } diff --git a/src/Reader/Filter/Compare.php b/src/Reader/Filter/Compare.php index afefa1a3..c94b3ddc 100644 --- a/src/Reader/Filter/Compare.php +++ b/src/Reader/Filter/Compare.php @@ -20,8 +20,7 @@ abstract class Compare implements FilterInterface final public function __construct( public readonly string $field, public readonly bool|DateTimeInterface|float|int|string|Stringable $value, - ) { - } + ) {} /** * @param bool|DateTimeInterface|float|int|string|Stringable $value Value to compare to. diff --git a/src/Reader/Filter/Equals.php b/src/Reader/Filter/Equals.php index 6b7ef70e..a0826af7 100644 --- a/src/Reader/Filter/Equals.php +++ b/src/Reader/Filter/Equals.php @@ -7,6 +7,4 @@ /** * `Equals` filter defines a criteria for ensuring field value equals a given value. */ -final class Equals extends Compare -{ -} +final class Equals extends Compare {} diff --git a/src/Reader/Filter/EqualsNull.php b/src/Reader/Filter/EqualsNull.php index 0b50edba..dd3f091f 100644 --- a/src/Reader/Filter/EqualsNull.php +++ b/src/Reader/Filter/EqualsNull.php @@ -16,6 +16,5 @@ final class EqualsNull implements FilterInterface */ public function __construct( public readonly string $field, - ) { - } + ) {} } diff --git a/src/Reader/Filter/GreaterThan.php b/src/Reader/Filter/GreaterThan.php index ec474b6d..812d760f 100644 --- a/src/Reader/Filter/GreaterThan.php +++ b/src/Reader/Filter/GreaterThan.php @@ -7,6 +7,4 @@ /** * `GreaterThan` filter defines a criteria for ensuring field value is greater than a given value. */ -final class GreaterThan extends Compare -{ -} +final class GreaterThan extends Compare {} diff --git a/src/Reader/Filter/GreaterThanOrEqual.php b/src/Reader/Filter/GreaterThanOrEqual.php index feb95371..6fa26e20 100644 --- a/src/Reader/Filter/GreaterThanOrEqual.php +++ b/src/Reader/Filter/GreaterThanOrEqual.php @@ -7,6 +7,4 @@ /** * `GreaterThanOrEqual` filter defines a criteria for ensuring field value is greater than or equal to a given value. */ -final class GreaterThanOrEqual extends Compare -{ -} +final class GreaterThanOrEqual extends Compare {} diff --git a/src/Reader/Filter/In.php b/src/Reader/Filter/In.php index 911df614..c7755c5b 100644 --- a/src/Reader/Filter/In.php +++ b/src/Reader/Filter/In.php @@ -23,7 +23,7 @@ final class In implements FilterInterface public function __construct( public readonly string $field, /** @var bool[]|float[]|int[]|string[]|Stringable[] Values to check against. */ - public readonly array $values + public readonly array $values, ) { $this->assertValues($values); } @@ -36,7 +36,7 @@ private function assertValues(array $values): void sprintf( 'The value should be scalar or Stringable. "%s" is received.', get_debug_type($value), - ) + ), ); } } diff --git a/src/Reader/Filter/LessThan.php b/src/Reader/Filter/LessThan.php index eb2d0824..c236b0b6 100644 --- a/src/Reader/Filter/LessThan.php +++ b/src/Reader/Filter/LessThan.php @@ -7,6 +7,4 @@ /** * `LessThan` filter defines a criteria for ensuring field value is less than a given value. */ -final class LessThan extends Compare -{ -} +final class LessThan extends Compare {} diff --git a/src/Reader/Filter/LessThanOrEqual.php b/src/Reader/Filter/LessThanOrEqual.php index 876b60e1..d33a27af 100644 --- a/src/Reader/Filter/LessThanOrEqual.php +++ b/src/Reader/Filter/LessThanOrEqual.php @@ -7,6 +7,4 @@ /** * `LessThanOrEqual` filter defines a criteria for ensuring field value is less than or equal to a given value. */ -final class LessThanOrEqual extends Compare -{ -} +final class LessThanOrEqual extends Compare {} diff --git a/src/Reader/Filter/Like.php b/src/Reader/Filter/Like.php index 6b12482f..e2fcdf88 100644 --- a/src/Reader/Filter/Like.php +++ b/src/Reader/Filter/Like.php @@ -27,6 +27,5 @@ public function __construct( public readonly string|Stringable $value, public readonly ?bool $caseSensitive = null, public readonly LikeMode $mode = LikeMode::Contains, - ) { - } + ) {} } diff --git a/src/Reader/Filter/None.php b/src/Reader/Filter/None.php index f729ef18..e39410ce 100644 --- a/src/Reader/Filter/None.php +++ b/src/Reader/Filter/None.php @@ -10,6 +10,4 @@ * Represents a filter that matches no items. * Used to indicate that all items should be excluded. */ -final class None implements FilterInterface -{ -} +final class None implements FilterInterface {} diff --git a/src/Reader/Filter/Not.php b/src/Reader/Filter/Not.php index a31c8e20..d601262d 100644 --- a/src/Reader/Filter/Not.php +++ b/src/Reader/Filter/Not.php @@ -16,6 +16,5 @@ final class Not implements FilterInterface */ public function __construct( public readonly FilterInterface $filter, - ) { - } + ) {} } diff --git a/src/Reader/FilterInterface.php b/src/Reader/FilterInterface.php index a979b3a4..c15863f8 100644 --- a/src/Reader/FilterInterface.php +++ b/src/Reader/FilterInterface.php @@ -7,6 +7,4 @@ /** * Filter is a set of settings for modifying data reader criteria. */ -interface FilterInterface -{ -} +interface FilterInterface {} diff --git a/src/Reader/Iterable/Context.php b/src/Reader/Iterable/Context.php index 5810e7e1..851b3999 100644 --- a/src/Reader/Iterable/Context.php +++ b/src/Reader/Iterable/Context.php @@ -18,8 +18,7 @@ public function __construct( */ public readonly array $iterableFilterHandlers, private readonly ValueReaderInterface $valueReader, - ) { - } + ) {} /** * @psalm-param class-string $class diff --git a/src/Reader/Iterable/IterableDataReader.php b/src/Reader/Iterable/IterableDataReader.php index 52e93a8d..3efba9d0 100644 --- a/src/Reader/Iterable/IterableDataReader.php +++ b/src/Reader/Iterable/IterableDataReader.php @@ -193,6 +193,21 @@ public function readOne(): array|object|null ->current(); } + public function getFilter(): FilterInterface + { + return $this->filter; + } + + public function getLimit(): ?int + { + return $this->limit; + } + + public function getOffset(): int + { + return $this->offset; + } + /** * @psalm-return array */ @@ -271,7 +286,7 @@ static function (array|object $itemA, array|object $itemB) use ($criteria) { } return 0; - } + }, ); } @@ -294,8 +309,8 @@ private function prepareFilterHandlers(array $filterHandlers): array sprintf( '%s::withFilterHandlers() accepts instances of %s only.', self::class, - IterableFilterHandlerInterface::class - ) + IterableFilterHandlerInterface::class, + ), ); } $result[$filterHandler->getFilterClass()] = $filterHandler; @@ -318,19 +333,4 @@ private function iterableToArray(iterable $iterable): array { return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; } - - public function getFilter(): FilterInterface - { - return $this->filter; - } - - public function getLimit(): ?int - { - return $this->limit; - } - - public function getOffset(): int - { - return $this->offset; - } } diff --git a/src/Reader/Iterable/ValueReader/PathValueReader.php b/src/Reader/Iterable/ValueReader/PathValueReader.php index d0812a9f..49a5703e 100644 --- a/src/Reader/Iterable/ValueReader/PathValueReader.php +++ b/src/Reader/Iterable/ValueReader/PathValueReader.php @@ -10,8 +10,7 @@ final class PathValueReader implements ValueReaderInterface { public function __construct( private readonly string $delimiter = '.', - ) { - } + ) {} public function read(object|array $item, string $field): mixed { diff --git a/src/Reader/Sort.php b/src/Reader/Sort.php index dddf5717..795b9d21 100644 --- a/src/Reader/Sort.php +++ b/src/Reader/Sort.php @@ -223,7 +223,7 @@ public static function any(array $config = []): self public function withOrderString(string $orderString): self { return $this->withOrder( - OrderHelper::stringToArray($orderString) + OrderHelper::stringToArray($orderString), ); } @@ -331,7 +331,7 @@ public function getDefaultOrder(): array { return array_map( static fn(array $item) => $item['default'], - $this->config + $this->config, ); } } diff --git a/src/Writer/DataWriterException.php b/src/Writer/DataWriterException.php index be631e1d..485ad316 100644 --- a/src/Writer/DataWriterException.php +++ b/src/Writer/DataWriterException.php @@ -13,6 +13,4 @@ * * @psalm-suppress ClassMustBeFinal We assume that the class may be extended in userland. */ -class DataWriterException extends RuntimeException -{ -} +class DataWriterException extends RuntimeException {} diff --git a/src/Writer/DataWriterInterface.php b/src/Writer/DataWriterInterface.php index 97c6f31d..49f1f917 100644 --- a/src/Writer/DataWriterInterface.php +++ b/src/Writer/DataWriterInterface.php @@ -7,6 +7,4 @@ /** * A data writer is able to write or delete data items. */ -interface DataWriterInterface extends WriteableInterface, DeletableInterface -{ -} +interface DataWriterInterface extends WriteableInterface, DeletableInterface {} diff --git a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php index 8d1a3e43..40eb2fd9 100644 --- a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php +++ b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php @@ -33,7 +33,7 @@ public static function dataWithReader(): array public function testWithReader( string $field, mixed $value, - bool|null $caseSensitive, + ?bool $caseSensitive, array $expectedFixtureIndexes, ): void { $reader = $this->getReader()->withFilter(new Like($field, $value, $caseSensitive)); @@ -63,7 +63,7 @@ public static function dataWithReaderAndMode(): array public function testWithReaderAndMode( string $field, string $value, - bool|null $caseSensitive, + ?bool $caseSensitive, LikeMode $mode, array $expectedFixtureIndexes, ): void { diff --git a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php index 9baf6fb7..20dcc2f5 100644 --- a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php +++ b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php @@ -45,7 +45,7 @@ public static function dataWithReader(): array #[DataProvider('dataWithReader')] public function testWithReader(Not $filter, array $expectedFixtureNumbers): void { - $expectedFixtureIndexes = array_map(static fn (int $number): int => $number - 1, $expectedFixtureNumbers); + $expectedFixtureIndexes = array_map(static fn(int $number): int => $number - 1, $expectedFixtureNumbers); $this->assertFixtures( $expectedFixtureIndexes, $this->getReader()->withFilter($filter)->read(), diff --git a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithOrXTestCase.php b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithOrXTestCase.php index dfe2fc94..234c7cba 100644 --- a/tests/Common/Reader/ReaderWithFilter/BaseReaderWithOrXTestCase.php +++ b/tests/Common/Reader/ReaderWithFilter/BaseReaderWithOrXTestCase.php @@ -30,7 +30,7 @@ public function testNested(): void new OrX( new AndX(new GreaterThan('balance', 500), new LessThan('number', 5)), new Like('email', 'st'), - ) + ), ); $this->assertFixtures([3, 4], $reader->read()); } diff --git a/tests/Paginator/KeysetPaginatorTest.php b/tests/Paginator/KeysetPaginatorTest.php index 8250c22b..0d522bf9 100644 --- a/tests/Paginator/KeysetPaginatorTest.php +++ b/tests/Paginator/KeysetPaginatorTest.php @@ -100,7 +100,7 @@ public function testDataReaderWithLimit(): void public function testDataReaderWithoutLimitableInterface(): void { - $dataReader = new class () implements ReadableDataInterface, SortableDataInterface, FilterableDataInterface { + $dataReader = new class implements ReadableDataInterface, SortableDataInterface, FilterableDataInterface { public function withSort(?Sort $sort): static { return clone $this; @@ -367,7 +367,7 @@ public static function dataReadOne(): array $data['empty'] = [ null, new KeysetPaginator( - (new IterableDataReader([]))->withSort(Sort::only(['id'])->withOrderString('id')) + (new IterableDataReader([]))->withSort(Sort::only(['id'])->withOrderString('id')), ), ]; @@ -378,7 +378,7 @@ public static function dataReadOne(): array ['id' => 1, 'name' => 'Mike'], ['id' => 2, 'name' => 'John'], ])) - ->withSort(Sort::only(['id'])->withOrderString('-id')) + ->withSort(Sort::only(['id'])->withOrderString('-id')), ), ]; @@ -628,132 +628,6 @@ public function testCustomPageSize(): void $this->assertCount(2, $paginator->read()); } - private static function getDataSet(?array $keys = null): array - { - if ($keys === null) { - return self::DEFAULT_DATASET; - } - - $result = []; - - foreach ($keys as $key) { - $result[] = self::DEFAULT_DATASET[$key]; - } - - return $result; - } - - private function getNonSortableDataReader() - { - return new class () implements ReadableDataInterface, LimitableDataInterface, FilterableDataInterface { - public function withLimit(?int $limit): static - { - return clone $this; - } - - public function read(): iterable - { - return []; - } - - public function readOne(): array|object|null - { - return null; - } - - public function withFilter(?FilterInterface $filter): static - { - return clone $this; - } - - public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static - { - return clone $this; - } - - public function getFilter(): FilterInterface - { - return new All(); - } - - public function getLimit(): int - { - return 0; - } - }; - } - - private function getNonFilterableDataReader() - { - return new class () implements ReadableDataInterface, LimitableDataInterface, SortableDataInterface { - public function withLimit(?int $limit): static - { - return clone $this; - } - - public function read(): iterable - { - return []; - } - - public function readOne(): array|object|null - { - return null; - } - - public function withSort(?Sort $sort): static - { - return clone $this; - } - - public function getSort(): ?Sort - { - return Sort::only([]); - } - - public function getLimit(): int - { - return 0; - } - }; - } - - private function createObjectWithPublicProperties(int $id, string $name): stdClass - { - $object = new stdClass(); - $object->id = $id; - $object->name = $name; - - return $object; - } - - private function createObjectWithGetters(int $id, string $name): object - { - return new class ($id, $name) { - private int $createdAt; - - public function __construct(private int $id, private string $name, ?int $createdAt = null) - { - $this->createdAt = $createdAt ?: time(); - } - - public function getId(): string - { - return (string) $this->id; - } - - public function getName(): string - { - return $this->name; - } - - public function getCreatedAt(): int - { - return $this->createdAt; - } - }; - } - public function testGetSort(): void { $sort = Sort::only(['id'])->withOrderString('id'); @@ -875,7 +749,7 @@ public function testFilterCallback(): void static function ($item) { $item['id']--; return $item; - } + }, ))->withSort(Sort::only(['id'])->withOrderString('id')); $paginator = (new KeysetPaginator($dataReader)) ->withPageSize(2) @@ -883,13 +757,13 @@ static function ($item) { ->withFilterCallback( static function ( GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual $filter, - KeysetFilterContext $context + KeysetFilterContext $context, ): FilterInterface { if ($context->field === 'id') { - $filter = $filter->withValue((string)($context->value + 1)); + $filter = $filter->withValue((string) ($context->value + 1)); } return $filter; - } + }, ); $this->assertSame( @@ -903,7 +777,7 @@ static function ( 'name' => 'Agent J', ], ], - array_values($paginator->read()) + array_values($paginator->read()), ); } @@ -914,7 +788,7 @@ public function testFilterCallbackExtended(): void static function ($item) { $item['id']--; return $item; - } + }, ))->withSort(Sort::only(['id'])->withOrderString('id')); $paginator = (new KeysetPaginator($dataReader)) ->withPageSize(2) @@ -922,10 +796,10 @@ static function ($item) { ->withFilterCallback( static function ( GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual $filter, - KeysetFilterContext $context + KeysetFilterContext $context, ): FilterInterface { $value = $context->field === 'id' - ? (string)($context->value + 1) + ? (string) ($context->value + 1) : $context->value; if ($context->isReverse) { @@ -939,7 +813,7 @@ static function ( } return $filter; - } + }, ); $this->assertSame( @@ -953,7 +827,7 @@ static function ( 'name' => 'Codename Doris', ], ], - array_values($paginator->read()) + array_values($paginator->read()), ); } @@ -966,7 +840,7 @@ public function testFilterCallbackWithReverse(): void ->withFilterCallback( static function ( GreaterThan|LessThan|GreaterThanOrEqual|LessThanOrEqual $filter, - KeysetFilterContext $context + KeysetFilterContext $context, ): FilterInterface { if ($context->isReverse) { return $context->sorting === SORT_ASC @@ -976,7 +850,7 @@ static function ( return $context->sorting === SORT_ASC ? new GreaterThan($context->field, $context->value) : new LessThan($context->field, $context->value); - } + }, ); $this->assertTrue($paginator->isOnFirstPage()); @@ -1026,7 +900,7 @@ public function testPageTypeWithPreviousPageToken( bool $expectedIsOnLastPage, array $expectedIds, string $token, - bool $isReverseOrder = false + bool $isReverseOrder = false, ): void { $data = [ ['id' => 10], @@ -1089,7 +963,7 @@ public function testPageTypeWithNextPageToken( bool $expectedIsOnLastPage, array $expectedIds, string $token, - bool $isReverseOrder = false + bool $isReverseOrder = false, ): void { $data = [ ['id' => 10], @@ -1262,4 +1136,130 @@ public function testGetFilter(): void $this->assertSame($filter, $paginator->getFilter()); } + + private static function getDataSet(?array $keys = null): array + { + if ($keys === null) { + return self::DEFAULT_DATASET; + } + + $result = []; + + foreach ($keys as $key) { + $result[] = self::DEFAULT_DATASET[$key]; + } + + return $result; + } + + private function getNonSortableDataReader() + { + return new class implements ReadableDataInterface, LimitableDataInterface, FilterableDataInterface { + public function withLimit(?int $limit): static + { + return clone $this; + } + + public function read(): iterable + { + return []; + } + + public function readOne(): array|object|null + { + return null; + } + + public function withFilter(?FilterInterface $filter): static + { + return clone $this; + } + + public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static + { + return clone $this; + } + + public function getFilter(): FilterInterface + { + return new All(); + } + + public function getLimit(): int + { + return 0; + } + }; + } + + private function getNonFilterableDataReader() + { + return new class implements ReadableDataInterface, LimitableDataInterface, SortableDataInterface { + public function withLimit(?int $limit): static + { + return clone $this; + } + + public function read(): iterable + { + return []; + } + + public function readOne(): array|object|null + { + return null; + } + + public function withSort(?Sort $sort): static + { + return clone $this; + } + + public function getSort(): ?Sort + { + return Sort::only([]); + } + + public function getLimit(): int + { + return 0; + } + }; + } + + private function createObjectWithPublicProperties(int $id, string $name): stdClass + { + $object = new stdClass(); + $object->id = $id; + $object->name = $name; + + return $object; + } + + private function createObjectWithGetters(int $id, string $name): object + { + return new class ($id, $name) { + private int $createdAt; + + public function __construct(private int $id, private string $name, ?int $createdAt = null) + { + $this->createdAt = $createdAt ?: time(); + } + + public function getId(): string + { + return (string) $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getCreatedAt(): int + { + return $this->createdAt; + } + }; + } } diff --git a/tests/Paginator/OffsetPaginatorTest.php b/tests/Paginator/OffsetPaginatorTest.php index 320251e2..1be4a3c4 100644 --- a/tests/Paginator/OffsetPaginatorTest.php +++ b/tests/Paginator/OffsetPaginatorTest.php @@ -57,7 +57,7 @@ final class OffsetPaginatorTest extends TestCase public function testDataReaderWithoutOffsetableInterface(): void { - $nonOffsetableDataReader = new class () implements ReadableDataInterface, CountableDataInterface { + $nonOffsetableDataReader = new class implements ReadableDataInterface, CountableDataInterface { public function withLimit(int $limit): static { // do nothing @@ -83,7 +83,7 @@ public function count(): int $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage(sprintf( 'Data reader should implement "%s" in order to be used with offset paginator.', - OffsetableDataInterface::class + OffsetableDataInterface::class, )); new OffsetPaginator($nonOffsetableDataReader); @@ -91,7 +91,7 @@ public function count(): int public function testDataReaderWithoutCountableInterface(): void { - $nonCountableDataReader = new class () implements ReadableDataInterface, OffsetableDataInterface { + $nonCountableDataReader = new class implements ReadableDataInterface, OffsetableDataInterface { public function withLimit(int $limit): static { // do nothing @@ -136,7 +136,7 @@ public function getOffset(): int public function testDataReaderWithoutLimitableInterface(): void { - $nonLimitableDataReader = new class () implements + $nonLimitableDataReader = new class implements ReadableDataInterface, CountableDataInterface, OffsetableDataInterface { @@ -172,7 +172,7 @@ public function getOffset(): int sprintf( 'Data reader should implement "%s" in order to be used with offset paginator.', LimitableDataInterface::class, - ) + ), ); new OffsetPaginator($nonLimitableDataReader); @@ -327,7 +327,7 @@ public static function dataReadOne(): array new IterableDataReader([ ['id' => 1, 'name' => 'Mike'], ['id' => 2, 'name' => 'John'], - ]) + ]), ))->withPageSize(1)->withCurrentPage(2), ]; diff --git a/tests/Paginator/PageTokenAssertTrait.php b/tests/Paginator/PageTokenAssertTrait.php index 7f395a1d..c34e7418 100644 --- a/tests/Paginator/PageTokenAssertTrait.php +++ b/tests/Paginator/PageTokenAssertTrait.php @@ -18,7 +18,7 @@ public function assertPageToken( string $expectedValue, bool $expectedIsPrevious, mixed $pageToken, - string $message = '' + string $message = '', ): void { static::assertThat($pageToken, new IsInstanceOf(PageToken::class), $message); static::assertThat($pageToken->value, new IsIdentical($expectedValue), $message); diff --git a/tests/Reader/Iterable/FilterHandler/LikeHandlerTest.php b/tests/Reader/Iterable/FilterHandler/LikeHandlerTest.php index 87beedae..058d01eb 100644 --- a/tests/Reader/Iterable/FilterHandler/LikeHandlerTest.php +++ b/tests/Reader/Iterable/FilterHandler/LikeHandlerTest.php @@ -111,7 +111,7 @@ public function testMatchWithMode( $this->assertSame( $expected, - $handler->match($item, $filter, $context) + $handler->match($item, $filter, $context), ); } diff --git a/tests/Reader/Iterable/IterableDataReaderTest.php b/tests/Reader/Iterable/IterableDataReaderTest.php index c64f88b9..c0a3a452 100644 --- a/tests/Reader/Iterable/IterableDataReaderTest.php +++ b/tests/Reader/Iterable/IterableDataReaderTest.php @@ -78,7 +78,7 @@ public function testImmutability(): void public function testExceptionOnPassingNonIterableFilters(): void { - $nonIterableFilterHandler = new class () implements FilterHandlerInterface { + $nonIterableFilterHandler = new class implements FilterHandlerInterface { public function getFilterClass(): string { return '?'; @@ -89,7 +89,7 @@ public function getFilterClass(): string $message = sprintf( '%s::withFilterHandlers() accepts instances of %s only.', IterableDataReader::class, - IterableFilterHandlerInterface::class + IterableFilterHandlerInterface::class, ); $this->expectExceptionMessage($message); @@ -129,7 +129,7 @@ public function testOffsetIsApplied(): void 3 => self::ITEM_4, 4 => self::ITEM_5, ], - $data + $data, ); $this->assertSame(2, $reader->getOffset()); } @@ -312,7 +312,7 @@ public function testOrXFiltering(): void { $filter = new OrX( new Equals('id', 1), - new Equals('id', 2) + new Equals('id', 2), ); $reader = (new IterableDataReader(self::DEFAULT_DATASET)) ->withFilter($filter); @@ -327,7 +327,7 @@ public function testAndXFiltering(): void { $filter = new AndX( new GreaterThan('id', 3), - new Like('name', 'Agent') + new Like('name', 'Agent'), ); $reader = (new IterableDataReader(self::DEFAULT_DATASET)) ->withFilter($filter); @@ -341,7 +341,7 @@ public function testLimitedSort(): void { $readerMin = (new IterableDataReader(self::DEFAULT_DATASET)) ->withSort( - Sort::only(['id'])->withOrder(['id' => 'asc']) + Sort::only(['id'])->withOrder(['id' => 'asc']), ) ->withLimit(1); $min = $readerMin->read()[0]['id']; @@ -349,7 +349,7 @@ public function testLimitedSort(): void $readerMax = (new IterableDataReader(self::DEFAULT_DATASET)) ->withSort( - Sort::only(['id'])->withOrder(['id' => 'desc']) + Sort::only(['id'])->withOrder(['id' => 'desc']), ) ->withLimit(1); $max = $readerMax->readOne()['id']; @@ -420,7 +420,7 @@ public function testCustomEqualsProcessor(): void $dataReader = (new IterableDataReader(self::DEFAULT_DATASET)) ->withSort($sort) ->withAddedFilterHandlers( - new class () implements IterableFilterHandlerInterface { + new class implements IterableFilterHandlerInterface { public function getFilterClass(): string { return Equals::class; @@ -434,7 +434,7 @@ public function match( /** @var Equals $filter */ return $item[$filter->field] === 2; } - } + }, ); $dataReader = $dataReader->withFilter(new Equals('id', 100)); @@ -457,13 +457,13 @@ public function testNotSupportedFilter(): void public function testArrayOfObjects(): void { $data = [ - 'one' => new class () { + 'one' => new class { public int $a = 1; }, - 'two' => new class () { + 'two' => new class { public int $a = 2; }, - 'three' => new class () { + 'three' => new class { public int $a = 3; }, ]; @@ -488,7 +488,7 @@ public function testSortingWithSameValues(): void $reader = (new IterableDataReader($data)) ->withSort( - Sort::any()->withOrder(['value' => 'asc']) + Sort::any()->withOrder(['value' => 'asc']), ); $this->assertSame( @@ -498,7 +498,7 @@ public function testSortingWithSameValues(): void 3 => ['value' => 2], 2 => ['value' => 3], ], - $reader->read() + $reader->read(), ); } @@ -522,7 +522,7 @@ public function testWithLimitZero(): void 2 => ['value' => 3], 3 => ['value' => 2], ], - $reader->read() + $reader->read(), ); } diff --git a/tests/Reader/Iterable/ReaderWithFilter/ReaderWithOrXTest.php b/tests/Reader/Iterable/ReaderWithFilter/ReaderWithOrXTest.php index 8345bb5e..de745ca1 100644 --- a/tests/Reader/Iterable/ReaderWithFilter/ReaderWithOrXTest.php +++ b/tests/Reader/Iterable/ReaderWithFilter/ReaderWithOrXTest.php @@ -23,7 +23,7 @@ public function testNested(): void new OrX( new AndX(new GreaterThan('balance', 500), new LessThan('number', 5)), new Like('email', 'st'), - ) + ), ); $this->assertFixtures([3, 4], $reader->read()); } diff --git a/tests/Reader/SortTest.php b/tests/Reader/SortTest.php index 8c9dcd2b..e52f9490 100644 --- a/tests/Reader/SortTest.php +++ b/tests/Reader/SortTest.php @@ -249,7 +249,7 @@ public function testWithoutDefaultSortingWhenFormingCriteria(): void ->withOrder( [ 'b' => 'desc', - ] + ], ) ->withoutDefaultSorting(); @@ -257,7 +257,7 @@ public function testWithoutDefaultSortingWhenFormingCriteria(): void [ 'bee' => SORT_DESC, ], - $sort->getCriteria() + $sort->getCriteria(), ); } @@ -272,7 +272,7 @@ public function testIgnoreExtraFields(): void 'a' => SORT_DESC, 'b' => SORT_DESC, ], - $sort->getCriteria() + $sort->getCriteria(), ); } diff --git a/tests/Support/Car.php b/tests/Support/Car.php index 77667069..a5518da2 100644 --- a/tests/Support/Car.php +++ b/tests/Support/Car.php @@ -8,8 +8,7 @@ final class Car { public function __construct( private ?int $number, - ) { - } + ) {} public function getNumber(): ?int { diff --git a/tests/Support/CustomFilter/Digital.php b/tests/Support/CustomFilter/Digital.php index 0b1cb3c5..e0a3c66d 100644 --- a/tests/Support/CustomFilter/Digital.php +++ b/tests/Support/CustomFilter/Digital.php @@ -8,7 +8,5 @@ final class Digital implements FilterInterface { - public function __construct(public readonly string $field) - { - } + public function __construct(public readonly string $field) {} } diff --git a/tests/Support/CustomFilter/FilterWithoutHandler.php b/tests/Support/CustomFilter/FilterWithoutHandler.php index e507f0d9..5f384df3 100644 --- a/tests/Support/CustomFilter/FilterWithoutHandler.php +++ b/tests/Support/CustomFilter/FilterWithoutHandler.php @@ -6,6 +6,4 @@ use Yiisoft\Data\Reader\FilterInterface; -final class FilterWithoutHandler implements FilterInterface -{ -} +final class FilterWithoutHandler implements FilterInterface {} diff --git a/tests/Support/MutationDataReader.php b/tests/Support/MutationDataReader.php index 7f969d06..e8ef7da8 100644 --- a/tests/Support/MutationDataReader.php +++ b/tests/Support/MutationDataReader.php @@ -23,8 +23,7 @@ final class MutationDataReader implements public function __construct( private IterableDataReader $decorated, private Closure $mutation, - ) { - } + ) {} public function withFilter(FilterInterface $filter): static { diff --git a/tests/Support/StringableValue.php b/tests/Support/StringableValue.php index 2d61b5f1..8873bb35 100644 --- a/tests/Support/StringableValue.php +++ b/tests/Support/StringableValue.php @@ -10,8 +10,7 @@ final class StringableValue implements Stringable { public function __construct( private readonly string $value, - ) { - } + ) {} public function __toString(): string { diff --git a/tests/TestCase.php b/tests/TestCase.php index 09b11a26..fec140fc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,7 +19,7 @@ public static function invalidArrayValueDataProvider(): array return [ 'bool-true' => [true], 'bool-false' => [false], - 'callback' => [fn () => null], + 'callback' => [fn() => null], 'float' => [1.0], 'int' => [1], 'null' => [null], @@ -31,7 +31,7 @@ public static function invalidArrayValueDataProvider(): array public static function invalidFilterDataProvider(): array { return [ - 'callback' => [fn () => null], + 'callback' => [fn() => null], 'float' => [1.0], 'int' => [1], 'null' => [null], @@ -44,7 +44,7 @@ public static function invalidFilterOperatorDataProvider(): array { return [ 'array' => [[[]]], - 'callback' => [[fn () => null]], + 'callback' => [[fn() => null]], 'empty-string' => [['']], 'float' => [[1.0]], 'int' => [[1]], @@ -57,7 +57,7 @@ public static function invalidScalarValueDataProvider(): array { return [ 'array' => [[]], - 'callback' => [fn () => null], + 'callback' => [fn() => null], 'null' => [null], 'object' => [new stdClass()], ];