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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
34 | class PDOStatement extends BasePDOStatement implements IteratorAggregate |
||
35 | { |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $parameters = []; |
||
40 | |||
41 | /** |
||
42 | * @var string|null |
||
43 | */ |
||
44 | private $errorCode; |
||
45 | |||
46 | /** |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $errorMessage; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $sql; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $options = [ |
||
60 | 'fetchMode' => null, |
||
61 | 'fetchColumn' => 0, |
||
62 | 'fetchClass' => 'array', |
||
63 | 'fetchClassCtorArgs' => null, |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * Used for the {@see PDO::FETCH_BOUND} |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $columnBinding = []; |
||
72 | |||
73 | /** |
||
74 | * @var CollectionInterface|null |
||
75 | */ |
||
76 | private $collection; |
||
77 | |||
78 | /** |
||
79 | * @var PDOInterface |
||
80 | */ |
||
81 | private $pdo; |
||
82 | |||
83 | /** |
||
84 | * @var Closure |
||
85 | */ |
||
86 | private $request; |
||
87 | |||
88 | private $namedToPositionalMap = array(); |
||
89 | |||
90 | /** |
||
91 | * @param PDOInterface $pdo |
||
92 | * @param Closure $request |
||
93 | * @param string $sql |
||
94 | * @param array $options |
||
95 | */ |
||
96 | 1 | public function __construct(PDOInterface $pdo, Closure $request, $sql, array $options) |
|
103 | |||
104 | 70 | private function replaceNamedParametersWithPositionals($sql) |
|
124 | |||
125 | /** |
||
126 | * Determines if the statement has been executed |
||
127 | * |
||
128 | * @internal |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 32 | private function hasExecuted() |
|
136 | |||
137 | /** |
||
138 | * Internal pointer to mark the state of the current query |
||
139 | * |
||
140 | * @internal |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 30 | private function isSuccessful() |
|
154 | |||
155 | /** |
||
156 | * Get the fetch style to be used |
||
157 | * |
||
158 | * @internal |
||
159 | * |
||
160 | * @return int |
||
161 | */ |
||
162 | 2 | private function getFetchStyle() |
|
166 | |||
167 | /** |
||
168 | * Update all the bound column references |
||
169 | * |
||
170 | * @internal |
||
171 | * |
||
172 | * @param array $row |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | 1 | private function updateBoundColumns(array $row) |
|
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | 2 | public function execute($input_parameters = null) |
|
223 | |||
224 | /** |
||
225 | * {@inheritDoc} |
||
226 | */ |
||
227 | 8 | public function fetch($fetch_style = null, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) |
|
269 | |||
270 | /** |
||
271 | * {@inheritDoc} |
||
272 | */ |
||
273 | 2 | public function bindParam( |
|
300 | |||
301 | /** |
||
302 | * {@inheritDoc} |
||
303 | */ |
||
304 | 1 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
|
315 | |||
316 | /** |
||
317 | * {@inheritDoc} |
||
318 | */ |
||
319 | 4 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
|
324 | |||
325 | /** |
||
326 | * {@inheritDoc} |
||
327 | */ |
||
328 | 2 | public function rowCount() |
|
340 | |||
341 | /** |
||
342 | * {@inheritDoc} |
||
343 | */ |
||
344 | 5 | public function fetchColumn($column_number = 0) |
|
373 | |||
374 | /** |
||
375 | * {@inheritDoc} |
||
376 | */ |
||
377 | 13 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = []) |
|
378 | { |
||
379 | 13 | if (!$this->hasExecuted()) { |
|
380 | 13 | $this->execute(); |
|
381 | 13 | } |
|
382 | |||
383 | 13 | if (!$this->isSuccessful()) { |
|
384 | 1 | return false; |
|
385 | } |
||
386 | |||
387 | 12 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
|
388 | |||
389 | switch ($fetch_style) |
||
390 | { |
||
391 | 12 | case PDO::FETCH_NUM: |
|
392 | 1 | return $this->collection->getRows(); |
|
393 | |||
394 | 11 | case PDO::FETCH_NAMED: |
|
395 | 11 | case PDO::FETCH_ASSOC: |
|
396 | 2 | $columns = $this->collection->getColumns(false); |
|
397 | return $this->collection->map(function (array $row) use ($columns) { |
||
398 | 2 | return array_combine($columns, $row); |
|
399 | 2 | }); |
|
400 | |||
401 | 9 | case PDO::FETCH_BOTH: |
|
402 | 3 | $columns = $this->collection->getColumns(false); |
|
403 | |||
404 | return $this->collection->map(function (array $row) use ($columns) { |
||
405 | 3 | return array_merge($row, array_combine($columns, $row)); |
|
406 | 3 | }); |
|
407 | |||
408 | 6 | case PDO::FETCH_FUNC: |
|
409 | 2 | if (!is_callable($fetch_argument)) { |
|
410 | 1 | throw new Exception\InvalidArgumentException('Second argument must be callable'); |
|
411 | } |
||
412 | |||
413 | return $this->collection->map(function (array $row) use ($fetch_argument) { |
||
414 | 1 | return call_user_func_array($fetch_argument, $row); |
|
415 | 1 | }); |
|
416 | |||
417 | 4 | case PDO::FETCH_COLUMN: |
|
418 | 3 | $columnIndex = $fetch_argument ?: $this->options['fetchColumn']; |
|
419 | |||
420 | 3 | if (!is_int($columnIndex)) { |
|
421 | 1 | throw new Exception\InvalidArgumentException('Second argument must be a integer'); |
|
422 | } |
||
423 | |||
424 | 2 | $columns = $this->collection->getColumns(false); |
|
425 | 2 | if (!isset($columns[$columnIndex])) { |
|
426 | 1 | throw new Exception\OutOfBoundsException( |
|
427 | 1 | sprintf('Column with the index %d does not exist.', $columnIndex) |
|
428 | 1 | ); |
|
429 | } |
||
430 | |||
431 | 1 | return $this->collection->map(function (array $row) use ($columnIndex) { |
|
432 | 1 | return $row[$columnIndex]; |
|
433 | 1 | }); |
|
434 | |||
435 | 1 | default: |
|
436 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
437 | 1 | } |
|
438 | } |
||
439 | |||
440 | /** |
||
441 | * {@inheritDoc} |
||
442 | */ |
||
443 | 1 | public function fetchObject($class_name = null, $ctor_args = null) |
|
447 | |||
448 | /** |
||
449 | * {@inheritDoc} |
||
450 | */ |
||
451 | 1 | public function errorCode() |
|
455 | |||
456 | /** |
||
457 | * {@inheritDoc} |
||
458 | */ |
||
459 | 2 | public function errorInfo() |
|
482 | |||
483 | /** |
||
484 | * {@inheritDoc} |
||
485 | */ |
||
486 | 1 | public function setAttribute($attribute, $value) |
|
490 | |||
491 | /** |
||
492 | * {@inheritDoc} |
||
493 | */ |
||
494 | 1 | public function getAttribute($attribute) |
|
498 | |||
499 | /** |
||
500 | * {@inheritDoc} |
||
501 | */ |
||
502 | 2 | public function columnCount() |
|
510 | |||
511 | /** |
||
512 | * {@inheritDoc} |
||
513 | */ |
||
514 | 1 | public function getColumnMeta($column) |
|
518 | |||
519 | /** |
||
520 | * {@inheritDoc} |
||
521 | */ |
||
522 | 14 | public function setFetchMode($mode, $params = null) |
|
523 | { |
||
524 | 14 | $args = func_get_args(); |
|
525 | 14 | $argCount = count($args); |
|
526 | |||
527 | switch ($mode) |
||
528 | { |
||
529 | 14 | case PDO::FETCH_COLUMN: |
|
530 | 3 | if ($argCount != 2) { |
|
531 | 1 | throw new Exception\InvalidArgumentException('fetch mode requires the colno argument'); |
|
532 | } |
||
533 | |||
534 | 2 | if (!is_int($params)) { |
|
535 | 1 | throw new Exception\InvalidArgumentException('colno must be an integer'); |
|
536 | } |
||
537 | |||
538 | 1 | $this->options['fetchMode'] = $mode; |
|
539 | 1 | $this->options['fetchColumn'] = $params; |
|
540 | 1 | break; |
|
541 | |||
542 | 11 | case PDO::FETCH_ASSOC: |
|
543 | 11 | case PDO::FETCH_NUM: |
|
544 | 11 | case PDO::FETCH_BOTH: |
|
545 | 11 | case PDO::FETCH_BOUND: |
|
546 | 11 | case PDO::FETCH_NAMED: |
|
547 | 10 | if ($params !== null) { |
|
548 | 5 | throw new Exception\InvalidArgumentException('fetch mode doesn\'t allow any extra arguments'); |
|
549 | } |
||
550 | |||
551 | 5 | $this->options['fetchMode'] = $mode; |
|
552 | 5 | break; |
|
553 | |||
554 | 1 | default: |
|
555 | 1 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
|
556 | 1 | } |
|
557 | 6 | } |
|
558 | |||
559 | /** |
||
560 | * {@inheritDoc} |
||
561 | */ |
||
562 | 2 | public function nextRowset() |
|
575 | |||
576 | /** |
||
577 | * {@inheritDoc} |
||
578 | */ |
||
579 | 1 | public function closeCursor() |
|
585 | |||
586 | /** |
||
587 | * {@inheritDoc} |
||
588 | */ |
||
589 | 1 | public function debugDumpParams() |
|
593 | |||
594 | /** |
||
595 | * {@Inheritdoc} |
||
596 | */ |
||
597 | 1 | public function getIterator() |
|
601 | |||
602 | 8 | private function typedValue($value, $data_type) |
|
639 | } |
||
640 |