| Total Complexity | 62 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CommandAdmin 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 CommandAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class CommandAdmin extends AbstractAdmin |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) |
||
| 14 | * //! PDO - silent error |
||
| 15 | * |
||
| 16 | * @var ConnectionInterface |
||
| 17 | */ |
||
| 18 | protected $connection = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Open a second connection to the server |
||
| 22 | * |
||
| 23 | * @return ConnectionInterface|null |
||
| 24 | */ |
||
| 25 | private function connection() |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param mixed $value |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | // protected function editLink($value) |
||
| 43 | // { |
||
| 44 | // $link = ''; |
||
| 45 | // if (isset($links[$key]) && !$columns[$links[$key]]) { |
||
| 46 | // if ($orgtables && $this->driver->jush() == 'sql') { // MySQL EXPLAIN |
||
| 47 | // $table = $row[\array_search('table=', $links)]; |
||
| 48 | // $link = /*ME .*/ $links[$key] . |
||
| 49 | // \urlencode($orgtables[$table] != '' ? $orgtables[$table] : $table); |
||
| 50 | // } else { |
||
| 51 | // $link = /*ME .*/ 'edit=' . \urlencode($links[$key]); |
||
| 52 | // foreach ($indexes[$links[$key]] as $col => $j) { |
||
| 53 | // $link .= '&where' . \urlencode('[' . |
||
| 54 | // $this->util->bracketEscape($col) . ']') . '=' . \urlencode($row[$j]); |
||
| 55 | // } |
||
| 56 | // } |
||
| 57 | // } elseif ($this->util->isUrl($val)) { |
||
| 58 | // $link = $val; |
||
| 59 | // } |
||
| 60 | // } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $row |
||
| 64 | * @param array $blobs |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | protected function values(array $row, array $blobs) |
||
| 69 | { |
||
| 70 | $values = []; |
||
| 71 | foreach ($row as $key => $value) { |
||
| 72 | // $link = $this->editLink($val); |
||
| 73 | if ($value === null) { |
||
| 74 | $value = '<i>NULL</i>'; |
||
| 75 | } elseif (isset($blobs[$key]) && $blobs[$key] && !$this->util->isUtf8($value)) { |
||
| 76 | //! link to download |
||
| 77 | $value = '<i>' . $this->trans->lang('%d byte(s)', \strlen($value)) . '</i>'; |
||
| 78 | } else { |
||
| 79 | $value = $this->util->html($value); |
||
| 80 | if (isset($types[$key]) && $types[$key] == 254) { // 254 - char |
||
|
|
|||
| 81 | $value = "<code>$value</code>"; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | $values[$key] = $value; |
||
| 85 | } |
||
| 86 | return $values; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param mixed $statement |
||
| 91 | * @param int $rowCount |
||
| 92 | * @param int $limit |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | private function message($statement, int $rowCount, int $limit) |
||
| 97 | { |
||
| 98 | $numRows = $statement->rowCount(); |
||
| 99 | $message = ''; |
||
| 100 | if ($numRows > 0) { |
||
| 101 | if ($limit > 0 && $numRows > $limit) { |
||
| 102 | $message = $this->trans->lang('%d / ', $limit); |
||
| 103 | } |
||
| 104 | $message .= $this->trans->lang('%d row(s)', $numRows); |
||
| 105 | } |
||
| 106 | return $message; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param mixed $field |
||
| 111 | * @param array $orgtables |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | // protected function indexes($field, array $orgtables) |
||
| 116 | // { |
||
| 117 | // static $links = []; // colno => orgtable - create links from these columns |
||
| 118 | // static $indexes = []; // orgtable => array(column => colno) - primary keys |
||
| 119 | // static $columns = []; // orgtable => array(column => ) - not selected columns in primary key |
||
| 120 | |||
| 121 | // if (!empty($this->orgtables) && $this->driver->jush() == 'sql') { // MySQL EXPLAIN |
||
| 122 | // $links[$j] = ($name == 'table' ? 'table=' : ($name == 'possible_keys' ? 'indexes=' : null)); |
||
| 123 | // } elseif ($orgtable != '') { |
||
| 124 | // if (!isset($indexes[$orgtable])) { |
||
| 125 | // // find primary key in each table |
||
| 126 | // $indexes[$orgtable] = []; |
||
| 127 | // foreach ($this->driver->indexes($orgtable, $connection) as $index) { |
||
| 128 | // if ($index->type == 'PRIMARY') { |
||
| 129 | // $indexes[$orgtable] = \array_flip($index->columns); |
||
| 130 | // break; |
||
| 131 | // } |
||
| 132 | // } |
||
| 133 | // $columns[$orgtable] = $indexes[$orgtable]; |
||
| 134 | // } |
||
| 135 | // if (isset($columns[$orgtable][$orgname])) { |
||
| 136 | // unset($columns[$orgtable][$orgname]); |
||
| 137 | // $indexes[$orgtable][$orgname] = $j; |
||
| 138 | // $links[$j] = $orgtable; |
||
| 139 | // } |
||
| 140 | // } |
||
| 141 | // } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Print select result |
||
| 145 | * From editing.inc.php |
||
| 146 | * |
||
| 147 | * @param mixed $statement |
||
| 148 | * @param int $limit |
||
| 149 | * @param array $orgtables |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | protected function select($statement, $limit = 0, $orgtables = []) |
||
| 154 | { |
||
| 155 | $blobs = []; // colno => bool - display bytes for blobs |
||
| 156 | $types = []; // colno => type - display char in <code> |
||
| 157 | $tables = []; // table => orgtable - mapping to use in EXPLAIN |
||
| 158 | $headers = []; |
||
| 159 | |||
| 160 | $colCount = 0; |
||
| 161 | $rowCount = 0; |
||
| 162 | $details = []; |
||
| 163 | // Fetch the first row. |
||
| 164 | if (!($row = $statement->fetchRow())) { |
||
| 165 | // Empty resultset. |
||
| 166 | $message = $this->trans->lang('No rows.'); |
||
| 167 | return \compact('tables', 'headers', 'details', 'message'); |
||
| 168 | } |
||
| 169 | |||
| 170 | // Table headers. |
||
| 171 | $colCount = \count($row); |
||
| 172 | for ($j = 0; $j < $colCount; $j++) { |
||
| 173 | $field = $statement->fetchField(); |
||
| 174 | // PostgreSQL fix: the table field can be missing. |
||
| 175 | $tables[$field->tableName()] = $field->orgTable(); |
||
| 176 | // $this->indexes($field); |
||
| 177 | if ($field->isBinary()) { |
||
| 178 | $blobs[$j] = true; |
||
| 179 | } |
||
| 180 | $types[$j] = $field->type(); // Some drivers don't set the type field. |
||
| 181 | $headers[] = $this->util->html($field->name()); |
||
| 182 | } |
||
| 183 | |||
| 184 | // Table rows (the first was already fetched). |
||
| 185 | do { |
||
| 186 | $rowCount++; |
||
| 187 | $details[] = $this->values($row, $blobs); |
||
| 188 | } while (($limit === 0 || $rowCount < $limit) && ($row = $statement->fetchRow())); |
||
| 189 | |||
| 190 | $message = $this->message($statement, $rowCount, $limit); |
||
| 191 | return \compact('tables', 'headers', 'details', 'message'); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $queries The queries to execute |
||
| 196 | * @param int $offset |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | private function nextQueryPos(string &$queries, int &$offset) |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $query The query to execute |
||
| 246 | * @param array $results |
||
| 247 | * @param int $limit The max number of rows to return |
||
| 248 | * @param bool $errorStops Stop executing the requests in case of error |
||
| 249 | * @param bool $onlyErrors Return only errors |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | private function executeQuery(string $query, array &$results, int $limit, bool $errorStops, bool $onlyErrors) |
||
| 254 | { |
||
| 255 | $space = "(?:\\s|/\\*[\s\S]*?\\*/|(?:#|-- )[^\n]*\n?|--\r?\n)"; |
||
| 256 | $messages = []; |
||
| 257 | $errors = []; |
||
| 258 | $select = null; |
||
| 259 | |||
| 260 | if ($this->driver->jush() == 'sqlite' && \preg_match("~^$space*+ATTACH\\b~i", $query, $match)) { |
||
| 261 | // PHP doesn't support setting SQLITE_LIMIT_ATTACHED |
||
| 262 | // $errors[] = " <a href='#sql-$commands'>$commands</a>"; |
||
| 263 | $errors[] = $this->trans->lang('ATTACH queries are not supported.'); |
||
| 264 | $results[] = \compact('query', 'errors', 'messages', 'select'); |
||
| 265 | // return $errorStops ? false : true; |
||
| 266 | return !$errorStops; |
||
| 267 | } |
||
| 268 | |||
| 269 | $connection = $this->connection(); |
||
| 270 | //! Don't allow changing of character_set_results, convert encoding of displayed query |
||
| 271 | if ($this->driver->multiQuery($query) && $connection !== null && \preg_match("~^$space*+USE\\b~i", $query)) { |
||
| 272 | $connection->query($query); |
||
| 273 | } |
||
| 274 | |||
| 275 | do { |
||
| 276 | $statement = $this->driver->storedResult(); |
||
| 277 | |||
| 278 | if ($this->driver->hasError()) { |
||
| 279 | $error = $this->driver->error(); |
||
| 280 | if ($this->driver->hasErrno()) { |
||
| 281 | $error = '(' . $this->driver->errno() . "): $error"; |
||
| 282 | } |
||
| 283 | $errors[] = $error; |
||
| 284 | } else { |
||
| 285 | $affected = $this->driver->affectedRows(); // Getting warnigns overwrites this |
||
| 286 | if ($statement !== null) { |
||
| 287 | if (!$onlyErrors) { |
||
| 288 | $select = $this->select($statement, $limit); |
||
| 289 | $messages[] = $select['message']; |
||
| 290 | } |
||
| 291 | } else { |
||
| 292 | if (!$onlyErrors) { |
||
| 293 | // $title = $this->util->html($this->driver->info()); |
||
| 294 | $messages[] = $this->trans->lang('Query executed OK, %d row(s) affected.', $affected); // . "$time"; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | $results[] = \compact('query', 'errors', 'messages', 'select'); |
||
| 300 | |||
| 301 | if ($this->driver->hasError() && $errorStops) { |
||
| 302 | return false; |
||
| 303 | } |
||
| 304 | |||
| 305 | // $start = \microtime(true); |
||
| 306 | } while ($this->driver->nextResult()); |
||
| 307 | |||
| 308 | return true; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Execute a set of queries |
||
| 313 | * |
||
| 314 | * @param string $queries The queries to execute |
||
| 315 | * @param int $limit The max number of rows to return |
||
| 316 | * @param bool $errorStops Stop executing the requests in case of error |
||
| 317 | * @param bool $onlyErrors Return only errors |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function executeCommands(string $queries, int $limit, bool $errorStops, bool $onlyErrors) |
||
| 393 | } |
||
| 394 | } |
||
| 395 |