Skip to content

Commit

Permalink
Unset value in extract method, not calling method
Browse files Browse the repository at this point in the history
For DRY purposes, the unset should be performed in the extracting
method, not the method that performs the calling.

Does require a pass-by-reference, but it saves a ton of repetitive
code and makes it easier to work with objects
  • Loading branch information
polyfractal committed Jan 23, 2014
1 parent 5153338 commit 55cd213
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 148 deletions.
123 changes: 63 additions & 60 deletions src/Elasticsearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ public function ping()
public function get($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -190,13 +190,13 @@ public function get($params)
public function getSource($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -233,13 +233,13 @@ public function getSource($params)
public function delete($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -270,13 +270,13 @@ public function delete($params)
public function deleteByQuery($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -312,13 +312,13 @@ public function deleteByQuery($params = array())
public function count($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -348,13 +348,13 @@ public function count($params = array())
public function percolate($params)
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -388,13 +388,13 @@ public function percolate($params)
public function exists($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -456,16 +456,16 @@ public function exists($params)
public function mlt($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -504,13 +504,13 @@ public function mlt($params)
public function mget($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -540,13 +540,13 @@ public function mget($params = array())
public function msearch($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -587,16 +587,16 @@ public function msearch($params = array())
public function create($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -630,13 +630,13 @@ public function create($params)
public function bulk($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -678,16 +678,16 @@ public function bulk($params = array())
public function index($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -720,10 +720,10 @@ public function index($params)
public function suggest($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -767,16 +767,16 @@ public function suggest($params = array())
public function explain($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -835,13 +835,13 @@ public function explain($params)
public function search($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -870,11 +870,8 @@ public function search($params = array())
public function scroll($params = array())
{
$scrollID = $this->extractArgument($params, 'scroll_id');
unset($params['scroll_id']);

$body = $this->extractArgument($params, 'body');
unset($params['body']);


/** @var callback $endpointBuilder */
$endpointBuilder = $this->dicEndpoints;
Expand Down Expand Up @@ -916,16 +913,16 @@ public function scroll($params = array())
public function update($params)
{
$id = $this->extractArgument($params, 'id');
unset($params['id']);


$index = $this->extractArgument($params, 'index');
unset($params['index']);


$type = $this->extractArgument($params, 'type');
unset($params['type']);


$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -1158,10 +1155,16 @@ private function prependMissingScheme($host) {
*
* @return null|mixed
*/
private function extractArgument($params, $arg)
public function extractArgument(&$params, $arg)
{
if (is_object($params) === true) {
$params = (array)$params;
}

if (isset($params[$arg]) === true) {
return $params[$arg];
$val = $params[$arg];
unset($params[$arg]);
return $val;
} else {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Elasticsearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function performRequest()
*/
public function setParams($params)
{
if (is_object($params) === true) {
$params = (array)$params;
}

$this->checkUserParams($params);
$this->params = $this->convertArraysToStrings($params);
$this->extractIgnore();
Expand Down
10 changes: 8 additions & 2 deletions src/Elasticsearch/Namespaces/AbstractNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ public function __construct($transport, $dicEndpoints)
*
* @return null|mixed
*/
public function extractArgument($params, $arg)
public function extractArgument(&$params, $arg)
{
if (is_object($params) === true) {
$params = (array)$params;
}

if (isset($params[$arg]) === true) {
return $params[$arg];
$val = $params[$arg];
unset($params[$arg]);
return $val;
} else {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Elasticsearch/Namespaces/ClusterNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ClusterNamespace extends AbstractNamespace
public function health($params = array())
{
$index = $this->extractArgument($params, 'index');
unset($params['index']);



/** @var callback $endpointBuilder */
Expand All @@ -64,7 +64,7 @@ public function health($params = array())
public function reroute($params = array())
{
$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down Expand Up @@ -116,7 +116,7 @@ public function state($params = array())
public function putSettings($params = array())
{
$body = $this->extractArgument($params, 'body');
unset($params['body']);



/** @var callback $endpointBuilder */
Expand Down
Loading

0 comments on commit 55cd213

Please sign in to comment.