| Total Complexity | 56 |
| Total Lines | 350 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like MySQLRunnableSelect 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 MySQLRunnableSelect, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class MySQLRunnableSelect extends MySQLSelect { |
||
| 20 | /** @var array<string, mixed> */ |
||
| 21 | private array $values = []; |
||
| 22 | private bool $preserveTypes; |
||
| 23 | private string $defaultClassName; |
||
| 24 | private int $foundRows = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param MySQL $db |
||
| 28 | * @param array<string, mixed> $options |
||
| 29 | */ |
||
| 30 | public function __construct($db, array $options = []) { |
||
| 31 | parent::__construct($db); |
||
| 32 | $this->preserveTypes = array_key_exists('preserve-types-default', $options) ? $options['preserve-types-default'] : false; |
||
| 33 | $this->defaultClassName = array_key_exists('fetch-object-class-default', $options) ? $options['fetch-object-class-default'] : stdClass::class; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritDoc |
||
| 38 | */ |
||
| 39 | public function bindValues(array $values) { |
||
| 40 | $this->values = array_merge($this->values, $values); |
||
| 41 | |||
| 42 | return $this; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritDoc |
||
| 47 | */ |
||
| 48 | public function bindValue(string $key, $value) { |
||
| 49 | $this->values[$key] = $value; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritDoc |
||
| 56 | */ |
||
| 57 | public function clearValues(): self { |
||
| 58 | $this->values = []; |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritDoc |
||
| 65 | */ |
||
| 66 | public function setPreserveTypes(bool $preserveTypes = true): self { |
||
| 67 | $this->preserveTypes = $preserveTypes; |
||
| 68 | |||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritDoc |
||
| 74 | */ |
||
| 75 | public function fetchIndexedRows($callback = null): array { |
||
| 76 | return $this->fetchAll($callback, PDO::FETCH_NUM); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritDoc |
||
| 81 | */ |
||
| 82 | public function fetchRows($callback = null): array { |
||
| 83 | return $this->fetchAll($callback, PDO::FETCH_ASSOC); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | public function fetchRowsLazy($callback = null) { |
||
| 90 | $callback ??= static fn($row) => $row; |
||
| 91 | yield from $this->fetchLazy($callback, PDO::FETCH_ASSOC); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | public function fetchRow($callback = null): array { |
||
| 98 | $callback ??= static fn($row) => $row; |
||
| 99 | |||
| 100 | return $this->fetch($callback, PDO::FETCH_ASSOC, null, static fn($row) => [ |
||
|
|
|||
| 101 | 'valid' => is_array($row), |
||
| 102 | 'default' => [], |
||
| 103 | ]); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritDoc |
||
| 108 | */ |
||
| 109 | public function fetchObjects(string $className = 'stdClass', $callback = null): array { |
||
| 110 | return $this->createTempStatement(function(QueryStatement $statement) use ($className, $callback) { |
||
| 111 | $data = $statement->fetchAll(PDO::FETCH_CLASS, $className); |
||
| 112 | if($this->preserveTypes) { |
||
| 113 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
||
| 114 | $data = array_map(static fn($row) => FieldValueConverter::convertValues($row, $columnDefinitions), $data); |
||
| 115 | } |
||
| 116 | if($callback !== null) { |
||
| 117 | return call_user_func(static function($resultData = []) use ($data, $callback) { |
||
| 118 | foreach($data as $row) { |
||
| 119 | $result = $callback($row); |
||
| 120 | if($result !== null && !($result instanceof DBIgnoreRow)) { |
||
| 121 | $resultData[] = $result; |
||
| 122 | } else { |
||
| 123 | $resultData[] = $row; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return $resultData; |
||
| 128 | }); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $data; |
||
| 132 | }); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | public function fetchObjectsLazy($className = null, $callback = null) { |
||
| 139 | $callback ??= static fn($row) => $row; |
||
| 140 | yield from $this->fetchLazy($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @inheritDoc |
||
| 145 | */ |
||
| 146 | public function fetchObject($className = null, $callback = null) { |
||
| 147 | $callback ??= static fn($row) => $row; |
||
| 148 | |||
| 149 | return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static fn($row) => [ |
||
| 150 | 'valid' => is_object($row), |
||
| 151 | 'default' => null, |
||
| 152 | ]); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritDoc |
||
| 157 | */ |
||
| 158 | public function fetchKeyValue($treatValueAsArray = false): array { |
||
| 159 | return $this->createTempStatement(static function(QueryStatement $statement) use ($treatValueAsArray) { |
||
| 160 | if($treatValueAsArray) { |
||
| 161 | $rows = $statement->fetchAll(PDO::FETCH_ASSOC); |
||
| 162 | $result = []; |
||
| 163 | foreach($rows as $row) { |
||
| 164 | [$key] = array_values($row); |
||
| 165 | $result[$key] = $row; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $result; |
||
| 169 | } |
||
| 170 | |||
| 171 | return $statement->fetchAll(PDO::FETCH_KEY_PAIR); |
||
| 172 | }); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | public function fetchGroups(array $fields): array { |
||
| 179 | $rows = $this->fetchRows(); |
||
| 180 | $result = []; |
||
| 181 | foreach($rows as $row) { |
||
| 182 | /** @var array<string, mixed> $tmp */ |
||
| 183 | $tmp = &$result; |
||
| 184 | foreach($fields as $field) { |
||
| 185 | $value = (string) $row[$field]; |
||
| 186 | if(!array_key_exists($value, $tmp)) { |
||
| 187 | $tmp[$value] = []; |
||
| 188 | } |
||
| 189 | $tmp = &$tmp[$value]; |
||
| 190 | } |
||
| 191 | $tmp[] = $row; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $result; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | public function fetchArray(?callable $fn = null): array { |
||
| 201 | return $this->createTempStatement(static function(QueryStatement $stmt) use ($fn) { |
||
| 202 | if($fn !== null) { |
||
| 203 | return $stmt->fetchAll(PDO::FETCH_FUNC, $fn); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $stmt->fetchAll(PDO::FETCH_COLUMN); |
||
| 207 | }); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @inheritDoc |
||
| 212 | */ |
||
| 213 | public function fetchValue($default = null, ?callable $fn = null) { |
||
| 214 | return $this->createTempStatement(static function(QueryStatement $stmt) use ($default, $fn) { |
||
| 215 | $result = $stmt->fetchAll(PDO::FETCH_COLUMN); |
||
| 216 | if($result !== false && array_key_exists(0, $result)) { |
||
| 217 | return $fn !== null ? $fn($result[0]) : $result[0]; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $default; |
||
| 221 | }); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritDoc |
||
| 226 | */ |
||
| 227 | public function getFoundRows(): int { |
||
| 228 | return $this->foundRows; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param callable $fn |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | private function createTempStatement(callable $fn) { |
||
| 236 | $stmt = $this->createStatement(); |
||
| 237 | try { |
||
| 238 | return $fn($stmt); |
||
| 239 | } finally { |
||
| 240 | $stmt->closeCursor(); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return QueryStatement |
||
| 246 | */ |
||
| 247 | private function createStatement(): QueryStatement { |
||
| 248 | $db = $this->db(); |
||
| 249 | $query = $this->__toString(); |
||
| 250 | $statement = $db->prepare($query); |
||
| 251 | $statement->execute($this->values); |
||
| 252 | if($this->getCalcFoundRows()) { |
||
| 253 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $statement; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return Traversable<int, array<string, bool|float|int|string|null>> |
||
| 261 | */ |
||
| 262 | public function getIterator(): Traversable { |
||
| 263 | yield from $this->fetchRowsLazy(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @template TFnReturnType of array<int|string, mixed> |
||
| 268 | * @param null|(callable(array<string, null|scalar>): (TFnReturnType|DBIgnoreRow|null|void)) $callback |
||
| 269 | * @param int $mode |
||
| 270 | * @param mixed $arg0 |
||
| 271 | * @return ($callback is null ? array<int, ($mode is PDO::FETCH_NUM ? array<int, null|scalar> : array<string, null|scalar>)> : array<int, TFnReturnType>) |
||
| 272 | */ |
||
| 273 | private function fetchAll($callback = null, int $mode = 0, $arg0 = null) { |
||
| 274 | return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
||
| 275 | $statement->setFetchMode($mode, $arg0); |
||
| 276 | $data = $statement->fetchAll(); |
||
| 277 | if($this->preserveTypes) { |
||
| 278 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
||
| 279 | $data = array_map(static fn($row) => FieldValueConverter::convertValues($row, $columnDefinitions), $data); |
||
| 280 | } |
||
| 281 | if($callback !== null) { |
||
| 282 | return call_user_func(static function($resultData = []) use ($data, $callback) { |
||
| 283 | foreach($data as $row) { |
||
| 284 | $result = $callback($row); |
||
| 285 | if($result instanceof DBIgnoreRow) { |
||
| 286 | continue; |
||
| 287 | } |
||
| 288 | if($result !== null) { |
||
| 289 | $resultData[] = $result; |
||
| 290 | } else { |
||
| 291 | $resultData[] = $row; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return $resultData; |
||
| 296 | }); |
||
| 297 | } |
||
| 298 | |||
| 299 | return $data; |
||
| 300 | }); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @template T |
||
| 305 | * @template U |
||
| 306 | * @param null|callable(T): U $callback |
||
| 307 | * @param int $mode |
||
| 308 | * @param mixed $arg0 |
||
| 309 | * @return ($callback is null ? Generator<int, T> : Generator<int, U>) |
||
| 310 | */ |
||
| 311 | private function fetchLazy($callback = null, int $mode = PDO::FETCH_ASSOC, $arg0 = null): Generator { |
||
| 312 | $statement = $this->createStatement(); |
||
| 313 | $statement->setFetchMode($mode, $arg0); |
||
| 314 | try { |
||
| 315 | while($row = $statement->fetch()) { |
||
| 316 | /** @var T $row */ |
||
| 317 | // if($this->preserveTypes) { |
||
| 318 | // $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
||
| 319 | // $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
||
| 320 | // } |
||
| 321 | $result = $callback($row); |
||
| 322 | if($result instanceof DBIgnoreRow) { |
||
| 323 | // Skip row in this case |
||
| 324 | continue; |
||
| 325 | } |
||
| 326 | if($result !== null) { |
||
| 327 | yield $result; |
||
| 328 | } else { |
||
| 329 | yield $row; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } finally { |
||
| 333 | try { |
||
| 334 | $statement->closeCursor(); |
||
| 335 | } catch(Throwable) { |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @template T |
||
| 342 | * @template U |
||
| 343 | * @param callable(T): U $callback |
||
| 344 | * @param int $mode |
||
| 345 | * @param mixed $arg0 |
||
| 346 | * @param Closure|null $resultValidator |
||
| 347 | * @return T|U|array<string, mixed> |
||
| 348 | */ |
||
| 349 | private function fetch($callback, int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?Closure $resultValidator = null) { |
||
| 369 | }); |
||
| 370 | } |
||
| 371 | } |
||
| 372 |