From 55cd213a7d5dabec7790a6c92b30bd4ecb4359c5 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Wed, 22 Jan 2014 17:29:07 -0500 Subject: [PATCH] Unset value in extract method, not calling method 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 --- src/Elasticsearch/Client.php | 123 +++++++++--------- .../Endpoints/AbstractEndpoint.php | 4 + .../Namespaces/AbstractNamespace.php | 10 +- .../Namespaces/ClusterNamespace.php | 6 +- .../Namespaces/IndicesNamespace.php | 123 +++++++++--------- .../Namespaces/NodesNamespace.php | 7 - .../Namespaces/SnapshotNamespace.php | 24 ++-- 7 files changed, 149 insertions(+), 148 deletions(-) diff --git a/src/Elasticsearch/Client.php b/src/Elasticsearch/Client.php index 69e3c92d7..9237ee039 100644 --- a/src/Elasticsearch/Client.php +++ b/src/Elasticsearch/Client.php @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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 */ @@ -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; @@ -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 */ @@ -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; } diff --git a/src/Elasticsearch/Endpoints/AbstractEndpoint.php b/src/Elasticsearch/Endpoints/AbstractEndpoint.php index c5821d671..f3bb646f4 100644 --- a/src/Elasticsearch/Endpoints/AbstractEndpoint.php +++ b/src/Elasticsearch/Endpoints/AbstractEndpoint.php @@ -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(); diff --git a/src/Elasticsearch/Namespaces/AbstractNamespace.php b/src/Elasticsearch/Namespaces/AbstractNamespace.php index 930300e67..e85263816 100644 --- a/src/Elasticsearch/Namespaces/AbstractNamespace.php +++ b/src/Elasticsearch/Namespaces/AbstractNamespace.php @@ -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; } diff --git a/src/Elasticsearch/Namespaces/ClusterNamespace.php b/src/Elasticsearch/Namespaces/ClusterNamespace.php index e7167016c..d40dac82f 100644 --- a/src/Elasticsearch/Namespaces/ClusterNamespace.php +++ b/src/Elasticsearch/Namespaces/ClusterNamespace.php @@ -37,7 +37,7 @@ class ClusterNamespace extends AbstractNamespace public function health($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -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 */ @@ -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 */ diff --git a/src/Elasticsearch/Namespaces/IndicesNamespace.php b/src/Elasticsearch/Namespaces/IndicesNamespace.php index 04beb93a1..0c5e5217c 100644 --- a/src/Elasticsearch/Namespaces/IndicesNamespace.php +++ b/src/Elasticsearch/Namespaces/IndicesNamespace.php @@ -30,7 +30,7 @@ class IndicesNamespace extends AbstractNamespace public function exists($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -70,7 +70,7 @@ public function exists($params) public function segments($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -96,7 +96,7 @@ public function segments($params = array()) public function deleteTemplate($params) { $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -123,13 +123,13 @@ public function deleteTemplate($params) public function deleteWarmer($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + /** @var callback $endpointBuilder */ @@ -157,7 +157,7 @@ public function deleteWarmer($params) public function delete($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -202,10 +202,8 @@ public function delete($params = array()) public function stats($params = array()) { $metric = $this->extractArgument($params, '$metric'); - unset($params['$metric']); $index = $this->extractArgument($params, 'index'); - unset($params['index']); /** @var callback $endpointBuilder */ $endpointBuilder = $this->dicEndpoints; @@ -231,10 +229,10 @@ public function stats($params = array()) public function putSettings($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -263,7 +261,7 @@ public function putSettings($params = array()) public function snapshotIndex($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -289,10 +287,10 @@ public function snapshotIndex($params = array()) public function getMapping($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + /** @var callback $endpointBuilder */ @@ -320,13 +318,10 @@ public function getMapping($params = array()) public function getFieldMapping($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); $type = $this->extractArgument($params, 'type'); - unset($params['type']); $field = $this->extractArgument($params, 'field'); - unset($params['field']); /** @var callback $endpointBuilder */ @@ -360,7 +355,7 @@ public function getFieldMapping($params = array()) public function flush($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -389,7 +384,7 @@ public function flush($params = array()) public function refresh($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -418,10 +413,10 @@ public function refresh($params = array()) public function existsType($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + /** @var callback $endpointBuilder */ @@ -461,13 +456,13 @@ public function existsType($params) public function putAlias($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -496,13 +491,13 @@ public function putAlias($params = array()) public function getWarmer($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + /** @var callback $endpointBuilder */ @@ -532,16 +527,16 @@ public function getWarmer($params) public function putWarmer($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -572,10 +567,10 @@ public function putWarmer($params) public function putTemplate($params) { $name = $this->extractArgument($params, 'name'); - unset($params['name']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -607,13 +602,13 @@ public function putTemplate($params) public function validateQuery($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 */ @@ -643,10 +638,10 @@ public function validateQuery($params = array()) public function getAlias($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -676,13 +671,13 @@ public function getAlias($params) public function putMapping($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 */ $endpointBuilder = $this->dicEndpoints; @@ -709,10 +704,10 @@ public function putMapping($params) public function deleteMapping($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $type = $this->extractArgument($params, 'type'); - unset($params['type']); + /** @var callback $endpointBuilder */ @@ -738,7 +733,7 @@ public function deleteMapping($params) public function getTemplate($params) { $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -763,7 +758,7 @@ public function getTemplate($params) public function existsTemplate($params) { $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -801,10 +796,10 @@ public function existsTemplate($params) public function create($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -839,7 +834,7 @@ public function create($params) public function optimize($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -866,10 +861,10 @@ public function optimize($params = array()) public function deleteAlias($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -896,7 +891,7 @@ public function deleteAlias($params) public function open($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -929,10 +924,10 @@ public function open($params) public function analyze($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -970,7 +965,7 @@ public function analyze($params = array()) public function clearCache($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -997,10 +992,10 @@ public function clearCache($params = array()) public function updateAliases($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -1027,10 +1022,10 @@ public function updateAliases($params = array()) public function getAliases($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ $endpointBuilder = $this->dicEndpoints; @@ -1059,10 +1054,10 @@ public function getAliases($params = array()) public function existsAlias($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -1103,7 +1098,7 @@ public function existsAlias($params) public function status($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ @@ -1128,10 +1123,10 @@ public function status($params = array()) public function getSettings($params = array()) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + $name = $this->extractArgument($params, 'name'); - unset($params['name']); + /** @var callback $endpointBuilder */ @@ -1158,7 +1153,7 @@ public function getSettings($params = array()) public function close($params) { $index = $this->extractArgument($params, 'index'); - unset($params['index']); + /** @var callback $endpointBuilder */ diff --git a/src/Elasticsearch/Namespaces/NodesNamespace.php b/src/Elasticsearch/Namespaces/NodesNamespace.php index d02274b43..eaaaab68d 100644 --- a/src/Elasticsearch/Namespaces/NodesNamespace.php +++ b/src/Elasticsearch/Namespaces/NodesNamespace.php @@ -42,13 +42,10 @@ class NodesNamespace extends AbstractNamespace public function stats($params = array()) { $nodeID = $this->extractArgument($params, 'node_id'); - unset($params['node_id']); $metric = $this->extractArgument($params, 'metric'); - unset($params['metric']); $index_metric = $this->extractArgument($params, 'index_metric'); - unset($params['index_metric']); /** @var callback $endpointBuilder */ @@ -85,8 +82,6 @@ public function stats($params = array()) public function info($params = array()) { $nodeID = $this->extractArgument($params, 'node_id'); - unset($params['node_id']); - /** @var callback $endpointBuilder */ $endpointBuilder = $this->dicEndpoints; @@ -113,7 +108,6 @@ public function info($params = array()) public function hotThreads($params = array()) { $nodeID = $this->extractArgument($params, 'node_id'); - unset($params['node_id']); /** @var callback $endpointBuilder */ @@ -139,7 +133,6 @@ public function hotThreads($params = array()) public function shutdown($params = array()) { $nodeID = $this->extractArgument($params, 'node_id'); - unset($params['node_id']); /** @var callback $endpointBuilder */ diff --git a/src/Elasticsearch/Namespaces/SnapshotNamespace.php b/src/Elasticsearch/Namespaces/SnapshotNamespace.php index 2d6429f16..1ba41929a 100644 --- a/src/Elasticsearch/Namespaces/SnapshotNamespace.php +++ b/src/Elasticsearch/Namespaces/SnapshotNamespace.php @@ -29,10 +29,10 @@ class SnapshotNamespace extends AbstractNamespace public function create($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + $snapshot = $this->extractArgument($params, 'snapshot'); - unset($params['snapshot']); + /** @var callback $endpointBuilder */ @@ -59,10 +59,10 @@ public function create($params = array()) public function createRepository($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + $body = $this->extractArgument($params, 'body'); - unset($params['body']); + /** @var callback $endpointBuilder */ @@ -88,10 +88,10 @@ public function createRepository($params = array()) public function delete($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + $snapshot = $this->extractArgument($params, 'snapshot'); - unset($params['snapshot']); + /** @var callback $endpointBuilder */ @@ -118,7 +118,7 @@ public function delete($params = array()) public function deleteRepository($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + /** @var callback $endpointBuilder */ @@ -143,10 +143,10 @@ public function deleteRepository($params = array()) public function get($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + $snapshot = $this->extractArgument($params, 'snapshot'); - unset($params['snapshot']); + /** @var callback $endpointBuilder */ @@ -173,7 +173,7 @@ public function get($params = array()) public function getRepository($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + /** @var callback $endpointBuilder */ @@ -199,10 +199,10 @@ public function getRepository($params = array()) public function restore($params = array()) { $repository = $this->extractArgument($params, 'repository'); - unset($params['repository']); + $snapshot = $this->extractArgument($params, 'snapshot'); - unset($params['snapshot']); + /** @var callback $endpointBuilder */