| Total Complexity | 58 |
| Total Lines | 429 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MysqliStatement 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 MysqliStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class MysqliStatement implements IteratorAggregate, Statement |
||
| 27 | { |
||
| 28 | /** @var string[] */ |
||
| 29 | protected static $_paramTypeMap = [ |
||
| 30 | ParameterType::STRING => 's', |
||
| 31 | ParameterType::BINARY => 's', |
||
| 32 | ParameterType::BOOLEAN => 'i', |
||
| 33 | ParameterType::NULL => 's', |
||
| 34 | ParameterType::INTEGER => 'i', |
||
| 35 | ParameterType::LARGE_OBJECT => 'b', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** @var mysqli */ |
||
| 39 | protected $_conn; |
||
| 40 | |||
| 41 | /** @var mysqli_stmt */ |
||
| 42 | protected $_stmt; |
||
| 43 | |||
| 44 | /** @var string[]|false|null */ |
||
| 45 | protected $_columnNames; |
||
| 46 | |||
| 47 | /** @var mixed[] */ |
||
| 48 | protected $_rowBindedValues = []; |
||
| 49 | |||
| 50 | /** @var mixed[] */ |
||
| 51 | protected $_bindedValues; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | protected $types; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Contains ref values for bindValue(). |
||
| 58 | * |
||
| 59 | * @var mixed[] |
||
| 60 | */ |
||
| 61 | protected $_values = []; |
||
| 62 | |||
| 63 | /** @var int */ |
||
| 64 | protected $_defaultFetchMode = FetchMode::MIXED; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Indicates whether the statement is in the state when fetching results is possible |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | private $result = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $prepareString |
||
| 75 | * |
||
| 76 | * @throws MysqliException |
||
| 77 | */ |
||
| 78 | public function __construct(mysqli $conn, $prepareString) |
||
| 79 | { |
||
| 80 | $this->_conn = $conn; |
||
| 81 | |||
| 82 | $stmt = $conn->prepare($prepareString); |
||
| 83 | |||
| 84 | if ($stmt === false) { |
||
| 85 | throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->_stmt = $stmt; |
||
| 89 | |||
| 90 | $paramCount = $this->_stmt->param_count; |
||
| 91 | if (0 >= $paramCount) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->types = str_repeat('s', $paramCount); |
||
| 96 | $this->_bindedValues = array_fill(1, $paramCount, null); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
||
| 103 | { |
||
| 104 | assert(is_int($column)); |
||
| 105 | |||
| 106 | if (! isset(self::$_paramTypeMap[$type])) { |
||
| 107 | throw new MysqliException(sprintf("Unknown type: '%s'", $type)); |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->_bindedValues[$column] =& $variable; |
||
| 111 | $this->types[$column - 1] = self::$_paramTypeMap[$type]; |
||
| 112 | |||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function bindValue($param, $value, $type = ParameterType::STRING) |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function execute($params = null) |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Binds parameters with known types previously bound to the statement |
||
| 208 | */ |
||
| 209 | private function bindTypedParameters() : void |
||
| 210 | { |
||
| 211 | $streams = $values = []; |
||
| 212 | $types = $this->types; |
||
| 213 | |||
| 214 | foreach ($this->_bindedValues as $parameter => $value) { |
||
| 215 | assert(is_int($parameter)); |
||
| 216 | |||
| 217 | if (! isset($types[$parameter - 1])) { |
||
| 218 | $types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($types[$parameter - 1] === static::$_paramTypeMap[ParameterType::LARGE_OBJECT]) { |
||
| 222 | if (is_resource($value)) { |
||
| 223 | if (get_resource_type($value) !== 'stream') { |
||
| 224 | throw new InvalidArgumentException('Resources passed with the LARGE_OBJECT parameter type must be stream resources.'); |
||
| 225 | } |
||
| 226 | $streams[$parameter] = $value; |
||
| 227 | $values[$parameter] = null; |
||
| 228 | continue; |
||
| 229 | } |
||
| 230 | |||
| 231 | $types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; |
||
| 232 | } |
||
| 233 | |||
| 234 | $values[$parameter] = $value; |
||
| 235 | } |
||
| 236 | |||
| 237 | if (! $this->_stmt->bind_param($types, ...$values)) { |
||
| 238 | throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
||
| 239 | } |
||
| 240 | |||
| 241 | $this->sendLongData($streams); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Handle $this->_longData after regular query parameters have been bound |
||
| 246 | * |
||
| 247 | * @param array<int, resource> $streams |
||
| 248 | * |
||
| 249 | * @throws MysqliException |
||
| 250 | */ |
||
| 251 | private function sendLongData(array $streams) : void |
||
| 252 | { |
||
| 253 | foreach ($streams as $paramNr => $stream) { |
||
| 254 | while (! feof($stream)) { |
||
| 255 | $chunk = fread($stream, 8192); |
||
| 256 | |||
| 257 | if ($chunk === false) { |
||
| 258 | throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}."); |
||
| 259 | } |
||
| 260 | |||
| 261 | if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { |
||
| 262 | throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Binds a array of values to bound parameters. |
||
| 270 | * |
||
| 271 | * @param mixed[] $values |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | private function bindUntypedValues(array $values) |
||
| 276 | { |
||
| 277 | $params = []; |
||
| 278 | $types = str_repeat('s', count($values)); |
||
| 279 | |||
| 280 | foreach ($values as &$v) { |
||
| 281 | $params[] =& $v; |
||
| 282 | } |
||
| 283 | |||
| 284 | return $this->_stmt->bind_param($types, ...$params); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return mixed[]|false|null |
||
| 289 | */ |
||
| 290 | private function _fetch() |
||
| 291 | { |
||
| 292 | $ret = $this->_stmt->fetch(); |
||
| 293 | |||
| 294 | if ($ret === true) { |
||
| 295 | $values = []; |
||
| 296 | foreach ($this->_rowBindedValues as $v) { |
||
| 297 | $values[] = $v; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $values; |
||
| 301 | } |
||
| 302 | |||
| 303 | return $ret; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritdoc} |
||
| 308 | */ |
||
| 309 | public function fetch($fetchMode = null, ...$args) |
||
| 310 | { |
||
| 311 | // do not try fetching from the statement if it's not expected to contain result |
||
| 312 | // in order to prevent exceptional situation |
||
| 313 | if (! $this->result) { |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | |||
| 317 | $fetchMode = $fetchMode ?? $this->_defaultFetchMode; |
||
| 318 | |||
| 319 | if ($fetchMode === FetchMode::COLUMN) { |
||
| 320 | return $this->fetchColumn(); |
||
| 321 | } |
||
| 322 | |||
| 323 | $values = $this->_fetch(); |
||
| 324 | |||
| 325 | if ($values === null) { |
||
| 326 | return false; |
||
| 327 | } |
||
| 328 | |||
| 329 | if ($values === false) { |
||
| 330 | throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ($fetchMode === FetchMode::NUMERIC) { |
||
| 334 | return $values; |
||
| 335 | } |
||
| 336 | |||
| 337 | assert(is_array($this->_columnNames)); |
||
| 338 | $assoc = array_combine($this->_columnNames, $values); |
||
| 339 | assert(is_array($assoc)); |
||
| 340 | |||
| 341 | switch ($fetchMode) { |
||
| 342 | case FetchMode::ASSOCIATIVE: |
||
| 343 | return $assoc; |
||
| 344 | |||
| 345 | case FetchMode::MIXED: |
||
| 346 | return $assoc + $values; |
||
| 347 | |||
| 348 | case FetchMode::STANDARD_OBJECT: |
||
| 349 | return (object) $assoc; |
||
| 350 | |||
| 351 | default: |
||
| 352 | throw new MysqliException(sprintf("Unknown fetch type '%s'", $fetchMode)); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | */ |
||
| 359 | public function fetchAll($fetchMode = null, ...$args) |
||
| 360 | { |
||
| 361 | $fetchMode = $fetchMode ?? $this->_defaultFetchMode; |
||
| 362 | |||
| 363 | $rows = []; |
||
| 364 | |||
| 365 | if ($fetchMode === FetchMode::COLUMN) { |
||
| 366 | while (($row = $this->fetchColumn()) !== false) { |
||
| 367 | $rows[] = $row; |
||
| 368 | } |
||
| 369 | } else { |
||
| 370 | while (($row = $this->fetch($fetchMode)) !== false) { |
||
| 371 | $rows[] = $row; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | return $rows; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * {@inheritdoc} |
||
| 380 | */ |
||
| 381 | public function fetchColumn($columnIndex = 0) |
||
| 382 | { |
||
| 383 | $row = $this->fetch(FetchMode::NUMERIC); |
||
| 384 | |||
| 385 | if ($row === false) { |
||
| 386 | return false; |
||
| 387 | } |
||
| 388 | |||
| 389 | return $row[$columnIndex] ?? null; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | */ |
||
| 395 | public function errorCode() |
||
| 396 | { |
||
| 397 | return $this->_stmt->errno; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function errorInfo() |
||
| 404 | { |
||
| 405 | return $this->_stmt->error; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | public function closeCursor() |
||
| 412 | { |
||
| 413 | $this->_stmt->free_result(); |
||
| 414 | $this->result = false; |
||
| 415 | |||
| 416 | return true; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function rowCount() : int |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public function columnCount() |
||
| 435 | { |
||
| 436 | return $this->_stmt->field_count; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * {@inheritdoc} |
||
| 441 | */ |
||
| 442 | public function setFetchMode($fetchMode, ...$args) |
||
| 443 | { |
||
| 444 | $this->_defaultFetchMode = $fetchMode; |
||
| 445 | |||
| 446 | return true; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | public function getIterator() |
||
| 455 | } |
||
| 456 | } |
||
| 457 |