| Total Complexity | 43 |
| Total Lines | 245 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CommandFacade 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 CommandFacade, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class CommandFacade extends AbstractFacade |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) |
||
| 26 | * //! PDO - silent error |
||
| 27 | * |
||
| 28 | * @var ConnectionInterface |
||
| 29 | */ |
||
| 30 | protected $connection = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $results; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var float |
||
| 39 | */ |
||
| 40 | protected $duration; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initialize the facade |
||
| 44 | * |
||
| 45 | * @param AbstractFacade $dbFacade |
||
| 46 | * @param TimerService $timer |
||
| 47 | * @param QueryLogger|null $queryLogger |
||
| 48 | */ |
||
| 49 | public function __construct(AbstractFacade $dbFacade, |
||
| 50 | protected TimerService $timer, protected QueryLogger|null $queryLogger) |
||
| 51 | { |
||
| 52 | parent::__construct($dbFacade); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Open a second connection to the server |
||
| 57 | * |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | private function openSecondConnection() |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param array $row |
||
| 73 | * @param array $blobs |
||
| 74 | * @param array $types |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | protected function values(array $row, array $blobs, array $types): array |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param mixed $statement |
||
| 101 | * @param int $limit |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | private function message($statement, int $limit): string |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Print select result |
||
| 120 | * From editing.inc.php |
||
| 121 | * |
||
| 122 | * @param mixed $statement |
||
| 123 | * @param int $limit |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function select($statement, int $limit = 0): array |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param QueryEntity $queryEntity |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | private function executeCommand(QueryEntity $queryEntity): bool |
||
| 179 | { |
||
| 180 | if ($this->queryLogger !== null) { |
||
| 181 | $this->queryLogger->setCategoryToHistory(); |
||
| 182 | } |
||
| 183 | $this->timer->start(); |
||
| 184 | //! Don't allow changing of character_set_results, convert encoding of displayed query |
||
| 185 | $space = $this->utils->str->spaceRegex(); |
||
| 186 | $succeeded = $this->driver->multiQuery($queryEntity->query); |
||
| 187 | if ($succeeded && $this->connection !== null && |
||
| 188 | preg_match("~^$space*+USE\\b~i", $queryEntity->query)) { |
||
| 189 | $this->connection->query($queryEntity->query); |
||
| 190 | } |
||
| 191 | $this->duration += $this->timer->duration(); |
||
| 192 | |||
| 193 | do { |
||
| 194 | $select = null; |
||
| 195 | $errors = []; |
||
| 196 | $messages = []; |
||
| 197 | $statement = $this->driver->storedResult(); |
||
| 198 | |||
| 199 | if (!$statement || $this->driver->hasError()) { |
||
| 200 | $errors[] = $this->driver->errorMessage(); |
||
| 201 | } elseif (!$queryEntity->onlyErrors) { |
||
| 202 | [$select, $messages] = $this->select($statement, $queryEntity->limit); |
||
| 203 | } |
||
| 204 | |||
| 205 | $result = compact('errors', 'messages', 'select'); |
||
| 206 | $result['query'] = $queryEntity->query; |
||
| 207 | $this->results[] = $result; |
||
| 208 | if ($this->driver->hasError() && $queryEntity->errorStops) { |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | } while ($this->driver->nextResult()); |
||
| 212 | |||
| 213 | return true; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Execute a set of queries |
||
| 218 | * |
||
| 219 | * @param string $queries The queries to execute |
||
| 220 | * @param int $limit The max number of rows to return |
||
| 221 | * @param bool $errorStops Stop executing the requests in case of error |
||
| 222 | * @param bool $onlyErrors Return only errors |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function executeCommands(string $queries, int $limit, bool $errorStops, bool $onlyErrors): array |
||
| 267 | ]; |
||
| 268 | } |
||
| 270 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.