From e36ab876d5b62b6d644209264ba2ac34ceacb119 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 09:28:47 -0300 Subject: [PATCH 1/9] Few tweaks on CS --- src/InfluxDB/Client.php | 89 ++++++++++------------- src/InfluxDB/Client/Exception.php | 3 +- src/InfluxDB/Database.php | 52 ++++--------- src/InfluxDB/Database/Exception.php | 10 +-- src/InfluxDB/Database/RetentionPolicy.php | 17 ++--- src/InfluxDB/Exception.php | 10 +-- src/InfluxDB/Point.php | 68 +++++++++++------ src/InfluxDB/Query/Builder.php | 76 +++++++++++-------- src/InfluxDB/Query/Exception.php | 10 +-- src/InfluxDB/ResultSet.php | 63 ++++++---------- 10 files changed, 186 insertions(+), 212 deletions(-) diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 947622984..bc87b5bdb 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -1,17 +1,14 @@ host = $host; - $this->port = (int) $port; - $this->username = $username; - $this->password = $password; - $this->timeout = $timeout; + ) { + $this->host = (string) $host; + $this->port = (int) $port; + $this->username = (string) $username; + $this->password = (string) $password; + $this->timeout = (int) $timeout; $this->verifySSL = (bool) $verifySSL; if ($ssl) { $this->scheme = 'https'; - $this->options += array( - 'verify' => $verifySSL - ); + $this->options += array('verify' => $verifySSL); } // the the base URI @@ -119,7 +112,8 @@ class Client /** * For testing - * @param \Guzzle\Http\Client $client + * + * @param \Guzzle\Http\Client $client * @return $this */ public function setHttpClient(\Guzzle\Http\Client $client) @@ -132,8 +126,7 @@ class Client /** * Use the given database * - * @param string $name - * + * @param string $name * @return Database */ public function selectDB($name) @@ -144,21 +137,19 @@ class Client /** * Query influxDB * - * @param string $database - * @param string $query - * @param array $params - * + * @param string $database + * @param string $query + * @param array $params * @return ResultSet * @throws Exception */ public function query($database, $query, $params = array()) { - if ($database) { $params['db'] = $database; } - $params = '?'.http_build_query(array_merge(array('q' => $query), $params)); + $params = '?' . http_build_query(array_merge(array('q' => $query), $params)); $options = array_merge($this->options, array('exceptions' => false, 'timeout' => $this->getTimeout())); @@ -168,7 +159,6 @@ class Client $raw = (string) $response->send()->getBody(); return new ResultSet($raw); - } catch (\Exception $e) { throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage())); } @@ -177,28 +167,27 @@ class Client /** * Write points to the database * - * @param string $database - * @param string $data - * @param string $precision The timestamp precision - * + * @param string $database + * @param string $data + * @param string $precision The timestamp precision * @return bool + * @throws Exception * * @internal Internal method, do not use directly - * @throws Exception */ public function write($database, $data, $precision) { try { - - $result = $this->httpClient->post($this->getBaseURI() - . '/write?db=' . $database - . '&precision=' . $precision - , null, $data, + $result = $this->httpClient->post( + $this->getBaseURI() . + '/write?db=' . $database . + '&precision=' . $precision, + null, + $data, array('timeout' => $this->getTimeout()) )->send(); - return $result->getStatusCode() == 204; - + return $result->getStatusCode() === 204; } catch (\Exception $e) { throw new Exception(sprintf('Writing has failed, exception: %s', $e->getMessage())); } @@ -218,7 +207,6 @@ class Client * List all the users * * @return array - * * @throws Exception */ public function listUsers() @@ -230,37 +218,33 @@ class Client /** * Build the client from a dsn - * * Example: https+influxdb://username:pass@localhost:8086/databasename', timeout=5 * - * @param string $dsn - * - * @param int $timeout - * @param bool $verifySSL - * + * @param string $dsn + * @param int $timeout + * @param bool $verifySSL * @return Client|Database - * * @throws ClientException */ public static function fromDSN($dsn, $timeout = 0, $verifySSL = false) { $connParams = parse_url($dsn); $schemeInfo = explode('+', $connParams['scheme']); - $dbName = null; - $modifier = null; - $scheme = $schemeInfo[0]; + $dbName = null; + $modifier = null; + $scheme = $schemeInfo[0]; if (isset($schemeInfo[1])) { $modifier = $schemeInfo[0]; - $scheme = $schemeInfo[1]; + $scheme = $schemeInfo[1]; } if ($scheme != 'influxdb') { throw new ClientException(sprintf('%s is not a valid scheme', $scheme)); } - $ssl = ($modifier && $modifier == 'https' ? true : false); - $dbName = ($connParams['path'] ? substr($connParams['path'], 1) : null); + $ssl = $modifier === 'https' ? true : false; + $dbName = $connParams['path'] ? substr($connParams['path'], 1) : null; $client = new self( $connParams['host'], @@ -274,6 +258,7 @@ class Client return ($dbName ? $client->selectDB($dbName) : $client); } + /** * @return mixed */ @@ -305,4 +290,4 @@ class Client return $names; } -} \ No newline at end of file +} diff --git a/src/InfluxDB/Client/Exception.php b/src/InfluxDB/Client/Exception.php index 1b446a2ac..0674e8686 100644 --- a/src/InfluxDB/Client/Exception.php +++ b/src/InfluxDB/Client/Exception.php @@ -9,5 +9,4 @@ namespace InfluxDB\Client; */ class Exception extends \InfluxDB\Exception { - -} \ No newline at end of file +} diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index f5d58d78a..3540aa828 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -1,13 +1,10 @@ client = $client; - - if (!$name) { + if (empty($name)) { throw new \InvalidArgumentException('No database name provided'); } - $this->name = $name; - + $this->name = $name; + $this->client = $client; } /** - * @return string db name + * @return string */ public function getName() { @@ -73,11 +65,9 @@ class Database /** * Query influxDB * - * @param string $query - * @param array $params - * + * @param string $query + * @param array $params * @return ResultSet - * * @throws Exception */ public function query($query, $params = array()) @@ -88,10 +78,8 @@ class Database /** * Create this database * - * @param RetentionPolicy $retentionPolicy - * + * @param RetentionPolicy $retentionPolicy * @return ResultSet - * * @throws DatabaseException * @throws Exception */ @@ -103,7 +91,6 @@ class Database if ($retentionPolicy) { $this->createRetentionPolicy($retentionPolicy); } - } catch (\Exception $e) { throw new DatabaseException( sprintf('Failed to created database %s, exception: %s', $this->name, $e->getMessage()) @@ -112,8 +99,7 @@ class Database } /** - * @param RetentionPolicy $retentionPolicy - * + * @param RetentionPolicy $retentionPolicy * @return ResultSet */ public function createRetentionPolicy(RetentionPolicy $retentionPolicy) @@ -124,9 +110,8 @@ class Database /** * Writes points into InfluxDB * - * @param Point[] $points Array of points - * @param string $precision The timestamp precision (defaults to nanoseconds) - * + * @param Point[] $points Array of points + * @param string $precision The timestamp precision (defaults to nanoseconds) * @return bool * @throws Exception */ @@ -135,8 +120,7 @@ class Database $payload = array(); foreach ($points as $point) { - - if (!$point instanceof Point) { + if (! $point instanceof Point) { throw new \InvalidArgumentException('An array of Point[] should be passed'); } @@ -166,7 +150,6 @@ class Database /** * @return array - * * @throws Exception */ public function listRetentionPolicies() @@ -174,7 +157,6 @@ class Database return $this->query(sprintf('SHOW RETENTION POLICIES %s', $this->name))->getPoints(); } - /** * Drop this database */ @@ -202,9 +184,8 @@ class Database } /** - * @param $method - * @param RetentionPolicy $retentionPolicy - * + * @param string $method + * @param RetentionPolicy $retentionPolicy * @return string */ protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy) @@ -229,5 +210,4 @@ class Database return $query; } - -} \ No newline at end of file +} diff --git a/src/InfluxDB/Database/Exception.php b/src/InfluxDB/Database/Exception.php index fa17ab7aa..f5142b49e 100644 --- a/src/InfluxDB/Database/Exception.php +++ b/src/InfluxDB/Database/Exception.php @@ -1,12 +1,10 @@ name = $name; - $this->duration = $duration; + $this->name = $name; + $this->duration = $duration; $this->replication = $replication; - - $this->default = (bool) $default; + $this->default = (bool) $default; } -} \ No newline at end of file +} diff --git a/src/InfluxDB/Exception.php b/src/InfluxDB/Exception.php index fbf6cb5cb..868dd74c4 100644 --- a/src/InfluxDB/Exception.php +++ b/src/InfluxDB/Exception.php @@ -1,12 +1,10 @@ measurement = (string) $measurement; - $this->tags = $tags; - $this->fields = $additionalFields; + $this->tags = $tags; + $this->fields = $additionalFields; $this->fields += array('value' => (float) $value); @@ -72,7 +81,7 @@ class Point $string .= ',' . $this->arrayToString($this->tags); } - $string .= ' ' .$this->arrayToString($this->fields); + $string .= ' ' . $this->arrayToString($this->fields); if ($this->timestamp) { $string .= ' '.$this->timestamp; @@ -81,26 +90,39 @@ class Point return $string; } + /** + * @param array $arr + * @return string + */ private function arrayToString(array $arr) { $strParts = array(); foreach ($arr as $key => $value) { - $strParts[] = "{$key}={$value}"; + $strParts[] = sprintf('%s=%s', $key, $value); } - return implode(",", $strParts); + return implode(',', $strParts); } /** - * @param $timestamp - * + * @param int $timestamp * @return bool */ private function isValidTimeStamp($timestamp) { - return ((int) $timestamp === $timestamp) - && ($timestamp <= PHP_INT_MAX) - && ($timestamp >= ~PHP_INT_MAX); + if ((int) $timestamp === $timestamp) { + return true; + } + + if ($timestamp <= PHP_INT_MAX) { + return true; + } + + if ($timestamp >= ~PHP_INT_MAX) { + return true; + } + + return false; } -} \ No newline at end of file +} diff --git a/src/InfluxDB/Query/Builder.php b/src/InfluxDB/Query/Builder.php index 1709025f5..c3cad07e2 100644 --- a/src/InfluxDB/Query/Builder.php +++ b/src/InfluxDB/Query/Builder.php @@ -1,7 +1,4 @@ - */ namespace InfluxDB\Query; @@ -24,16 +21,43 @@ use InfluxDB\ResultSet; * @todo add merge * * @package InfluxDB\Query + * @author Stephen "TheCodeAssassin" Hoogendijk */ class Builder { + /** + * @var Database + */ + protected $db; - protected $db = null; + /** + * @var string + */ protected $selection = '*'; + + /** + * @var string[] + */ protected $where = array(); - protected $startTime = null; - protected $endTime = null; - protected $metric = null; + + /** + * @var string + */ + protected $startTime; + + /** + * @var string + */ + protected $endTime; + + /** + * @var string + */ + protected $metric; + + /** + * @var string + */ protected $limitClause = ''; /** @@ -45,8 +69,7 @@ class Builder } /** - * @param string $metric The metric to select (required) - * + * @param string $metric The metric to select (required) * @return $this */ public function from($metric) @@ -63,8 +86,7 @@ class Builder * * $series->select('sum(value)', * - * @param string $customSelect - * + * @param string $customSelect * @return $this */ public function select($customSelect) @@ -83,7 +105,6 @@ class Builder */ public function where(array $conditions) { - foreach ($conditions as $condition) { $this->where[] = $condition; } @@ -92,8 +113,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function count($field = 'type') @@ -104,8 +124,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function median($field = 'type') @@ -116,8 +135,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function mean($field = 'type') @@ -128,8 +146,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function sum($field = 'type') @@ -140,8 +157,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function first($field = 'type') @@ -152,8 +168,7 @@ class Builder } /** - * @param string $field - * + * @param string $field * @return $this */ public function last($field = 'type') @@ -166,9 +181,8 @@ class Builder /** * Set's the time range to select data from * - * @param int $from Unix timestamp from - * @param int $to Unix timestamp to - * + * @param int $from + * @param int $to * @return $this */ public function setTimeRange($from, $to) @@ -207,7 +221,6 @@ class Builder return $this; } - /** * @return string */ @@ -233,12 +246,13 @@ class Builder { $query = sprintf("SELECT %s FROM %s", $this->selection, $this->metric); - if (!$this->metric) { + if (! $this->metric) { throw new \InvalidArgumentException('No metric provided to from()'); } - for ($i=0; $i < count($this->where); $i++) { + for ($i = 0; $i < count($this->where); $i++) { $selection = 'WHERE'; + if ($i > 0) { $selection = 'AND'; } @@ -254,4 +268,4 @@ class Builder return $query; } -} \ No newline at end of file +} diff --git a/src/InfluxDB/Query/Exception.php b/src/InfluxDB/Query/Exception.php index 4571efb74..448a41e97 100644 --- a/src/InfluxDB/Query/Exception.php +++ b/src/InfluxDB/Query/Exception.php @@ -1,12 +1,10 @@ raw = $raw; - - $this->parsedResults = json_decode($raw, true); + $this->parsedResults = json_decode((string) $raw, true); if (json_last_error() !== JSON_ERROR_NONE) { - throw new \InvalidArgumentException("Invalid JSON"); + throw new \InvalidArgumentException('Invalid JSON'); } // There was an error in the query thrown by influxdb if (isset($this->parsedResults['error'])) { throw new ClientException($this->parsedResults['error']); + } - // Check if there are errors in the first serie - } elseif (isset($this->parsedResults['results'][0]['error'])) { + // Check if there are errors in the first serie + if (isset($this->parsedResults['results'][0]['error'])) { throw new ClientException($this->parsedResults['results'][0]['error']); } } /** - * @param $metricName - * @param array $tags - * + * @param $metricName + * @param array $tags * @return array $points */ public function getPoints($metricName = '', array $tags = array()) @@ -62,7 +52,6 @@ class ResultSet $series = $this->getSeries(); foreach ($series as $serie) { - if ((empty($metricName) && empty($tags) || $serie['name'] == $metricName || (isset($serie['tags']) && array_intersect($tags, $serie['tags']))) @@ -82,30 +71,26 @@ class ResultSet * each containing the keys for a series * * @throws Exception - * * @return array $series */ public function getSeries() { - $pickSeries = function ($object) { - - if (isset($object['error'])) { - throw new ClientException($object['error']); - } - - return (isset($object['series']) ? $object['series'] : array()); - }; - - // we use array_shift because of compatibility with php5.3 - // Foreach object, pick series key - $map = array_map($pickSeries, $this->parsedResults['results']); return array_shift( - $map + array_map( + function ($object) { + if (isset($object['error'])) { + throw new ClientException($object['error']); + } + + return isset($object['series']) ? $object['series'] : array(); + }, + $this->parsedResults['results'] + ) ); } /** - * @param array $serie + * @param array $serie * @return array */ private function getPointsFromSerie(array $serie) @@ -113,13 +98,9 @@ class ResultSet $points = array(); foreach ($serie['values'] as $point) { - $points[] = array_combine( - $serie['columns'], - $point - ); + $points[] = array_combine($serie['columns'], $point); } return $points; } - -} \ No newline at end of file +} From 5bc2b9fabd018a4b0243021e1666f2fffe032280 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 09:54:00 -0300 Subject: [PATCH 2/9] Use native type-checking --- src/InfluxDB/Database.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 3540aa828..713fa2753 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -117,15 +117,12 @@ class Database */ public function writePoints(array $points, $precision = self::PRECISION_NANOSECONDS) { - $payload = array(); - - foreach ($points as $point) { - if (! $point instanceof Point) { - throw new \InvalidArgumentException('An array of Point[] should be passed'); - } - - $payload[] = (string) $point; - } + $payload = array_map( + function (Point $point) { + return (string) $point; + }, + $points + ); return $this->client->write($this->name, implode(PHP_EOL, $payload), $precision); } @@ -190,7 +187,6 @@ class Database */ protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy) { - if (!in_array($method, array('CREATE', 'ALTER'))) { throw new \InvalidArgumentException(sprintf('%s is not a valid method')); } From 6419531178514b578b0855ac80e0ffba0c7031c5 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 09:56:10 -0300 Subject: [PATCH 3/9] Use array_merge instead --- src/InfluxDB/Point.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/InfluxDB/Point.php b/src/InfluxDB/Point.php index c32eda88d..03336eb79 100644 --- a/src/InfluxDB/Point.php +++ b/src/InfluxDB/Point.php @@ -55,9 +55,7 @@ class Point $this->measurement = (string) $measurement; $this->tags = $tags; - $this->fields = $additionalFields; - - $this->fields += array('value' => (float) $value); + $this->fields = array_merge($additionalFields, array('value' => (float) $value)); if ($timestamp && !$this->isValidTimeStamp($timestamp)) { throw new DatabaseException(sprintf('%s is not a valid timestamp', $timestamp)); From dfe45c0ff206ad2ba98b619187fd7db4f47d092a Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 09:57:10 -0300 Subject: [PATCH 4/9] Cast all to (int) --- src/InfluxDB/Database/RetentionPolicy.php | 4 ++-- src/InfluxDB/Query/Builder.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/InfluxDB/Database/RetentionPolicy.php b/src/InfluxDB/Database/RetentionPolicy.php index fc4773cca..cbb8ae6b2 100644 --- a/src/InfluxDB/Database/RetentionPolicy.php +++ b/src/InfluxDB/Database/RetentionPolicy.php @@ -40,9 +40,9 @@ class RetentionPolicy */ public function __construct($name, $duration = '1d', $replication = 1, $default = false) { - $this->name = $name; + $this->name = (string) $name; $this->duration = $duration; - $this->replication = $replication; + $this->replication = (int) $replication; $this->default = (bool) $default; } } diff --git a/src/InfluxDB/Query/Builder.php b/src/InfluxDB/Query/Builder.php index c3cad07e2..1060bc39d 100644 --- a/src/InfluxDB/Query/Builder.php +++ b/src/InfluxDB/Query/Builder.php @@ -187,8 +187,8 @@ class Builder */ public function setTimeRange($from, $to) { - $fromDate = date('Y-m-d H:i:s', $from); - $toDate = date('Y-m-d H:i:s', $to); + $fromDate = date('Y-m-d H:i:s', (int) $from); + $toDate = date('Y-m-d H:i:s', (int) $to); $this->where(array("time > '$fromDate'", "time < '$toDate'")); From 5536533d652f449fa75ebd7def3382f1b993967f Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 10:03:21 -0300 Subject: [PATCH 5/9] Fix composer.json indentation --- composer.json | 91 ++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/composer.json b/composer.json index eae2c469e..a21d4e43d 100644 --- a/composer.json +++ b/composer.json @@ -1,53 +1,46 @@ { - "name": "influxdb/influxdb-php", - "description": "InfluxDB client library for PHP", - "keywords": [ - "influxdb", - "client", - "time series", - "influxdb client", - "influxdb class", - "influxdb library" - ], - "minimum-stability": "dev", - "authors": [ - { - "name": "Stephen Hoogendijk", - "email": "stephen@tca0.nl" - }, - { - "name": "Daniel Martinez", - "email": "danimartcas@hotmail.com" - } - ], - "require": { - "php": ">=5.3", - "guzzlehttp/guzzle": "3.*", - "symfony/event-dispatcher": "2.*" - - }, - "require-dev": { - "codeclimate/php-test-reporter": "0.*", - "symfony/config": "~2.8", - "symfony/console": "~2.8", - "symfony/filesystem": "~2.8", - "symfony/stopwatch": "~2.8", - "symfony/yaml": "~2.8" - }, - "scripts": { - "post-install-cmd": [ + "name": "influxdb/influxdb-php", + "description": "InfluxDB client library for PHP", + "keywords": [ + "influxdb", + "client", + "time series", + "influxdb client", + "influxdb class", + "influxdb library" ], - "post-update-cmd": [ - ] - }, - "autoload": { - "psr-4": { - "InfluxDB\\": "src/InfluxDB" + "minimum-stability": "dev", + "authors": [ + { + "name": "Stephen Hoogendijk", + "email": "stephen@tca0.nl" + }, + { + "name": "Daniel Martinez", + "email": "danimartcas@hotmail.com" + } + ], + "require": { + "php": ">=5.3", + "guzzlehttp/guzzle": "3.*", + "symfony/event-dispatcher": "2.*" + }, + "require-dev": { + "codeclimate/php-test-reporter": "0.*", + "symfony/config": "~2.8", + "symfony/console": "~2.8", + "symfony/filesystem": "~2.8", + "symfony/stopwatch": "~2.8", + "symfony/yaml": "~2.8" + }, + "autoload": { + "psr-4": { + "InfluxDB\\": "src/InfluxDB" + } + }, + "autoload-dev": { + "psr-4": { + "InfluxDB\\Test\\": "tests" + } } - }, - "autoload-dev": { - "psr-4": { - "InfluxDB\\Test\\": "tests" - } - } } From fe5c97873fe3edd85a7f5ad7ab8b57c6ab5c57c9 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 10:03:41 -0300 Subject: [PATCH 6/9] `composer validate` complains about `license` entry --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index a21d4e43d..d6f44f717 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "influxdb/influxdb-php", "description": "InfluxDB client library for PHP", + "license": "MIT", "keywords": [ "influxdb", "client", From d266b25a5ab9187509c30ab875e913ab33bc999a Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 10:10:46 -0300 Subject: [PATCH 7/9] Fix `Only variables should be passed by reference` --- src/InfluxDB/ResultSet.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/InfluxDB/ResultSet.php b/src/InfluxDB/ResultSet.php index ccecf60ca..7c8e0c5bc 100644 --- a/src/InfluxDB/ResultSet.php +++ b/src/InfluxDB/ResultSet.php @@ -75,18 +75,18 @@ class ResultSet */ public function getSeries() { - return array_shift( - array_map( - function ($object) { - if (isset($object['error'])) { - throw new ClientException($object['error']); - } + $series = array_map( + function ($object) { + if (isset($object['error'])) { + throw new ClientException($object['error']); + } - return isset($object['series']) ? $object['series'] : array(); - }, - $this->parsedResults['results'] - ) + return isset($object['series']) ? $object['series'] : array(); + }, + $this->parsedResults['results'] ); + + return array_shift($series); } /** From 0a11cfd315827d98b968eb7cd7a75d38450be477 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 10:28:59 -0300 Subject: [PATCH 8/9] Always ensure `value` --- src/InfluxDB/Point.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/InfluxDB/Point.php b/src/InfluxDB/Point.php index 03336eb79..12cd119c4 100644 --- a/src/InfluxDB/Point.php +++ b/src/InfluxDB/Point.php @@ -54,8 +54,10 @@ class Point } $this->measurement = (string) $measurement; - $this->tags = $tags; - $this->fields = array_merge($additionalFields, array('value' => (float) $value)); + $this->tags = $tags; + $this->fields = $additionalFields; + + $this->fields['value'] = (float) $value; if ($timestamp && !$this->isValidTimeStamp($timestamp)) { throw new DatabaseException(sprintf('%s is not a valid timestamp', $timestamp)); @@ -113,11 +115,7 @@ class Point return true; } - if ($timestamp <= PHP_INT_MAX) { - return true; - } - - if ($timestamp >= ~PHP_INT_MAX) { + if ($timestamp <= PHP_INT_MAX && $timestamp >= ~PHP_INT_MAX) { return true; } From 2584146a44d7c82dd6225779a64779ee995c1f32 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 20 Jul 2015 10:31:41 -0300 Subject: [PATCH 9/9] Do not align assignments --- src/InfluxDB/Client.php | 24 +++++++++++------------ src/InfluxDB/Database.php | 2 +- src/InfluxDB/Database/RetentionPolicy.php | 6 +++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index bc87b5bdb..0f76df243 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -92,11 +92,11 @@ class Client $verifySSL = true, $timeout = 0 ) { - $this->host = (string) $host; - $this->port = (int) $port; - $this->username = (string) $username; - $this->password = (string) $password; - $this->timeout = (int) $timeout; + $this->host = (string) $host; + $this->port = (int) $port; + $this->username = (string) $username; + $this->password = (string) $password; + $this->timeout = (int) $timeout; $this->verifySSL = (bool) $verifySSL; if ($ssl) { @@ -107,7 +107,6 @@ class Client // the the base URI $this->baseURI = sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port); $this->httpClient = new \Guzzle\Http\Client($this->getBaseURI()); - } /** @@ -230,20 +229,20 @@ class Client { $connParams = parse_url($dsn); $schemeInfo = explode('+', $connParams['scheme']); - $dbName = null; - $modifier = null; - $scheme = $schemeInfo[0]; + $dbName = null; + $modifier = null; + $scheme = $schemeInfo[0]; if (isset($schemeInfo[1])) { $modifier = $schemeInfo[0]; - $scheme = $schemeInfo[1]; + $scheme = $schemeInfo[1]; } if ($scheme != 'influxdb') { throw new ClientException(sprintf('%s is not a valid scheme', $scheme)); } - $ssl = $modifier === 'https' ? true : false; + $ssl = $modifier === 'https' ? true : false; $dbName = $connParams['path'] ? substr($connParams['path'], 1) : null; $client = new self( @@ -276,8 +275,7 @@ class Client } /** - * @param array $points - * + * @param array $points * @return array */ protected function pointsToArray(array $points) diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 713fa2753..922155672 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -50,7 +50,7 @@ class Database throw new \InvalidArgumentException('No database name provided'); } - $this->name = $name; + $this->name = (string) $name; $this->client = $client; } diff --git a/src/InfluxDB/Database/RetentionPolicy.php b/src/InfluxDB/Database/RetentionPolicy.php index cbb8ae6b2..c795f4647 100644 --- a/src/InfluxDB/Database/RetentionPolicy.php +++ b/src/InfluxDB/Database/RetentionPolicy.php @@ -40,9 +40,9 @@ class RetentionPolicy */ public function __construct($name, $duration = '1d', $replication = 1, $default = false) { - $this->name = (string) $name; - $this->duration = $duration; + $this->name = (string) $name; + $this->duration = $duration; $this->replication = (int) $replication; - $this->default = (bool) $default; + $this->default = (bool) $default; } }