Skip to content

Commit be7b3be

Browse files
committed
Fix parameters order + Aggregation's SortBy option + more UT
1 parent c4eb7e6 commit be7b3be

21 files changed

+669
-76
lines changed

.phan/config.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@
260260
//
261261
// This is useful for excluding hopelessly unanalyzable
262262
// files that can't be removed for whatever reason.
263-
'exclude_file_list' => [
264-
'src/Kernel.php'
265-
],
263+
'exclude_file_list' => [],
266264

267265
// A directory list that defines files that will be excluded
268266
// from static analysis, but whose class and method
@@ -277,7 +275,6 @@
277275
// to `exclude_analysis_directory_list`.
278276
'exclude_analysis_directory_list' => [
279277
'vendor/',
280-
'src/DataFixtures/',
281278
],
282279

283280
// Enable this to enable checks of require/include statements referring to valid paths.

.php_cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,6 @@ return PhpCsFixer\Config::create()
270270
->exclude('vendor')
271271
->in(__DIR__.'/src')
272272
->in(__DIR__.'/alias')
273+
->in(__DIR__.'/tests')
273274
)
274275
;

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Shorthand for Reducer functions
1212
- Add **Deprecated** section in `CHANGELOG.md`
1313
- Add Redis pipeline support
14+
- Prefix properties name with `@` in Aggregation's SortBy (keep BC)
15+
- Add more unit tests (Aggregation options, Field creation)
1416

1517
### Changed
1618

1719
- Allow `GROUPBY` with no properties
1820
- Allow `GROUPBY` with no reducers
1921
- (dev) Replace `php-parallel-lint` lib.
22+
- (dev) Add Php-Cs-Fixer on `tests/`
23+
- (dev) Improve Makefile
2024

2125
### Fixed
2226

