Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81c1d7f

Browse files
committedOct 26, 2021
Remove unnecessary 'transformParsedResponse' function
1 parent e108895 commit 81c1d7f

13 files changed

+212
-74
lines changed
 
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\Exception;
23+
24+
use function is_string;
25+
use UnexpectedValueException;
26+
27+
class UnexpectedServerResponseException extends UnexpectedValueException
28+
{
29+
/** @var mixed */
30+
private $response;
31+
32+
/**
33+
* @param mixed $response
34+
*/
35+
public function __construct($response, ?string $message = null)
36+
{
37+
$this->response = $response;
38+
$additional = '';
39+
if (is_string($message)) {
40+
$additional = ': '.$message;
41+
}
42+
parent::__construct('Unexpected response from the Redis server'.$additional);
43+
}
44+
45+
/**
46+
* @codeCoverageIgnore
47+
*
48+
* @return mixed
49+
*/
50+
public function getResponse()
51+
{
52+
return $this->response;
53+
}
54+
}

‎src/Redis/Command/AbstractCommand.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ public function setRediSearchVersion(string $rediSearchVersion): Command
6363
return $this;
6464
}
6565

66-
/**
67-
* @param array<mixed>|float|int|mixed|string $data
68-
*
69-
* @return mixed|string
70-
*/
71-
public function parseResponse($data)
72-
{
73-
return $this->transformParsedResponse($data);
74-
}
75-
7666
/**
7767
* @return array<float|int|string>
7868
*/
@@ -99,20 +89,22 @@ public function getArguments(): array
9989
}
10090

10191
/**
102-
* @return array<string>
103-
*/
104-
abstract protected function getRequiredOptions(): array;
105-
106-
/**
107-
* @param mixed $data
92+
* @codeCoverageIgnore
93+
*
94+
* @param array<mixed>|float|int|mixed|string $data
10895
*
10996
* @return mixed
11097
*/
111-
protected function transformParsedResponse($data)
98+
public function parseResponse($data)
11299
{
113100
return $data;
114101
}
115102

103+
/**
104+
* @return array<string>
105+
*/
106+
abstract protected function getRequiredOptions(): array;
107+
116108
/**
117109
* @return array<string>
118110
*/

‎src/Redis/Command/Aggregate.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace MacFJA\RediSearch\Redis\Command;
2323

2424
use function assert;
25+
use function is_array;
26+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2527
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ApplyOption;
2628
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
2729
use MacFJA\RediSearch\Redis\Command\AggregateCommand\LimitOption;
@@ -33,10 +35,11 @@
3335
use MacFJA\RediSearch\Redis\Command\Option\NumberedOption;
3436
use MacFJA\RediSearch\Redis\Response\AggregateResponseItem;
3537
use MacFJA\RediSearch\Redis\Response\ArrayResponseTrait;
38+
use MacFJA\RediSearch\Redis\Response\CursorResponse;
3639
use MacFJA\RediSearch\Redis\Response\PaginatedResponse;
3740

3841
/**
39-
* @method CursorResponse|PaginatedResponse<AggregateResponseItem> parseResponse(mixed $data)
42+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4043
*/
4144
class Aggregate extends AbstractCommand implements PaginatedCommand
4245
{
@@ -180,13 +183,18 @@ public function getSize(): ?int
180183
return $limit->getSize();
181184
}
182185

183-
protected function getRequiredOptions(): array
186+
/**
187+
* @param array|mixed|string $data
188+
*
189+
* @return CursorResponse|PaginatedResponse<AggregateResponseItem>
190+
* @phpstan-return CursorResponse|PaginatedResponse
191+
*/
192+
public function parseResponse($data)
184193
{
185-
return ['index', 'query'];
186-
}
194+
if (!is_array($data)) {
195+
throw new UnexpectedServerResponseException($data);
196+
}
187197

