Skip to content

Commit

Permalink
Replaced array_walk with array_map
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pascal de Vink committed Dec 15, 2020
1 parent a8e4e52 commit cb80cdc
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -346,15 +346,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
32 changes: 32 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,36 @@ public function testHeaderClientParamIsResetAfterSent()
$headersAfter = $connection->getHeaders();
$this->assertEquals($headersBefore, $headersAfter);
}

/**
* @depends testGetHeadersContainUserAgent
*
* @covers \Connection::performRequest
* @covers \Connection::getURI
*/
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 cb80cdc

Please # to comment.