| Total Complexity | 40 |
| Total Lines | 258 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MySQL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQL, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MySQL extends AbstractDriver implements DriverInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * {@inheritDoc} |
||
| 22 | * |
||
| 23 | * @var object |
||
| 24 | */ |
||
| 25 | protected $dbconn; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritDoc} |
||
| 29 | * |
||
| 30 | * @param array $options |
||
| 31 | */ |
||
| 32 | public function __construct($options) |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Connects to database |
||
| 47 | * |
||
| 48 | * @throws RuntimeException |
||
| 49 | * @throws Exception\ConnectionException |
||
| 50 | * |
||
| 51 | * @return \mysqli |
||
| 52 | */ |
||
| 53 | public function connect() |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Excecutes a statement |
||
| 83 | * |
||
| 84 | * @param string $sql |
||
| 85 | * @param array $params |
||
| 86 | * |
||
| 87 | * @throws RuntimeException |
||
| 88 | * @throws Exception\InvalidQueryException |
||
| 89 | * |
||
| 90 | * @return mysqli_result |
||
| 91 | */ |
||
| 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 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritDoc} |
||
| 194 | */ |
||
| 195 | public function commit() |
||
| 196 | { |
||
| 197 | return $this->dbconn->commit(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritDoc} |
||
| 202 | */ |
||
| 203 | public function rollback() |
||
| 204 | { |
||
| 205 | return $this->dbconn->rollback(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritDoc} |
||
| 210 | */ |
||
| 211 | public function disconnect() |
||
| 212 | { |
||
| 213 | parent::disconnect(); |
||
| 214 | |||
| 215 | if ($this->dbconn->close()) |
||
| 216 | { |
||
| 217 | $this->dbconn = null; |
||
| 218 | return true; |
||
| 219 | } |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritDoc} |
||
| 226 | */ |
||
| 227 | public function autocommit($value) |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns an array with the rows fetched |
||
| 235 | * |
||
| 236 | * @throws LogicException |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | protected function toArray() |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * By default __destruct() disconnects to database |
||
| 268 | * |
||
| 269 | * @return null |
||
| 270 | */ |
||
| 271 | public function __destruct() |
||
| 276 | } |
||
| 277 | } |