188-
protected function transformParsedResponse($data)
189-
{
190198
if (true === $this->options['cursor']->getDataOfOption('enabled')) {
191199
return CursorRead::transformResponse(
192200
$data,
@@ -206,4 +214,9 @@ protected function transformParsedResponse($data)
206214

207215
return new PaginatedResponse($this, $totalCount, $items);
208216
}
217+
218+
protected function getRequiredOptions(): array
219+
{
220+
return ['index', 'query'];
221+
}
209222
}

‎src/Redis/Command/CursorRead.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@
2525
use function count;
2626
use function is_array;
2727
use function is_int;
28+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2829
use MacFJA\RediSearch\Redis\Command\Option\FlagOption;
2930
use MacFJA\RediSearch\Redis\Command\Option\NamedOption;
3031
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
3132
use MacFJA\RediSearch\Redis\Response\AggregateResponseItem;
3233
use MacFJA\RediSearch\Redis\Response\ArrayResponseTrait;
3334
use MacFJA\RediSearch\Redis\Response\CursorResponse;
3435

35-
/**
36-
* @method CursorResponse parseResponse(mixed $data)
37-
*/
3836
class CursorRead extends AbstractCommand
3937
{
4038
use ArrayResponseTrait;
@@ -102,18 +100,27 @@ public static function transformResponse(array $data, ?int $count, string $index
102100
return new CursorResponse($cursorId, $totalCount, $items, $size, $index, $rediSearchVersion);
103101
}
104102

105-
protected function getRequiredOptions(): array
103+
/**
104+
* @param array|mixed $data
105+
*
106+
* @return CursorResponse
107+
*/
108+
public function parseResponse($data)
106109
{
107-
return ['type', 'index', 'cursor'];
108-
}
110+
if (!is_array($data)) {
111+
throw new UnexpectedServerResponseException($data);
112+
}
109113

110-
protected function transformParsedResponse($data)
111-
{
112114
return self::transformResponse(
113115
$data,
114116
$this->options['count']->getValue(),
115117
$this->options['index']->getValue(),
116118
$this->getRediSearchVersion()
117119
);
118120
}
121+
122+
protected function getRequiredOptions(): array
123+
{
124+
return ['type', 'index', 'cursor'];
125+
}
119126
}

‎src/Redis/Command/Info.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121

2222
namespace MacFJA\RediSearch\Redis\Command;
2323

24+
use function is_array;
25+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2426
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
2527
use MacFJA\RediSearch\Redis\Response\InfoResponse;
2628

27-
/**
28-
* @method InfoResponse parseResponse(mixed $data)
29-
*/
3029
class Info extends AbstractCommand
3130
{
3231
public function __construct(string $rediSearchVersion = self::MIN_IMPLEMENTED_VERSION)
@@ -48,13 +47,22 @@ public function getId(): string
4847
return 'FT.INFO';
4948
}
5049

51-
protected function getRequiredOptions(): array
50+
/**
51+
* @param array|mixed $data
52+
*
53+
* @return InfoResponse
54+
*/
55+
public function parseResponse($data)
5256
{
53-
return ['index'];
57+
if (!is_array($data)) {
58+
throw new UnexpectedServerResponseException($data);
59+
}
60+
61+
return new InfoResponse($data);
5462
}
5563

56-
protected function transformParsedResponse($data)
64+
protected function getRequiredOptions(): array
5765
{
58-
return new InfoResponse($data);
66+
return ['index'];
5967
}
6068
}

