| Conditions | 19 |
| Paths | 306 |
| Total Lines | 98 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 92 | public function execute($sql, Array $params = []) |
||
| 93 | { |
||
| 94 | $this->numRows = 0; |
||
| 95 | $this->numFields = 0; |
||
| 96 | $this->rowsAffected = 0; |
||
| 97 | |||
| 98 | $this->arrayResult = null; |
||
| 99 | |||
| 100 | # Bound variables |
||
| 101 | if (count($params)) |
||
| 102 | { |
||
| 103 | $this->result = $stmt = @$this->dbconn->prepare($sql); |
||
| 104 | |||
| 105 | if (!$stmt) |
||
| 106 | { |
||
| 107 | $this->error($this->dbconn->errno, $this->dbconn->error); |
||
| 108 | throw new Exception\InvalidQueryException($this->dbconn->error, $this->dbconn->errno); |
||
| 109 | } |
||
| 110 | |||
| 111 | $param_values = array_values($params); |
||
| 112 | |||
| 113 | $n_params = count($param_values); |
||
| 114 | $bind_values = []; |
||
| 115 | $bind_types = ""; |
||
| 116 | |||
| 117 | for ($i = 0; $i < $n_params; $i++) |
||
| 118 | { |
||
| 119 | if (is_string($param_values[$i])) |
||
| 120 | $bind_types .= 's'; |
||
| 121 | else if(is_float($param_values[$i])) |
||
| 122 | $bind_types .= 'd'; |
||
| 123 | # [POSSIBLE BUG] - To Future revision (What about non-string and non-decimal types ?) |
||
| 124 | else |
||
| 125 | $bind_types .= 's'; |
||
| 126 | |||
| 127 | $bind_values[] = '$param_values[' . $i . ']'; |
||
| 128 | } |
||
| 129 | |||
| 130 | $values = implode(', ', $bind_values); |
||
| 131 | eval('$stmt->bind_param(\'' . $bind_types . '\', ' . $values . ');'); |
||
| 132 | |||
| 133 | $r = $stmt->execute(); |
||
| 134 | |||
| 135 | if ($r) |
||
| 136 | { |
||
| 137 | if (is_object($stmt) && get_class($stmt) == 'mysqli_stmt') |
||
| 138 | { |
||
| 139 | $res = $this->result->get_result(); |
||
| 140 | |||
| 141 | /* |
||
| 142 | * if $res is false then there aren't results. |
||
| 143 | * It is useful to prevent rollback transactions on insert statements because |
||
| 144 | * insert statement do not free results. |
||
| 145 | */ |
||
| 146 | if ($res) |
||
| 147 | $this->result = $res; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | else |
||
| 152 | { |
||
| 153 | $prev_error_handler = set_error_handler(['\Drone\Error\ErrorHandler', 'errorControlOperator'], E_ALL); |
||
| 154 | |||
| 155 | // may be throw a Fatal error (Ex: Maximum execution time) |
||
| 156 | $r = $this->result = $this->dbconn->query($sql); |
||
| 157 | |||
| 158 | set_error_handler($prev_error_handler); |
||
| 159 | } |
||
| 160 | |||
| 161 | if (!$r) |
||
| 162 | { |
||
| 163 | $this->error($this->dbconn->errno, $this->dbconn->error); |
||
| 164 | throw new Exception\InvalidQueryException($this->dbconn->error, $this->dbconn->errno); |
||
| 165 | } |
||
| 166 | |||
| 167 | # identify SELECT, SHOW, DESCRIBE or EXPLAIN queries |
||
| 168 | if (is_object($this->result) && property_exists($this->result, 'num_rows')) |
||
| 169 | $this->numRows = $this->result->num_rows; |
||
| 170 | else |
||
| 171 | { |
||
| 172 | # affected_rows return the same of num_rows on select statements! |
||
| 173 | if (property_exists($this->dbconn, 'affected_rows')) |
||
| 174 | $this->rowsAffected = $this->dbconn->affected_rows; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (property_exists($this->dbconn, 'field_count')) |
||
| 178 | $this->numFields = $this->dbconn->field_count; |
||
| 179 | |||
| 180 | if ($this->transac_mode) |
||
| 181 | $this->transac_result = is_null($this->transac_result) ? $this->result: $this->transac_result && $this->result; |
||
| 182 | |||
| 183 | /* |
||
| 184 | * Because mysqli_query() returns FALSE on failure, a mysqli_result object for SELECT, SHOW, DESCRIBE or EXPLAIN queries, |
||
| 185 | * and TRUE for other successful queries, it should be handled to return only objects or resources. |
||
| 186 | * |
||
| 187 | * Ref: http://php.net/manual/en/mysqli.query.php |
||
| 188 | */ |
||
| 189 | return is_bool($this->result) ? $this->dbconn : $this->result; |
||
| 190 | } |
||
| 277 | } |