From 8de266993fa8845f6ae2f50f9e372c2b98a7cf91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A4=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= <15100894+00dev@users.noreply.github.com> Date: Tue, 5 Feb 2019 13:05:50 +0300 Subject: [PATCH] Remove redundant code and fix phpDoc 1. Method `rawQuery()` can't return false because method `error()` can't return any result (it throws an exception or triggers error); 2. As `rawQuery()` always returns truthy value `if ($res = $this->rawQuery())...` construction is no sense; 3. `mysqli_fetch_array()` can't return FALSE. It returns neither array or NULL; 4. As `fetch()` returns array or NULL `getRow()` also returns array or NULL; 5. `getOne()` doesn't free resultset if the query have empty result (not sure if it's bad, but I think it might be better if we clean all resources after method calling). --- safemysql.class.php | 82 ++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/safemysql.class.php b/safemysql.class.php index 4296d65..80e0f03 100644 --- a/safemysql.class.php +++ b/safemysql.class.php @@ -133,7 +133,7 @@ function __construct($opt = array()) * * @param string $query - an SQL query with placeholders * @param mixed $arg,... unlimited number of arguments to match placeholders in the query - * @return resource|FALSE whatever mysqli_query returns + * @return mysql_result|true whatever mysqli_query returns */ public function query() { @@ -145,7 +145,7 @@ public function query() * * @param resource $result - myqli result * @param int $mode - optional fetch mode, RESULT_ASSOC|RESULT_NUM, default RESULT_ASSOC - * @return array|FALSE whatever mysqli_fetch_array returns + * @return array|null whatever mysqli_fetch_array returns */ public function fetch($result,$mode=self::RESULT_ASSOC) { @@ -204,14 +204,11 @@ public function free($result) */ public function getOne() { - $query = $this->prepareQuery(func_get_args()); - if ($res = $this->rawQuery($query)) - { - $row = $this->fetch($res); - if (is_array($row)) { - return reset($row); - } - $this->free($res); + $res = $this->rawQuery($this->prepareQuery(func_get_args())); + $row = $this->fetch($res); + $this->free($res); + if (is_array($row)) { + return reset($row); } return FALSE; } @@ -225,17 +222,14 @@ public function getOne() * * @param string $query - an SQL query with placeholders * @param mixed $arg,... unlimited number of arguments to match placeholders in the query - * @return array|FALSE either associative array contains first row of resultset or FALSE if none found + * @return array|null either associative array contains first row of resultset or NULL if none found */ public function getRow() { - $query = $this->prepareQuery(func_get_args()); - if ($res = $this->rawQuery($query)) { - $ret = $this->fetch($res); - $this->free($res); - return $ret; - } - return FALSE; + $res = $this->rawQuery($this->prepareQuery(func_get_args())); + $ret = $this->fetch($res); + $this->free($res); + return $ret; } /** @@ -252,15 +246,12 @@ public function getRow() public function getCol() { $ret = array(); - $query = $this->prepareQuery(func_get_args()); - if ( $res = $this->rawQuery($query) ) + $res = $this->rawQuery($this->prepareQuery(func_get_args())); + while ($row = $this->fetch($res)) { - while($row = $this->fetch($res)) - { - $ret[] = reset($row); - } - $this->free($res); + $ret[] = reset($row); } + $this->free($res); return $ret; } @@ -277,16 +268,13 @@ public function getCol() */ public function getAll() { - $ret = array(); - $query = $this->prepareQuery(func_get_args()); - if ( $res = $this->rawQuery($query) ) + $ret = array(); + $res = $this->rawQuery($this->prepareQuery(func_get_args())); + while ($row = $this->fetch($res)) { - while($row = $this->fetch($res)) - { - $ret[] = $row; - } - $this->free($res); + $ret[] = $row; } + $this->free($res); return $ret; } @@ -306,17 +294,14 @@ public function getInd() { $args = func_get_args(); $index = array_shift($args); - $query = $this->prepareQuery($args); $ret = array(); - if ( $res = $this->rawQuery($query) ) + $res = $this->rawQuery($this->prepareQuery($args)); + while ($row = $this->fetch($res)) { - while($row = $this->fetch($res)) - { - $ret[$row[$index]] = $row; - } - $this->free($res); + $ret[$row[$index]] = $row; } + $this->free($res); return $ret; } @@ -335,19 +320,16 @@ public function getIndCol() { $args = func_get_args(); $index = array_shift($args); - $query = $this->prepareQuery($args); $ret = array(); - if ( $res = $this->rawQuery($query) ) + $res = $this->rawQuery($this->prepareQuery($args)); + while ($row = $this->fetch($res)) { - while($row = $this->fetch($res)) - { - $key = $row[$index]; - unset($row[$index]); - $ret[$key] = reset($row); - } - $this->free($res); + $key = $row[$index]; + unset($row[$index]); + $ret[$key] = reset($row); } + $this->free($res); return $ret; } @@ -458,7 +440,7 @@ public function getStats() * also logs some stats like profiling info and error message * * @param string $query - a regular SQL query - * @return mysqli result resource or FALSE on error + * @return mysqli_result|true result or true if query not provides a result */ protected function rawQuery($query) {