| Total Complexity | 47 |
| Total Lines | 346 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class Query implements QueryInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var DriverInterface |
||
| 17 | */ |
||
| 18 | protected $driver; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var UtilInterface |
||
| 22 | */ |
||
| 23 | protected $util; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var TranslatorInterface |
||
| 27 | */ |
||
| 28 | protected $trans; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ConnectionInterface |
||
| 32 | */ |
||
| 33 | protected $connection; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The last error code |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $errno = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The last error message |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $error = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The number of rows affected by the last query |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $affectedRows; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The constructor |
||
| 58 | * |
||
| 59 | * @param DriverInterface $driver |
||
| 60 | * @param UtilInterface $util |
||
| 61 | * @param TranslatorInterface $trans |
||
| 62 | * @param ConnectionInterface $connection |
||
| 63 | */ |
||
| 64 | public function __construct(DriverInterface $driver, UtilInterface $util, TranslatorInterface $trans, ConnectionInterface $connection) |
||
| 65 | { |
||
| 66 | $this->driver = $driver; |
||
| 67 | $this->util = $util; |
||
| 68 | $this->trans = $trans; |
||
| 69 | $this->connection = $connection; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritDoc |
||
| 74 | */ |
||
| 75 | public function schema() |
||
| 76 | { |
||
| 77 | return ""; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @inheritDoc |
||
| 82 | */ |
||
| 83 | public function select(string $table, array $select, array $where, |
||
| 84 | array $group, array $order = [], int $limit = 1, int $page = 0) |
||
| 85 | { |
||
| 86 | $query = $this->driver->buildSelectQuery($table, $select, $where, $group, $order, $limit, $page); |
||
| 87 | // $start = microtime(true); |
||
| 88 | return $this->connection->query($query); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritDoc |
||
| 93 | */ |
||
| 94 | public function insert(string $table, array $values) |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | public function update(string $table, array $values, string $queryWhere, int $limit = 0, string $separator = "\n") |
||
| 108 | { |
||
| 109 | $assignments = []; |
||
| 110 | foreach ($values as $name => $value) { |
||
| 111 | $assignments[] = "$name = $value"; |
||
| 112 | } |
||
| 113 | $query = $this->driver->table($table) . " SET$separator" . implode(",$separator", $assignments); |
||
| 114 | if (!$limit) { |
||
| 115 | return $this->driver->queries('UPDATE ' . $query . $queryWhere); |
||
| 116 | } |
||
| 117 | return $this->driver->queries('UPDATE' . $this->driver->limitToOne($table, $query, $queryWhere, $separator)); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritDoc |
||
| 122 | */ |
||
| 123 | public function delete(string $table, string $queryWhere, int $limit = 0) |
||
| 124 | { |
||
| 125 | $query = 'FROM ' . $this->driver->table($table); |
||
| 126 | if (!$limit) { |
||
| 127 | return $this->driver->queries("DELETE $query $queryWhere"); |
||
| 128 | } |
||
| 129 | return $this->driver->queries('DELETE' . $this->driver->limitToOne($table, $query, $queryWhere)); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritDoc |
||
| 134 | */ |
||
| 135 | public function explain(ConnectionInterface $connection, string $query) |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | */ |
||
| 143 | public function slowQuery(string $query, int $timeout) |
||
| 144 | { |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritDoc |
||
| 149 | */ |
||
| 150 | public function setError(string $error = '') |
||
| 151 | { |
||
| 152 | $this->error = $error; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritDoc |
||
| 157 | */ |
||
| 158 | public function error() |
||
| 159 | { |
||
| 160 | return $this->error; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritDoc |
||
| 165 | */ |
||
| 166 | public function hasError() |
||
| 167 | { |
||
| 168 | return $this->error !== ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritDoc |
||
| 173 | */ |
||
| 174 | public function setErrno($errno) |
||
| 175 | { |
||
| 176 | $this->errno = $errno; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritDoc |
||
| 181 | */ |
||
| 182 | public function errno() |
||
| 183 | { |
||
| 184 | return $this->errno; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritDoc |
||
| 189 | */ |
||
| 190 | public function hasErrno() |
||
| 191 | { |
||
| 192 | return $this->errno !== 0; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritDoc |
||
| 197 | */ |
||
| 198 | public function setAffectedRows($affectedRows) |
||
| 199 | { |
||
| 200 | $this->affectedRows = $affectedRows; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @inheritDoc |
||
| 205 | */ |
||
| 206 | public function affectedRows() |
||
| 207 | { |
||
| 208 | return $this->affectedRows; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritDoc |
||
| 213 | */ |
||
| 214 | public function queries(string $query = '') |
||
| 215 | { |
||
| 216 | static $queries = []; |
||
| 217 | static $start; |
||
| 218 | if (!$start) { |
||
| 219 | $start = microtime(true); |
||
| 220 | } |
||
| 221 | if ($query === '') { |
||
| 222 | // return executed queries |
||
| 223 | return [implode("\n", $queries), $this->trans->formatTime($start)]; |
||
| 224 | } |
||
| 225 | $queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";"; |
||
| 226 | return $this->connection->query($query); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @inheritDoc |
||
| 231 | */ |
||
| 232 | public function applyQueries(string $query, array $tables, $escape = null) |
||
| 233 | { |
||
| 234 | if (!$escape) { |
||
| 235 | $escape = function ($table) { |
||
| 236 | return $this->driver->table($table); |
||
| 237 | }; |
||
| 238 | } |
||
| 239 | foreach ($tables as $table) { |
||
| 240 | if (!$this->queries("$query " . $escape($table))) { |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritDoc |
||
| 249 | */ |
||
| 250 | public function values(string $query, $column = 0) |
||
| 251 | { |
||
| 252 | $values = []; |
||
| 253 | $statement = $this->connection->query($query); |
||
| 254 | if (is_object($statement)) { |
||
| 255 | while ($row = $statement->fetchRow()) { |
||
| 256 | $values[] = $row[$column]; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | return $values; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritDoc |
||
| 264 | */ |
||
| 265 | public function keyValues(string $query, ConnectionInterface $connection = null, bool $setKeys = true) |
||
| 266 | { |
||
| 267 | if (!is_object($connection)) { |
||
| 268 | $connection = $this->connection; |
||
| 269 | } |
||
| 270 | $values = []; |
||
| 271 | $statement = $connection->query($query); |
||
| 272 | if (is_object($statement)) { |
||
| 273 | while ($row = $statement->fetchRow()) { |
||
| 274 | if ($setKeys) { |
||
| 275 | $values[$row[0]] = $row[1]; |
||
| 276 | } else { |
||
| 277 | $values[] = $row[0]; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | return $values; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritDoc |
||
| 286 | */ |
||
| 287 | public function rows(string $query, ConnectionInterface $connection = null) |
||
| 288 | { |
||
| 289 | if (!$connection) { |
||
| 290 | $connection = $this->connection; |
||
| 291 | } |
||
| 292 | $statement = $connection->query($query); |
||
| 293 | if (!is_object($statement)) { // can return true |
||
| 294 | return []; |
||
| 295 | } |
||
| 296 | $rows = []; |
||
| 297 | while ($row = $statement->fetchAssoc()) { |
||
| 298 | $rows[] = $row; |
||
| 299 | } |
||
| 300 | return $rows; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritDoc |
||
| 305 | */ |
||
| 306 | public function removeDefiner(string $query) |
||
| 307 | { |
||
| 308 | return preg_replace('~^([A-Z =]+) DEFINER=`' . |
||
| 309 | preg_replace('~@(.*)~', '`@`(%|\1)', $this->user()) . |
||
| 310 | '`~', '\1', $query); //! proper escaping of user |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @inheritDoc |
||
| 315 | */ |
||
| 316 | public function begin() |
||
| 317 | { |
||
| 318 | return $this->connection->query("BEGIN"); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @inheritDoc |
||
| 323 | */ |
||
| 324 | public function commit() |
||
| 325 | { |
||
| 326 | return $this->connection->query("COMMIT"); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritDoc |
||
| 331 | */ |
||
| 332 | public function rollback() |
||
| 333 | { |
||
| 334 | return $this->connection->query("ROLLBACK"); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @inheritDoc |
||
| 339 | */ |
||
| 340 | public function countRows(TableEntity $tableStatus, array $where) |
||
| 341 | { |
||
| 342 | return null; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @inheritDoc |
||
| 347 | */ |
||
| 348 | public function convertSearch(string $idf, array $val, TableFieldEntity $field) |
||
| 349 | { |
||
| 350 | return $idf; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @inheritDoc |
||
| 355 | */ |
||
| 356 | public function view(string $name) |
||
| 359 | } |
||
| 360 | } |
||
| 361 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: