| Total Complexity | 108 |
| Total Lines | 645 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
Complex classes like PDOStatement 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 PDOStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class PDOStatement extends BasePDOStatement implements IteratorAggregate |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $parameters = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string|null |
||
| 44 | */ |
||
| 45 | private $errorCode; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string|null |
||
| 49 | */ |
||
| 50 | private $errorMessage; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $sql; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $options = [ |
||
| 61 | 'fetchMode' => null, |
||
| 62 | 'fetchColumn' => 0, |
||
| 63 | 'fetchClass' => 'array', |
||
| 64 | 'fetchClassCtorArgs' => null, |
||
| 65 | ]; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Used for the {@see PDO::FETCH_BOUND} |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $columnBinding = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var CollectionInterface|null |
||
| 76 | */ |
||
| 77 | private $collection; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var PDOInterface |
||
| 81 | */ |
||
| 82 | private $pdo; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Closure |
||
| 86 | */ |
||
| 87 | private $request; |
||
| 88 | |||
| 89 | private $namedToPositionalMap = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param PDOInterface $pdo |
||
| 93 | * @param Closure $request |
||
| 94 | * @param string $sql |
||
| 95 | * @param array $options |
||
| 96 | */ |
||
| 97 | public function __construct(PDOInterface $pdo, Closure $request, $sql, array $options) |
||
| 103 | } |
||
| 104 | |||
| 105 | private function replaceNamedParametersWithPositionals($sql) |
||
| 106 | { |
||
| 107 | if (strpos($sql, ':') === false) { |
||
| 108 | return $sql; |
||
| 109 | } |
||
| 110 | $pattern = '/:((?:[\w|\d|_](?=([^\'\\\]*(\\\.|\'([^\'\\\]*\\\.)*[^\'\\\]*\'))*[^\']*$))*)/'; |
||
| 111 | |||
| 112 | $idx = 1; |
||
| 113 | $callback = function ($matches) use (&$idx) { |
||
| 114 | $value = $matches[1]; |
||
| 115 | if (empty($value)) { |
||
| 116 | return $matches[0]; |
||
| 117 | } |
||
| 118 | $this->namedToPositionalMap[$idx] = $value; |
||
| 119 | $idx++; |
||
| 120 | |||
| 121 | return '?'; |
||
| 122 | }; |
||
| 123 | |||
| 124 | return preg_replace_callback($pattern, $callback, $sql); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Determines if the statement has been executed |
||
| 129 | * |
||
| 130 | * @internal |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function hasExecuted() |
||
| 135 | { |
||
| 136 | return ($this->collection !== null || $this->errorCode !== null); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Internal pointer to mark the state of the current query |
||
| 141 | * |
||
| 142 | * @internal |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | private function isSuccessful() |
||
| 147 | { |
||
| 148 | if (!$this->hasExecuted()) { |
||
| 149 | // @codeCoverageIgnoreStart |
||
| 150 | throw new Exception\LogicException('The statement has not been executed yet'); |
||
| 151 | // @codeCoverageIgnoreEnd |
||
| 152 | } |
||
| 153 | |||
| 154 | return $this->collection !== null; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get the fetch style to be used |
||
| 159 | * |
||
| 160 | * @internal |
||
| 161 | * |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | private function getFetchStyle() |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Update all the bound column references |
||
| 171 | * |
||
| 172 | * @internal |
||
| 173 | * |
||
| 174 | * @param array $row |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | private function updateBoundColumns(array $row) |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritDoc} |
||
| 201 | */ |
||
| 202 | public function execute($input_parameters = null) |
||
| 203 | { |
||
| 204 | $input_parameters_array = ArrayUtils::toArray($input_parameters); |
||
| 205 | $zero_based = array_key_exists(0, $input_parameters_array); |
||
| 206 | foreach ($input_parameters_array as $parameter => $value) { |
||
| 207 | if (is_int($parameter) && $zero_based) { |
||
| 208 | $parameter++; |
||
| 209 | } |
||
| 210 | $this->bindValue($parameter, $value); |
||
| 211 | } |
||
| 212 | |||
| 213 | // parameter binding might be unordered, so sort it before execute |
||
| 214 | ksort($this->parameters); |
||
| 215 | |||
| 216 | $result = $this->request->__invoke($this, $this->sql, array_values($this->parameters)); |
||
| 217 | |||
| 218 | if (is_array($result)) { |
||
| 219 | $this->errorCode = $result['code']; |
||
| 220 | $this->errorMessage = $result['message']; |
||
| 221 | |||
| 222 | return false; |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->collection = $result; |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | public function fetch($fetch_style = null, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) |
||
| 234 | { |
||
| 235 | if (!$this->hasExecuted()) { |
||
| 236 | $this->execute(); |
||
| 237 | } |
||
| 238 | |||
| 239 | if (!$this->isSuccessful()) { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!$this->collection->valid()) { |
||
|
|
|||
| 244 | return false; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Get the current row |
||
| 248 | $row = $this->collection->current(); |
||
| 249 | |||
| 250 | // Traverse |
||
| 251 | $this->collection->next(); |
||
| 252 | |||
| 253 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
||
| 254 | |||
| 255 | switch ($fetch_style) { |
||
| 256 | case PDO::FETCH_NAMED: |
||
| 257 | case PDO::FETCH_ASSOC: |
||
| 258 | return array_combine($this->collection->getColumns(false), $row); |
||
| 259 | |||
| 260 | case PDO::FETCH_BOTH: |
||
| 261 | return array_merge($row, array_combine($this->collection->getColumns(false), $row)); |
||
| 262 | |||
| 263 | case PDO::FETCH_BOUND: |
||
| 264 | $this->updateBoundColumns($row); |
||
| 265 | |||
| 266 | return true; |
||
| 267 | |||
| 268 | case PDO::FETCH_NUM: |
||
| 269 | return $row; |
||
| 270 | |||
| 271 | case PDO::FETCH_OBJ: |
||
| 272 | return $this->getObjectResult($this->collection->getColumns(false), $row); |
||
| 273 | |||
| 274 | default: |
||
| 275 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritDoc} |
||
| 281 | */ |
||
| 282 | public function bindParam( |
||
| 283 | $parameter, |
||
| 284 | &$variable, |
||
| 285 | $data_type = PDO::PARAM_STR, |
||
| 286 | $length = null, |
||
| 287 | $driver_options = null |
||
| 288 | ) { |
||
| 289 | if (is_numeric($parameter)) { |
||
| 290 | if ($parameter == 0) { |
||
| 291 | throw new Exception\UnsupportedException("0-based parameter binding not supported, use 1-based"); |
||
| 292 | } |
||
| 293 | $this->parameters[$parameter - 1] = &$variable; |
||
| 294 | } else { |
||
| 295 | $namedParameterKey = substr($parameter, 0, 1) === ':' ? substr($parameter, 1) : $parameter; |
||
| 296 | if (in_array($namedParameterKey, $this->namedToPositionalMap, true)) { |
||
| 297 | foreach ($this->namedToPositionalMap as $key => $value) { |
||
| 298 | if ($value == $namedParameterKey) { |
||
| 299 | $this->parameters[$key] = &$variable; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } else { |
||
| 303 | throw new Exception\OutOfBoundsException( |
||
| 304 | sprintf('The named parameter "%s" does not exist', $parameter) |
||
| 305 | ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritDoc} |
||
| 312 | */ |
||
| 313 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
||
| 314 | { |
||
| 315 | $type = $type ?: PDO::PARAM_STR; |
||
| 316 | |||
| 317 | $this->columnBinding[$column] = [ |
||
| 318 | 'ref' => &$param, |
||
| 319 | 'type' => $type, |
||
| 320 | 'maxlen' => $maxlen, |
||
| 321 | 'driverdata' => $driverdata, |
||
| 322 | ]; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritDoc} |
||
| 327 | */ |
||
| 328 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
||
| 329 | { |
||
| 330 | $value = $this->typedValue($value, $data_type); |
||
| 331 | $this->bindParam($parameter, $value, $data_type); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritDoc} |
||
| 336 | */ |
||
| 337 | public function rowCount() |
||
| 338 | { |
||
| 339 | if (!$this->hasExecuted()) { |
||
| 340 | $this->execute(); |
||
| 341 | } |
||
| 342 | |||
| 343 | if (!$this->isSuccessful()) { |
||
| 344 | return 0; |
||
| 345 | } |
||
| 346 | |||
| 347 | return $this->collection->count(); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritDoc} |
||
| 352 | */ |
||
| 353 | public function fetchColumn($column_number = 0) |
||
| 354 | { |
||
| 355 | if (!is_int($column_number)) { |
||
| 356 | throw new Exception\InvalidArgumentException('column_number must be a valid integer'); |
||
| 357 | } |
||
| 358 | |||
| 359 | if (!$this->hasExecuted()) { |
||
| 360 | $this->execute(); |
||
| 361 | } |
||
| 362 | |||
| 363 | if (!$this->isSuccessful()) { |
||
| 364 | return false; |
||
| 365 | } |
||
| 366 | |||
| 367 | if (!$this->collection->valid()) { |
||
| 368 | return false; |
||
| 369 | } |
||
| 370 | |||
| 371 | $row = $this->collection->current(); |
||
| 372 | $this->collection->next(); |
||
| 373 | |||
| 374 | if ($column_number >= count($row)) { |
||
| 375 | throw new Exception\OutOfBoundsException( |
||
| 376 | sprintf('The column "%d" with the zero-based does not exist', $column_number) |
||
| 377 | ); |
||
| 378 | } |
||
| 379 | |||
| 380 | return $row[$column_number]; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritDoc} |
||
| 385 | */ |
||
| 386 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = null) |
||
| 387 | { |
||
| 388 | if (!$this->hasExecuted()) { |
||
| 389 | $this->execute(); |
||
| 390 | } |
||
| 391 | |||
| 392 | if (!$this->isSuccessful()) { |
||
| 393 | return false; |
||
| 394 | } |
||
| 395 | |||
| 396 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
||
| 397 | |||
| 398 | switch ($fetch_style) { |
||
| 399 | case PDO::FETCH_NUM: |
||
| 400 | return $this->collection->getRows(); |
||
| 401 | |||
| 402 | case PDO::FETCH_NAMED: |
||
| 403 | case PDO::FETCH_ASSOC: |
||
| 404 | $columns = $this->collection->getColumns(false); |
||
| 405 | |||
| 406 | return $this->collection->map(function (array $row) use ($columns) { |
||
| 407 | return array_combine($columns, $row); |
||
| 408 | }); |
||
| 409 | |||
| 410 | case PDO::FETCH_BOTH: |
||
| 411 | $columns = $this->collection->getColumns(false); |
||
| 412 | |||
| 413 | return $this->collection->map(function (array $row) use ($columns) { |
||
| 414 | return array_merge($row, array_combine($columns, $row)); |
||
| 415 | }); |
||
| 416 | |||
| 417 | case PDO::FETCH_FUNC: |
||
| 418 | if (!is_callable($fetch_argument)) { |
||
| 419 | throw new Exception\InvalidArgumentException('Second argument must be callable'); |
||
| 420 | } |
||
| 421 | |||
| 422 | return $this->collection->map(function (array $row) use ($fetch_argument) { |
||
| 423 | return call_user_func_array($fetch_argument, $row); |
||
| 424 | }); |
||
| 425 | |||
| 426 | case PDO::FETCH_COLUMN: |
||
| 427 | $columnIndex = $fetch_argument ?: $this->options['fetchColumn']; |
||
| 428 | |||
| 429 | if (!is_int($columnIndex)) { |
||
| 430 | throw new Exception\InvalidArgumentException('Second argument must be a integer'); |
||
| 431 | } |
||
| 432 | |||
| 433 | $columns = $this->collection->getColumns(false); |
||
| 434 | if (!isset($columns[$columnIndex])) { |
||
| 435 | throw new Exception\OutOfBoundsException( |
||
| 436 | sprintf('Column with the index %d does not exist.', $columnIndex) |
||
| 437 | ); |
||
| 438 | } |
||
| 439 | |||
| 440 | return $this->collection->map(function (array $row) use ($columnIndex) { |
||
| 441 | return $row[$columnIndex]; |
||
| 442 | }); |
||
| 443 | case PDO::FETCH_OBJ: |
||
| 444 | $columns = $this->collection->getColumns(false); |
||
| 445 | |||
| 446 | return $this->collection->map(function (array $row) use ($columns) { |
||
| 447 | return $this->getObjectResult($columns, $row); |
||
| 448 | }); |
||
| 449 | |||
| 450 | default: |
||
| 451 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * {@inheritDoc} |
||
| 457 | */ |
||
| 458 | public function fetchObject($class_name = null, $ctor_args = null) |
||
| 459 | { |
||
| 460 | throw new Exception\UnsupportedException; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritDoc} |
||
| 465 | */ |
||
| 466 | public function errorCode() |
||
| 467 | { |
||
| 468 | return $this->errorCode; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * {@inheritDoc} |
||
| 473 | */ |
||
| 474 | public function errorInfo() |
||
| 475 | { |
||
| 476 | if ($this->errorCode === null) { |
||
| 477 | return null; |
||
| 478 | } |
||
| 479 | |||
| 480 | switch ($this->errorCode) { |
||
| 481 | case CrateConst::ERR_INVALID_SQL: |
||
| 482 | $ansiErrorCode = 42000; |
||
| 483 | break; |
||
| 484 | |||
| 485 | default: |
||
| 486 | $ansiErrorCode = 'Not available'; |
||
| 487 | break; |
||
| 488 | } |
||
| 489 | |||
| 490 | return [ |
||
| 491 | $ansiErrorCode, |
||
| 492 | $this->errorCode, |
||
| 493 | $this->errorMessage, |
||
| 494 | ]; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritDoc} |
||
| 499 | */ |
||
| 500 | public function setAttribute($attribute, $value) |
||
| 501 | { |
||
| 502 | throw new Exception\UnsupportedException('This driver doesn\'t support setting attributes'); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritDoc} |
||
| 507 | */ |
||
| 508 | public function getAttribute($attribute) |
||
| 509 | { |
||
| 510 | throw new Exception\UnsupportedException('This driver doesn\'t support getting attributes'); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * {@inheritDoc} |
||
| 515 | */ |
||
| 516 | public function columnCount() |
||
| 517 | { |
||
| 518 | if (!$this->hasExecuted()) { |
||
| 519 | $this->execute(); |
||
| 520 | } |
||
| 521 | |||
| 522 | return count($this->collection->getColumns(false)); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritDoc} |
||
| 527 | */ |
||
| 528 | public function getColumnMeta($column) |
||
| 529 | { |
||
| 530 | throw new Exception\UnsupportedException; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * {@inheritDoc} |
||
| 535 | */ |
||
| 536 | public function setFetchMode($mode, $params = null) |
||
| 537 | { |
||
| 538 | $args = func_get_args(); |
||
| 539 | $argCount = count($args); |
||
| 540 | |||
| 541 | switch ($mode) { |
||
| 542 | case PDO::FETCH_COLUMN: |
||
| 543 | if ($argCount != 2) { |
||
| 544 | throw new Exception\InvalidArgumentException('fetch mode requires the colno argument'); |
||
| 545 | } |
||
| 546 | |||
| 547 | if (!is_int($params)) { |
||
| 548 | throw new Exception\InvalidArgumentException('colno must be an integer'); |
||
| 549 | } |
||
| 550 | |||
| 551 | $this->options['fetchMode'] = $mode; |
||
| 552 | $this->options['fetchColumn'] = $params; |
||
| 553 | break; |
||
| 554 | |||
| 555 | case PDO::FETCH_ASSOC: |
||
| 556 | case PDO::FETCH_NUM: |
||
| 557 | case PDO::FETCH_BOTH: |
||
| 558 | case PDO::FETCH_BOUND: |
||
| 559 | case PDO::FETCH_NAMED: |
||
| 560 | case PDO::FETCH_OBJ: |
||
| 561 | if ($params !== null) { |
||
| 562 | throw new Exception\InvalidArgumentException('fetch mode doesn\'t allow any extra arguments'); |
||
| 563 | } |
||
| 564 | |||
| 565 | $this->options['fetchMode'] = $mode; |
||
| 566 | break; |
||
| 567 | |||
| 568 | default: |
||
| 569 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
||
| 570 | } |
||
| 571 | |||
| 572 | return true; |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * {@inheritDoc} |
||
| 577 | */ |
||
| 578 | public function nextRowset() |
||
| 579 | { |
||
| 580 | if (!$this->hasExecuted()) { |
||
| 581 | $this->execute(); |
||
| 582 | } |
||
| 583 | |||
| 584 | if (!$this->isSuccessful()) { |
||
| 585 | return false; |
||
| 586 | } |
||
| 587 | |||
| 588 | $this->collection->next(); |
||
| 589 | |||
| 590 | return $this->collection->valid(); |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * {@inheritDoc} |
||
| 595 | */ |
||
| 596 | public function closeCursor() |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * {@inheritDoc} |
||
| 606 | */ |
||
| 607 | public function debugDumpParams() |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * {@Inheritdoc} |
||
| 614 | */ |
||
| 615 | public function getIterator() |
||
| 616 | { |
||
| 617 | $results = $this->fetchAll(); |
||
| 618 | if ($results === false) { |
||
| 619 | throw new Exception\RuntimeException('Failure when fetching data'); |
||
| 620 | } |
||
| 621 | return new ArrayIterator($results); |
||
| 622 | } |
||
| 623 | |||
| 624 | private function typedValue($value, $data_type) |
||
| 663 | } |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Generate object from array |
||
| 668 | * |
||
| 669 | * @param array $columns |
||
| 670 | * @param array $row |
||
| 671 | */ |
||
| 672 | private function getObjectResult(array $columns, array $row) |
||
| 680 | } |
||
| 681 | } |
||
| 682 |
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.