From c692658075365d0b7684aa324818eace647359bf Mon Sep 17 00:00:00 2001 From: tommyknocker Date: Wed, 11 Nov 2015 23:15:59 +0300 Subject: [PATCH] PHP DocBlock update. Some small PSR-2 code refactoring. --- MysqliDb.php | 269 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 168 insertions(+), 101 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 8f0a7117..9c21ad03 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1,5 +1,4 @@ host)) { - throw new Exception('Mysql host is not set'); + throw new Exception('MySQL host is not set'); } $this->_mysqli = new mysqli($this->host, $this->username, $this->password, $this->db, $this->port); @@ -258,6 +252,8 @@ public function connect() /** * A method to get mysqli object or create it in case needed + * + * @return mysqli */ public function mysqli() { @@ -274,7 +270,7 @@ public function mysqli() * * @uses $db = MySqliDb::getInstance(); * - * @return object Returns the current instance. + * @return MysqliDb Returns the current instance. */ public static function getInstance() { @@ -284,7 +280,7 @@ public static function getInstance() /** * Reset states after an execution * - * @return object Returns the current instance. + * @return MysqliDb Returns the current instance. */ protected function reset() { @@ -300,7 +296,7 @@ protected function reset() $this->_bindParams = array(''); // Create the empty 0 index $this->_query = null; $this->_queryOptions = array(); - $this->returnType = 'Array'; + $this->returnType = 'array'; $this->_nestJoin = false; $this->_forUpdate = false; $this->_lockInShareMode = false; @@ -311,36 +307,36 @@ protected function reset() } /** - * Helper function to create dbObject with Json return type + * Helper function to create dbObject with JSON return type * - * @return object + * @return MysqliDb */ - public function JsonBuilder() + public function jsonBuilder() { - $this->returnType = 'Json'; + $this->returnType = 'json'; return $this; } /** - * Helper function to create dbObject with Array return type + * Helper function to create dbObject with array return type * Added for consistency as thats default output type * - * @return object + * @return MysqliDb */ - public function ArrayBuilder() + public function arrayBuilder() { - $this->returnType = 'Array'; + $this->returnType = 'array'; return $this; } /** - * Helper function to create dbObject with Object return type. + * Helper function to create dbObject with object return type. * - * @return object + * @return MysqliDb */ - public function ObjectBuilder() + public function objectBuilder() { - $this->returnType = 'Object'; + $this->returnType = 'object'; return $this; } @@ -348,7 +344,8 @@ public function ObjectBuilder() * Method to set a prefix * * @param string $prefix Contains a tableprefix - * @return object + * + * @return MysqliDb */ public function setPrefix($prefix = '') { @@ -397,7 +394,7 @@ public function rawQuery($query, $bindParams = null) * @param string $query User-provided query to execute. * @param array $bindParams Variables array to bind to the SQL statement. * - * @return array Contains the returned row from the query. + * @return array|null Contains the returned row from the query. */ public function rawQueryOne($query, $bindParams = null) { @@ -440,9 +437,10 @@ public function rawQueryValue($query, $bindParams = null) } /** - * + * A method to perform select query + * * @param string $query Contains a user-provided select query. - * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) * * @return array Contains the returned rows from the query. */ @@ -463,7 +461,8 @@ public function query($query, $numRows = null) * * @uses $MySqliDb->setQueryOption('name'); * - * @param string/array $options The optons name of the query. + * @param string|array $options The optons name of the query. + * * @throws Exception * @return MysqliDb */ @@ -512,8 +511,9 @@ public function withTotalCount() * A convenient SELECT * function. * * @param string $tableName The name of the database table to work with. - * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) * or only $count + * @param string $columns Desired columns * * @return array Contains the returned rows from the select query. */ @@ -551,7 +551,8 @@ public function get($tableName, $numRows = null, $columns = '*') * A convenient SELECT * function to get one record. * * @param string $tableName The name of the database table to work with. - * + * @param string $columns Desired columns + * * @return array Contains the returned rows from the select query. */ public function getOne($tableName, $columns = '*') @@ -573,6 +574,7 @@ public function getOne($tableName, $columns = '*') * A convenient SELECT COLUMN function to get a single column value from one row * * @param string $tableName The name of the database table to work with. + * @param string $column The desired column * @param int $limit Limit of rows to select. Use null for unlimited..1 by default * * @return mixed Contains the value of a returned column / array of values @@ -602,10 +604,10 @@ public function getValue($tableName, $column, $limit = 1) /** * Insert method to add new row * - * @param _where[] = Array($cond, $whereProp, $operator, $whereValue); + $this->_where[] = array($cond, $whereProp, $operator, $whereValue); return $this; } @@ -725,13 +729,15 @@ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond * This function store update column's name and column name of the * autoincrement column * - * @param Array Variable with values - * @param String Variable value + * @param array $updateColumns Variable with values + * @param string $lastInsertId Variable value + * + * @return MysqliDb */ - public function onDuplicate($_updateColumns, $_lastInsertId = null) + public function onDuplicate($updateColumns, $lastInsertId = null) { - $this->_lastInsertId = $_lastInsertId; - $this->_updateColumns = $_updateColumns; + $this->_lastInsertId = $lastInsertId; + $this->_updateColumns = $updateColumns; return $this; } @@ -742,6 +748,7 @@ public function onDuplicate($_updateColumns, $_lastInsertId = null) * * @param string $whereProp The name of the database field. * @param mixed $whereValue The value of the database field. + * @param string $operator Comparison operator. Default is = * * @return MysqliDb */ @@ -749,13 +756,15 @@ public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=') { return $this->where($whereProp, $whereValue, $operator, 'OR'); } - /* + + /** * This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries. * * @uses $MySqliDb->having('SUM(tags) > 10') * * @param string $havingProp The name of the database field. * @param mixed $havingValue The value of the database field. + * @param string $operator Comparison operator. Default is = * * @return MysqliDb */ @@ -763,10 +772,10 @@ public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=') public function having($havingProp, $havingValue = null, $operator = null) { if ($operator) { - $havingValue = Array($operator => $havingValue); + $havingValue = array($operator => $havingValue); } - $this->_having[] = Array("AND", $havingValue, $havingProp); + $this->_having[] = array("AND", $havingValue, $havingProp); return $this; } @@ -777,6 +786,7 @@ public function having($havingProp, $havingValue = null, $operator = null) * * @param string $havingProp The name of the database field. * @param mixed $havingValue The value of the database field. + * @param string $operator Comparison operator. Default is = * * @return MysqliDb */ @@ -798,6 +808,7 @@ public function orHaving($havingProp, $havingValue = null, $operator = null) * @param string $joinTable The name of the table. * @param string $joinCondition the condition. * @param string $joinType 'LEFT', 'INNER' etc. + * * @throws Exception * @return MysqliDb */ @@ -826,6 +837,8 @@ public function join($joinTable, $joinCondition, $joinType = '') * * @param string $orderByField The name of the database field. * @param string $orderByDirection Order direction. + * @param array $customFields Fieldset for ORDER BY FIELD() ordering + * * @throws Exception * @return MysqliDb */ @@ -877,7 +890,7 @@ public function groupBy($groupByField) /** * This methods returns the ID of the last inserted item * - * @return integer The last inserted item ID. + * @return int The last inserted item ID. */ public function getInsertId() { @@ -957,12 +970,13 @@ protected function _bindParam($value) /** * Helper function to add variables into bind parameters array in bulk * - * @param Array Variable with values + * @param array $values Variable with values */ protected function _bindParams($values) { - foreach ($values as $value) + foreach ($values as $value) { $this->_bindParam($value); + } } /** @@ -970,7 +984,10 @@ protected function _bindParams($values) * its SQL part of the query according to operator in ' $operator ?' or * ' $operator ($subquery) ' formats * - * @param Array Variable with values + * @param string $operator + * @param mixed $value Variable with values + * + * @return string */ protected function _buildPair($operator, $value) { @@ -988,10 +1005,11 @@ protected function _buildPair($operator, $value) /** * Internal function to build and execute INSERT/REPLACE calls * - * @param _buildOrderBy(); $this->_buildLimit($numRows); $this->_buildOnDuplicate($tableData); + if ($this->_forUpdate) { $this->_query .= ' FOR UPDATE'; } @@ -1116,7 +1135,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) $this->count = 0; while ($stmt->fetch()) { - if ($this->returnType == 'Object') { + if ($this->returnType == 'object') { $result = new stdClass (); foreach ($row as $key => $val) { if (is_array($val)) { @@ -1124,8 +1143,9 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) foreach ($val as $k => $v) { $result->$key->$k = $v; } - } else + } else { $result->$key = $val; + } } } else { $result = array(); @@ -1134,15 +1154,17 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) foreach ($val as $k => $v) { $result[$key][$k] = $v; } - } else + } else { $result[$key] = $val; + } } } $this->count++; - if ($this->_mapKey) + if ($this->_mapKey) { $results[$row[$this->_mapKey]] = count($row) > 2 ? $result : end($result); - else + } else { array_push($results, $result); + } } if ($shouldStoreResult) { @@ -1162,7 +1184,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) $this->totalCount = $totalCount[0]; } - if ($this->returnType == 'Json') { + if ($this->returnType == 'json') { return json_encode($results); } @@ -1171,6 +1193,8 @@ protected function _dynamicBindResults(mysqli_stmt $stmt) /** * Abstraction method that will build an JOIN part of the query + * + * @return void */ protected function _buildJoin() { @@ -1192,6 +1216,11 @@ protected function _buildJoin() } /** + * Insert/Update query helper + * + * @param array $tableData + * @param array $tableColumns + * @param bool $isInsert INSERT operation flag * * @throws Exception */ @@ -1231,10 +1260,11 @@ public function _buildDataPairs($tableData, $tableColumns, $isInsert) } break; case '[N]': - if ($val == null) + if ($val == null) { $this->_query .= "!" . $column . ", "; - else + } else { $this->_query .= "!" . $val . ", "; + } break; default: throw new Exception("Wrong operation"); @@ -1246,12 +1276,12 @@ public function _buildDataPairs($tableData, $tableColumns, $isInsert) /** * Helper function to add variables into the query statement * - * @param Array Variable with values + * @param array $tableData Variable with values */ protected function _buildOnDuplicate($tableData) { if (is_array($this->_updateColumns) && !empty($this->_updateColumns)) { - $this->_query .= " on duplicate key update "; + $this->_query .= " ON DUPLICATE KEY UPDATE "; if ($this->_lastInsertId) { $this->_query .= $this->_lastInsertId . "=LAST_INSERT_ID (" . $this->_lastInsertId . "), "; } @@ -1261,8 +1291,9 @@ protected function _buildOnDuplicate($tableData) if (is_numeric($key)) { $this->_updateColumns[$val] = ''; unset($this->_updateColumns[$key]); - } else + } else { $tableData[$key] = $val; + } } $this->_buildDataPairs($tableData, array_keys($this->_updateColumns), false); } @@ -1270,6 +1301,8 @@ protected function _buildOnDuplicate($tableData) /** * Abstraction method that will build an INSERT or UPDATE part of the query + * + * @param array $tableData */ protected function _buildInsertQuery($tableData) { @@ -1294,6 +1327,9 @@ protected function _buildInsertQuery($tableData) /** * Abstraction method that will build the part of the WHERE conditions + * + * @param string $operator + * @param array $conditions */ protected function _buildCondition($operator, &$conditions) { @@ -1346,6 +1382,7 @@ protected function _buildCondition($operator, &$conditions) /** * Abstraction method that will build the GROUP BY part of the WHERE statement * + * @return void */ protected function _buildGroupBy() { @@ -1365,6 +1402,7 @@ protected function _buildGroupBy() /** * Abstraction method that will build the LIMIT part of the WHERE statement * + * @return void */ protected function _buildOrderBy() { @@ -1387,8 +1425,10 @@ protected function _buildOrderBy() /** * Abstraction method that will build the LIMIT part of the WHERE statement * - * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * @param int|array $numRows Array to define SQL limit in format Array ($count, $offset) * or only $count + * + * @return void */ protected function _buildLimit($numRows) { @@ -1424,6 +1464,8 @@ protected function _prepareQuery() /** * Close connection + * + * @return void */ public function __destruct() { @@ -1438,11 +1480,13 @@ public function __destruct() } /** + * Referenced data array is required by mysqli since PHP 5.3+ + * * @param array $arr * * @return array */ - protected function refValues(Array &$arr) + protected function refValues(array &$arr) { //Reference in the function arguments are required for HHVM to work //https://github.com/facebook/hhvm/issues/5155 @@ -1459,8 +1503,9 @@ protected function refValues(Array &$arr) /** * Function to replace ? with variables from bind variable + * * @param string $str - * @param Array $vals + * @param array $vals * * @return string */ @@ -1531,16 +1576,17 @@ public function getSubQuery() $this->reset(); return $val; } + /* Helper functions */ /** * Method returns generated interval function as a string * - * @param string interval in the formats: + * @param string $diff interval in the formats: * "1", "-1d" or "- 1 day" -- For interval - 1 day * Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear * Default null; - * @param string Initial date + * @param string $func Initial date * * @return string */ @@ -1576,11 +1622,11 @@ public function interval($diff, $func = "NOW()") /** * Method returns generated interval function as an insert/update function * - * @param string interval in the formats: + * @param string $diff interval in the formats: * "1", "-1d" or "- 1 day" -- For interval - 1 day * Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear * Default null; - * @param string Initial date + * @param string $func Initial date * * @return array */ @@ -1591,8 +1637,11 @@ public function now($diff = null, $func = "NOW()") /** * Method generates incremental function call - * @param int increment by int or float. 1 by default + * + * @param int $num increment by int or float. 1 by default + * * @throws Exception + * @return array */ public function inc($num = 1) { @@ -1604,7 +1653,10 @@ public function inc($num = 1) /** * Method generates decrimental function call - * @param int increment by int or float. 1 by default + * + * @param int $num increment by int or float. 1 by default + * + * @return array */ public function dec($num = 1) { @@ -1616,7 +1668,10 @@ public function dec($num = 1) /** * Method generates change boolean function call - * @param string column name. null by default + * + * @param string $col column name. null by default + * + * @return array */ public function not($col = null) { @@ -1625,15 +1680,23 @@ public function not($col = null) /** * Method generates user defined function call - * @param string user function body + * + * @param string $expr user function body + * @param array $bindParams + * + * @return array */ public function func($expr, $bindParams = null) { - return array("[F]" => Array($expr, $bindParams)); + return array("[F]" => array($expr, $bindParams)); } /** * Method creates new mysqlidb object for a subquery generation + * + * @param string $subQueryAlias + * + * @return MysqliDb */ public static function subQuery($subQueryAlias = "") { @@ -1643,7 +1706,7 @@ public static function subQuery($subQueryAlias = "") /** * Method returns a copy of a mysqlidb subquery object * - * @param object new mysqlidb object + * @return MysqliDb new mysqlidb object */ public function copy() { @@ -1710,7 +1773,9 @@ public function _transaction_status_check() * * @param bool $enabled Enable execution time tracking * @param string $stripPrefix Prefix to strip from the path in exec log - * */ + * + * @return MysqliDb + */ public function setTrace($enabled, $stripPrefix = null) { $this->traceEnabled = $enabled; @@ -1740,7 +1805,7 @@ private function _traceGetCaller() * * @param array $tables Table name or an Array of table names to check * - * @returns boolean True if table exists + * @return bool True if table exists */ public function tableExists($tables) { @@ -1761,10 +1826,12 @@ public function tableExists($tables) /** * Return result as an associative array with $idField field value used as a record key + * + * Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise + * + * @param string $idField field name to use for a mapped element key * - * @param String $idField field name to use for a mapped element key - * - * @return Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise + * @return MysqliDb */ public function map($idField) {