2327
- Fix namespace of tests
2428
- Aggregation result can now be an array
2529
- Fix Index builder options not preserved ([PR#3])
30+
- Aggregation SortBy allow 0 (not limit) as value for Max
31+
- Fix helper removing _"nullish"_ values (`array_filter` is too lax by default)
32+
- Fix order in fields creation not being correct
2633

2734
### Deprecated
2835

Makefile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.PHONY: analyze fix-code test coverage
22

33
analyze: | vendor
4-
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
54
$(COMPOSER) exec -v parallel-lint -- src
65
$(COMPOSER) exec -v php-cs-fixer -- fix --dry-run
76
$(COMPOSER) exec -v unused_scanner -- .unused.php
@@ -13,8 +12,7 @@ analyze: | vendor
1312
$(COMPOSER) exec -v psalm -- --show-info=true src
1413
$(COMPOSER) exec -v phan -- --allow-polyfill-parser --color --color-scheme=light --output-mode=text
1514

16-
fix-code: | vendor
17-
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
15+
fix-code: |vendor
1816
$(COMPOSER) normalize
1917
$(COMPOSER) exec -v php-cs-fixer -- fix
2018
@#$(COMPOSER) exec -v psalm -- --alter --issues=all src
@@ -23,14 +21,12 @@ test: | vendor
2321
$(COMPOSER) exec -v phpunit
2422

2523
coverage: | vendor
24+
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
2625
$(COMPOSER) exec -v phpunit -- --coverage-text --color
2726

28-
vendor:
29-
ifneq (prod,${BUILD_MODE})
30-
$(COMPOSER) install --optimize-autoloader
31-
else
32-
APP_ENV=prod $(COMPOSER) install --optimize-autoloader --no-dev --no-suggest --prefer-dist
33-
endif
27+
vendor: composer.json
28+
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
29+
touch vendor
3430

3531
composer.phar:
3632
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

src/Aggregate/Exception/NotEnoughPropertiesException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
use OutOfRangeException;
2525
use Throwable;
2626

27-
/** @deprecated This exception is never used. */
27+
/**
28+
* @codeCoverageIgnore
29+
*
30+
* @deprecated This exception is never used.
31+
*/
2832
class NotEnoughPropertiesException extends OutOfRangeException
2933
{
3034
public function __construct(string $message = 'You must have at least one property', int $code = 0, ?Throwable $previous = null)

src/Aggregate/Exception/NotEnoughReducersException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
use OutOfRangeException;
2525
use Throwable;
2626

27-
/** @deprecated This exception is never used. */
27+
/**
28+
* @codeCoverageIgnore
29+
*
30+
* @deprecated This exception is never used.
31+
*/
2832
class NotEnoughReducersException extends OutOfRangeException
2933
{
3034
public function __construct(string $message = 'You must have at least one reducer', int $code = 0, ?Throwable $previous = null)

src/Aggregate/SortBy.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use MacFJA\RediSearch\Helper\DataHelper;
3131
use MacFJA\RediSearch\Helper\RedisHelper;
3232
use OutOfRangeException;
33+
use function strpos;
3334
use Throwable;
3435

3536
class SortBy implements \MacFJA\RediSearch\PartialQuery
@@ -66,7 +67,7 @@ public function __construct(array $properties, ?int $max = null)
6667
new UnknownSortDirectionException(array_diff($allValues, ['ASC', 'DESC']))
6768

6869
);
69-
DataHelper::assert(null === $max || $max > 0, new OutOfRangeException('MAX such by greater than 0'));
70+
DataHelper::assert(null === $max || $max >= 0, new OutOfRangeException('MAX such by greater or equals to 0'));
7071
$this->properties = $properties;
7172
$this->max = $max;
7273
}
@@ -75,6 +76,9 @@ public function getQueryParts(): array
7576
{
7677
$query = ['SORTBY', count($this->properties) * 2];
7778
foreach ($this->properties as $property => $direction) {
79+
if (!(0 === strpos($property, '@'))) {
80+
$property = '@'.$property;
81+
}
7882
$query[] = $property;
7983
$query[] = $direction;
8084
}

src/Helper/RedisHelper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RedisHelper
4747
*/
4848
public static function buildQueryBoolean(array $query, array $data): array
4949
{
50-
return array_merge($query, array_keys(array_filter($data, function ($item) {
50+
return array_merge($query, array_keys(array_filter($data, function (bool $item): bool {
5151
return $item;
5252
})));
5353
}
@@ -65,6 +65,9 @@ public static function buildQueryBoolean(array $query, array $data): array
6565
public static function buildQueryList(array $query, array $data, bool $allowEmpty = false): array
6666
{
6767
foreach ($data as $queryWord => $list) {
68+
if (!is_array($list)) {
69+
continue;
70+
}
6871
if (true === $allowEmpty || count($list) > 0) {
6972
$query[] = $queryWord;
7073
$query[] = count($list);
@@ -80,10 +83,15 @@ public static function buildQueryList(array $query, array $data, bool $allowEmpt
8083
* @param array<string, null|mixed> $data
8184
*
8285
* @return array<float|int|string>
86+
*
87+
* @phan-suppress PhanPartialTypeMismatchReturn
8388
*/
8489
public static function buildQueryNotNull($query, array $data): array
8590
{
86-
foreach (array_filter($data) as $queryWord => $value) {
91+
$notNullValues = array_filter($data, function ($value): bool {
92+
return !(null === $value);
93+
});
94+
foreach ($notNullValues as $queryWord => $value) {
8795
$query[] = $queryWord;
8896
$query[] = $value;
8997
}
@@ -101,6 +109,9 @@ public static function buildQueryPartial(array $query, array $partials): array
101109
{
102110
/** @var PartialQuery $partial */
103111
foreach (array_filter($partials) as $partial) {
112+
if (!$partial instanceof PartialQuery) {
113+
continue;
114+
}
104115
$query = array_merge($query, $partial->getQueryParts());
105116
}
106117

src/Index/Builder/AbstractField.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace MacFJA\RediSearch\Index\Builder;
2323

24+
use function array_merge;
2425
use function implode;
2526
use MacFJA\RediSearch\Helper\RedisHelper;
2627
use SGH\Comparable\ComparatorException;
@@ -80,7 +81,10 @@ public function getName(): string
8081

8182
public function getQueryParts(): array
8283
{
83-
$query = [$this->name, $this->getType()];
84+
$query = array_merge(
85+
[$this->name, $this->getType()],
86+
$this->getAdditionalQueryParts()
87+
);
8488

8589
return RedisHelper::buildQueryBoolean($query, [
8690
'SORTABLE' => $this->sortable,
@@ -96,4 +100,12 @@ public function compareTo($object)
96100

97101
return implode(' ', $object->getQueryParts()) <=> implode(' ', $this->getQueryParts());
98102
}
103+
104+
/**
105+
* @return array<float|int|string>
106+
*/
107+
protected function getAdditionalQueryParts(): array
108+
{
109+
return [];
110+
}
99111
}

src/Index/Builder/TagField.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ public function getSeparator(): ?string
5151
return $this->separator;
5252
}
5353

54-
public function getQueryParts(): array
54+
protected function getAdditionalQueryParts(): array
5555
{
56-
$query = parent::getQueryParts();
57-
58-
return RedisHelper::buildQueryNotNull($query, [
56+
return RedisHelper::buildQueryNotNull([], [
5957
'SEPARATOR' => $this->separator,
6058
]);
6159
}

0 commit comments

Comments
 (0)