Skip to content

Commit b55e189

Browse files
Replaced array_walk with array_map in Connection::getURI (#1075)
* Replaced array_walk with array_map In Connection::getURI, array_walk was used together with passing the values as references, to change the original array. Passing values as references is error-prone and discouraged for quite some time. Also, when using in conjunction with PHP 8.0, it will fail. array_map can do the same thing as the original array_walk implementation, but without the downsides of having side effects and having to pass values as references. * Add the x-elastic-client-meta header (#1089) * Added the x-elastic-client-meta header * Removed @ExpectedException usage in PHPUnit * Removed prestissimo plugin for composer in github action * Added .phpunit.result.cache in .gitignore * Add the t transport parameter in telemetry client header * Fixed semver format for PHP version in client telemetry header Co-authored-by: Enrico Zimuel <[email protected]>
1 parent c8084ec commit b55e189

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,17 @@ function ($response) use ($connection, $transport, $request, $options) {
352352
private function getURI(string $uri, ?array $params): string
353353
{
354354
if (isset($params) === true && !empty($params)) {
355-
array_walk(
356-
$params,
357-
function (&$value, &$key) {
355+
$params = array_map(
356+
function ($value) {
358357
if ($value === true) {
359-
$value = 'true';
358+
return 'true';
360359
} elseif ($value === false) {
361-
$value = 'false';
360+
return 'false';
362361
}
363-
}
362+
363+
return $value;
364+
},
365+
$params
364366
);
365367

366368
$uri .= '?' . http_build_query($params);

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,30 @@ public function testElasticMetaClientHeaderIsNotSentWhenParameterIsFalse()
426426

427427
$this->assertArrayNotHasKey('x-elastic-client-meta', $request['headers']);
428428
}
429+
430+
public function testParametersAreSent()
431+
{
432+
$connectionParams = [];
433+
$host = [
434+
'host' => 'localhost'
435+
];
436+
$requestParams = [
437+
'foo' => true,
438+
'baz' => false,
439+
'bar' => 'baz'
440+
];
441+
442+
$connection = new Connection(
443+
ClientBuilder::defaultHandler(),
444+
$host,
445+
$connectionParams,
446+
$this->serializer,
447+
$this->logger,
448+
$this->trace
449+
);
450+
$result = $connection->performRequest('GET', '/', $requestParams);
451+
$request = $connection->getLastRequestInfo()['request'];
452+
453+
$this->assertEquals('/?foo=true&baz=false&bar=baz', $request['uri']);
454+
}
429455
}

0 commit comments

Comments
 (0)