‎src/Redis/Command/Search.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
use function assert;
2525
use function count;
26-
use InvalidArgumentException;
2726
use function is_array;
27+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2828
use MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption as CV;
2929
use MacFJA\RediSearch\Redis\Command\Option\FlagOption;
3030
use MacFJA\RediSearch\Redis\Command\Option\NamedOption;
@@ -302,15 +302,15 @@ public function getSize(): ?int
302302
/**
303303
* @param mixed $data
304304
*
305-
* @return mixed|PaginatedResponse
305+
* @return PaginatedResponse
306306
*
307307
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
308308
* @SuppressWarnings(PHPMD.NPathComplexity)
309309
*/
310-
public function transformParsedResponse($data)
310+
public function parseResponse($data)
311311
{
312312
if (!is_array($data)) {
313-
return $data;
313+
throw new UnexpectedServerResponseException($data);
314314
}
315315

316316
$totalCount = array_shift($data);
@@ -329,7 +329,7 @@ public function transformParsedResponse($data)
329329

330330
$documents = array_chunk($data, $chunkSize);
331331

332-
$items = array_map(static function ($document) use ($useSortKeys, $usePayloads, $useScores, $noContent) {
332+
$items = array_map(static function ($document) use ($data, $useSortKeys, $usePayloads, $useScores, $noContent) {
333333
$hash = array_shift($document) ?? '';
334334
$score = true === $useScores ? (float) array_shift($document) : null;
335335
$payload = true === $usePayloads ? array_shift($document) : null;
@@ -338,7 +338,7 @@ public function transformParsedResponse($data)
338338
$fields = [];
339339
if (false === $noContent) {
340340
if (!(1 === count($document))) {
341-
throw new InvalidArgumentException();
341+
throw new UnexpectedServerResponseException($data, 'Incomplete response');
342342
}
343343
$rawData = reset($document);
344344
assert(is_array($rawData));

‎src/Redis/Command/SpellCheck.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222
namespace MacFJA\RediSearch\Redis\Command;
2323

2424
use function is_array;
25+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2526
use MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption;
2627
use MacFJA\RediSearch\Redis\Command\Option\NamedOption;
2728
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
2829
use MacFJA\RediSearch\Redis\Command\SpellCheckCommand\TermsOption;
2930
use MacFJA\RediSearch\Redis\Response\SpellCheckResponseItem;
3031
use Respect\Validation\Validator;
3132

32-
/**
33-
* @method array<SpellCheckResponseItem> parseResponse(mixed $data)
34-
*/
3533
class SpellCheck extends AbstractCommand
3634
{
3735
public function __construct(string $rediSearchVersion = self::MIN_IMPLEMENTED_VERSION)
@@ -83,20 +81,15 @@ public function getId(): string
8381
return 'FT.SPELLCHECK';
8482
}
8583

86-
protected function getRequiredOptions(): array
87-
{
88-
return ['index', 'query'];
89-
}
90-
9184
/**
9285
* @param mixed $data
9386
*
94-
* @return mixed|SpellCheckResponseItem[]
87+
* @return SpellCheckResponseItem[]
9588
*/
96-
protected function transformParsedResponse($data)
89+
public function parseResponse($data)
9790
{
9891
if (!is_array($data)) {
99-
return $data;
92+
throw new UnexpectedServerResponseException($data);
10093
}
10194

10295
$items = array_map(static function (array $group) {
@@ -113,4 +106,9 @@ protected function transformParsedResponse($data)
113106

114107
return array_filter($items);
115108
}
109+
110+
protected function getRequiredOptions(): array
111+
{
112+
return ['index', 'query'];
113+
}
116114
}

‎src/Redis/Command/SugGet.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@
2222
namespace MacFJA\RediSearch\Redis\Command;
2323

2424
use function count;
25-
use InvalidArgumentException;
2625
use function is_array;
26+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2727
use MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption as CV;
2828
use MacFJA\RediSearch\Redis\Command\Option\FlagOption;
2929
use MacFJA\RediSearch\Redis\Command\Option\NamedOption;
3030
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
3131
use MacFJA\RediSearch\Redis\Response\SuggestionResponseItem;
3232

33-
/**
34-
* @method array<SuggestionResponseItem> parseResponse(mixed $data)
35-
*/
3633
class SugGet extends AbstractCommand
3734
{
3835
public function __construct(string $rediSearchVersion = self::MIN_IMPLEMENTED_VERSION)
@@ -100,12 +97,12 @@ public function getId(): string
10097
/**
10198
* @param mixed $data
10299
*
103-
* @return mixed|SuggestionResponseItem[]
100+
* @return SuggestionResponseItem[]
104101
*/
105-
public function transformParsedResponse($data)
102+
public function parseResponse($data)
106103
{
107104
if (!is_array($data)) {
108-
return $data;
105+
throw new UnexpectedServerResponseException($data);
109106
}
110107

111108
$useScores = $this->options['scores']->isActive();
@@ -117,12 +114,12 @@ public function transformParsedResponse($data)
117114

118115
$documents = array_chunk($data, $chunkSize);
119116

120-
return array_map(static function ($document) use ($usePayloads, $useScores) {
117+
return array_map(static function ($document) use ($data, $usePayloads, $useScores) {
121118
$payload = true === $usePayloads ? array_pop($document) : null;
122119
$score = true === $useScores ? (float) array_pop($document) : null;
123120

124121
if (!(1 === count($document))) {
125-
throw new InvalidArgumentException();
122+
throw new UnexpectedServerResponseException($data, 'Incomplete response');
126123
}
127124
$value = reset($document);
128125

‎src/Redis/Response/CursorResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function setClient(Client $client): self
7373
}
7474

7575
/**
76-
* @return array<AggregateResponseItem>|array<SearchResponseItem>
76+
* @return array<AggregateResponseItem>
7777
*/
7878
public function current()
7979
{

‎tests/Redis/Command/InfoTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
namespace MacFJA\RediSearch\tests\Redis\Command;
2323

24+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2425
use MacFJA\RediSearch\Redis\Command\Info;
2526
use PHPUnit\Framework\TestCase;
2627

2728
/**
29+
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
30+
*
2831
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
2932
*
3033
* @covers \MacFJA\RediSearch\Redis\Command\Info
@@ -53,4 +56,11 @@ public function testFullOption(): void
5356
'idx',
5457
], $command->getArguments());
5558
}
59+
60+
public function testParseResponseInvalid(): void
61+
{
62+
$this->expectException(UnexpectedServerResponseException::class);
63+
$command = new Info();
64+
$command->parseResponse('foo');
65+
}
5666
}

‎tests/Redis/Command/SpellCheckTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
namespace MacFJA\RediSearch\tests\Redis\Command;
2323

24-
use Closure;
24+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2525
use MacFJA\RediSearch\Redis\Command\SpellCheck;
2626
use MacFJA\RediSearch\Redis\Response\SpellCheckResponseItem;
2727
use PHPUnit\Framework\TestCase;
2828

2929
/**
30+
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
31+
*
3032
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
3133
*
3234
* @covers \MacFJA\RediSearch\Redis\Command\SpellCheck
@@ -67,7 +69,7 @@ public function testFullOption(): void
6769
], $command->getArguments());
6870
}
6971

70-
public function testResponseTransformation(): void
72+
public function testParseResponse(): void
7173
{
7274
$command = new SpellCheck();
7375
$expected = [
@@ -78,11 +80,32 @@ public function testResponseTransformation(): void
7880
['TERM', 'hell', [[0.7, 'hello'], [0.4, 'hola']]],
7981
['TERM', 'worl', [[0.9, 'world'], [0.56, 'work']]],
8082
];
81-
$transformer = function ($data) {
82-
// @phpstan-ignore-next-line
83-
return $this->transformParsedResponse($data);
84-
};
85-
$actual = Closure::fromCallable($transformer)->call($command, $rawResponse);
83+
84+
$actual = $command->parseResponse($rawResponse);
85+
86+
static::assertEquals($expected, $actual);
87+
}
88+
89+
public function testParseResponseInvalid(): void
90+
{
91+
$this->expectException(UnexpectedServerResponseException::class);
92+
$command = new SpellCheck();
93+
94+
$command->parseResponse('foobar');
95+
}
96+
97+
public function testParseResponsePartialInvalid(): void
98+
{
99+
$command = new SpellCheck();
100+
$expected = [
101+
new SpellCheckResponseItem('hell', ['hello' => 0.7, 'hola' => 0.4]),
102+
];
103+
$rawResponse = [
104+
['TERM', 'hell', [[0.7, 'hello'], [0.4, 'hola']]],
105+
['TERMbar', 'worl', [[0.9, 'world'], [0.56, 'work']]],
106+
];
107+
108+
$actual = $command->parseResponse($rawResponse);
86109

87110
static::assertEquals($expected, $actual);
88111
}

‎tests/Redis/Command/SugGetTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121

2222
namespace MacFJA\RediSearch\tests\Redis\Command;
2323

24+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2425
use MacFJA\RediSearch\Redis\Command\SugGet;
26+
use MacFJA\RediSearch\Redis\Response\SuggestionResponseItem;
2527
use PHPUnit\Framework\TestCase;
2628

2729
/**
28-
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
30+
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
2931
*
32+
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
3033
* @covers \MacFJA\RediSearch\Redis\Command\SugGet
3134
*
3235
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
@@ -61,4 +64,38 @@ public function testFullOption(): void
6164
'ac', 'hell', 'FUZZY', 'WITHSCORES', 'WITHPAYLOADS', 'MAX', 10,
6265
], $command->getArguments());
6366
}
67+
68+
public function testParseResponse(): void
69+
{
70+
$expected = [
71+
new SuggestionResponseItem('hello', 0.4024922251701355, 'greeting'),
72+
new SuggestionResponseItem('hola', 0.40000000596046448, 'greeting'),
73+
];
74+
75+
$rawResult = [
76+
'hello', '0.4024922251701355', 'greeting',
77+
'hola', '0.40000000596046448', 'greeting',
78+
];
79+
80+
$command = new SugGet();
81+
$command->setWithPayloads()->setWithScores();
82+
83+
static::assertEquals($expected, $command->parseResponse($rawResult));
84+
}
85+
86+
public function testInvalidServerResponse1(): void
87+
{
88+
$this->expectException(UnexpectedServerResponseException::class);
89+
$command = new SugGet();
90+
$command->parseResponse('foobar');
91+
}
92+
93+
public function testInvalidServerResponse2(): void
94+
{
95+
$this->expectException(UnexpectedServerResponseException::class);
96+
$this->expectExceptionMessage('Unexpected response from the Redis server: Incomplete response');
97+
$command = new SugGet();
98+
$command->setWithPayloads();
99+
$command->parseResponse(['foobar']);
100+
}
64101
}

‎tests/Redis/Response/InfoResponseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
/**
3232
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand::parseResponse
33-
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand::transformParsedResponse
3433
* @covers \MacFJA\RediSearch\Redis\Command\Info
3534
* @covers \MacFJA\RediSearch\Redis\Response\InfoResponse
3635
*

0 commit comments

Comments
 (0)
Please sign in to comment.