Skip to content

Commit

Permalink
Replaced array_walk with array_map in Connection::getURI (#1075)
Browse files Browse the repository at this point in the history
* 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 <e.zimuel@gmail.com>
  • Loading branch information
pascaldevink and ezimuel authored Dec 16, 2020
1 parent c8084ec commit b55e189
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,17 @@ function ($response) use ($connection, $transport, $request, $options) {
private function getURI(string $uri, ?array $params): string
{
if (isset($params) === true && !empty($params)) {
array_walk(
$params,
function (&$value, &$key) {
$params = array_map(
function ($value) {
if ($value === true) {
$value = 'true';
return 'true';
} elseif ($value === false) {
$value = 'false';
return 'false';
}
}

return $value;
},
$params
);

$uri .= '?' . http_build_query($params);
Expand Down
26 changes: 26 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,30 @@ public function testElasticMetaClientHeaderIsNotSentWhenParameterIsFalse()

$this->assertArrayNotHasKey('x-elastic-client-meta', $request['headers']);
}

public function testParametersAreSent()
{
$connectionParams = [];
$host = [
'host' => 'localhost'
];
$requestParams = [
'foo' => true,
'baz' => false,
'bar' => 'baz'
];

$connection = new Connection(
ClientBuilder::defaultHandler(),
$host,
$connectionParams,
$this->serializer,
$this->logger,
$this->trace
);
$result = $connection->performRequest('GET', '/', $requestParams);
$request = $connection->getLastRequestInfo()['request'];

$this->assertEquals('/?foo=true&baz=false&bar=baz', $request['uri']);
}
}

0 comments on commit b55e189

Please sign in to comment.