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) |
|
228 | { |
||
229 | 8 | if (!$this->hasExecuted()) { |
|
230 | 8 | $this->execute(); |
|
231 | } |
||
232 | |||
233 | 8 | if (!$this->isSuccessful()) { |
|
234 | 1 | return false; |
|
235 | } |
||
236 | |||
237 | 7 | if (!$this->collection->valid()) { |
|
238 | 1 | return false; |
|
239 | } |
||
240 | |||
241 | // Get the current row |
||
242 | 6 | $row = $this->collection->current(); |
|
243 | |||
244 | // Traverse |
||
245 | 6 | $this->collection->next(); |
|
246 | |||
247 | 6 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
|
248 | |||
249 | switch ($fetch_style) |
||
250 | { |
||
251 | 6 | case PDO::FETCH_NAMED: |
|
252 | 5 | case PDO::FETCH_ASSOC: |
|
253 | 2 | return array_combine($this->collection->getColumns(false), $row); |
|
254 | |||
255 | 4 | case PDO::FETCH_BOTH: |
|
256 | 1 | return array_merge($row, array_combine($this->collection->getColumns(false), $row)); |
|
257 | |||
258 | 3 | case PDO::FETCH_BOUND: |
|
259 | 1 | $this->updateBoundColumns($row); |
|
260 | 1 | return true; |
|
261 | |||
262 | 2 | case PDO::FETCH_NUM: |
|
263 | 1 | return $row; |
|
264 | |||
265 | 1 | case PDO::FETCH_OBJ: |
|
266 | return $this->getObjectResult($this->collection->getColumns(false), $row); |
||
267 | |||
268 | default: |
||
269 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * {@inheritDoc} |
||
275 | */ |
||
276 | 2 | public function bindParam( |
|
277 | $parameter, |
||
278 | & $variable, |
||
279 | $data_type = PDO::PARAM_STR, |
||
280 | $length = null, |
||
281 | $driver_options = null |
||
282 | ) { |
||
283 | 2 | if (is_numeric($parameter)) { |
|
284 | 2 | if ($parameter == 0) { |
|
285 | 1 | throw new Exception\UnsupportedException("0-based parameter binding not supported, use 1-based"); |
|
286 | } |
||
287 | 1 | $this->parameters[$parameter-1] = &$variable; |
|
288 | } else { |
||
289 | $namedParameterKey = substr($parameter, 0, 1) === ':' ? substr($parameter, 1) : $parameter; |
||
290 | if (in_array($namedParameterKey, $this->namedToPositionalMap, true)) { |
||
291 | foreach ($this->namedToPositionalMap as $key => $value) { |
||
292 | if ($value == $namedParameterKey) { |
||
293 | $this->parameters[$key] = &$variable; |
||
294 | } |
||
295 | } |
||
296 | } else { |
||
297 | throw new Exception\OutOfBoundsException( |
||
298 | sprintf('The named parameter "%s" does not exist', $parameter) |
||
299 | ); |
||
300 | } |
||
301 | } |
||
302 | 1 | } |
|
303 | |||
304 | /** |
||
305 | * {@inheritDoc} |
||
306 | */ |
||
307 | 1 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
|
318 | |||
319 | /** |
||
320 | * {@inheritDoc} |
||
321 | */ |
||
322 | 4 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
|
327 | |||
328 | /** |
||
329 | * {@inheritDoc} |
||
330 | */ |
||
331 | 2 | public function rowCount() |
|
332 | { |
||
333 | 2 | if (!$this->hasExecuted()) { |
|
334 | 2 | $this->execute(); |
|
335 | } |
||
336 | |||
337 | 2 | if (!$this->isSuccessful()) { |
|
338 | 1 | return 0; |
|
339 | } |
||
340 | |||
341 | 1 | return $this->collection->count(); |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * {@inheritDoc} |
||
346 | */ |
||
347 | 5 | public function fetchColumn($column_number = 0) |
|
348 | { |
||
349 | 5 | if (!is_int($column_number)) { |
|
350 | 1 | throw new Exception\InvalidArgumentException('column_number must be a valid integer'); |
|
351 | } |
||
352 | |||
353 | 4 | if (!$this->hasExecuted()) { |
|
354 | 4 | $this->execute(); |
|
355 | } |
||
356 | |||
357 | 4 | if (!$this->isSuccessful()) { |
|
358 | 1 | return false; |
|
359 | } |
||
360 | |||
361 | 3 | if (!$this->collection->valid()) { |
|
362 | 2 | return false; |
|
363 | } |
||
364 | |||
365 | 2 | $row = $this->collection->current(); |
|
366 | 2 | $this->collection->next(); |
|
367 | |||
368 | 2 | if ($column_number >= count($row)) { |
|
369 | 1 | throw new Exception\OutOfBoundsException( |
|
370 | 1 | sprintf('The column "%d" with the zero-based does not exist', $column_number) |
|
371 | ); |
||
372 | } |
||
373 | |||
374 | 1 | return $row[$column_number]; |
|
375 | } |
||
376 | |||
377 | /** |
||
378 | * {@inheritDoc} |
||
379 | */ |
||
380 | 13 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = []) |
|
381 | { |
||
382 | 13 | if (!$this->hasExecuted()) { |
|
383 | 13 | $this->execute(); |
|
384 | } |
||
385 | |||
386 | 13 | if (!$this->isSuccessful()) { |
|
387 | 1 | return false; |
|
388 | } |
||
389 | |||
390 | 12 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
|
391 | |||
392 | switch ($fetch_style) |
||
393 | { |
||
394 | 12 | case PDO::FETCH_NUM: |
|
395 | 1 | return $this->collection->getRows(); |
|
396 | |||
397 | 11 | case PDO::FETCH_NAMED: |
|
398 | 10 | case PDO::FETCH_ASSOC: |
|
399 | 2 | $columns = $this->collection->getColumns(false); |
|
400 | return $this->collection->map(function (array $row) use ($columns) { |
||
401 | 2 | return array_combine($columns, $row); |
|
402 | 2 | }); |
|
403 | |||
404 | 9 | case PDO::FETCH_BOTH: |
|
405 | 3 | $columns = $this->collection->getColumns(false); |
|
406 | |||
407 | return $this->collection->map(function (array $row) use ($columns) { |
||
408 | 3 | return array_merge($row, array_combine($columns, $row)); |
|
409 | 3 | }); |
|
410 | |||
411 | 6 | case PDO::FETCH_FUNC: |
|
412 | 2 | if (!is_callable($fetch_argument)) { |
|
413 | 1 | throw new Exception\InvalidArgumentException('Second argument must be callable'); |
|
414 | } |
||
415 | |||
416 | return $this->collection->map(function (array $row) use ($fetch_argument) { |
||
417 | 1 | return call_user_func_array($fetch_argument, $row); |
|
418 | 1 | }); |
|
419 | |||
420 | 4 | case PDO::FETCH_COLUMN: |
|
421 | 3 | $columnIndex = $fetch_argument ?: $this->options['fetchColumn']; |
|
422 | |||
423 | 3 | if (!is_int($columnIndex)) { |
|
424 | 1 | throw new Exception\InvalidArgumentException('Second argument must be a integer'); |
|
425 | } |
||
426 | |||
427 | 2 | $columns = $this->collection->getColumns(false); |
|
428 | 2 | if (!isset($columns[$columnIndex])) { |
|
429 | 1 | throw new Exception\OutOfBoundsException( |
|
430 | 1 | sprintf('Column with the index %d does not exist.', $columnIndex) |
|
431 | ); |
||
432 | } |
||
433 | |||
434 | return $this->collection->map(function (array $row) use ($columnIndex) { |
||
435 | 1 | return $row[$columnIndex]; |
|
436 | 1 | }); |
|
437 | 1 | case PDO::FETCH_OBJ: |
|
438 | $columns = $this->collection->getColumns(false); |
||
439 | |||
440 | return $this->collection->map(function (array $row) use ($columns) { |
||
441 | return $this->getObjectResult($columns, $row); |
||
442 | }); |
||
443 | |||
444 | default: |
||
445 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
446 | } |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * {@inheritDoc} |
||
451 | */ |
||
452 | 1 | public function fetchObject($class_name = null, $ctor_args = null) |
|
456 | |||
457 | /** |
||
458 | * {@inheritDoc} |
||
459 | */ |
||
460 | 1 | public function errorCode() |
|
464 | |||
465 | /** |
||
466 | * {@inheritDoc} |
||
467 | */ |
||
468 | 2 | public function errorInfo() |
|
469 | { |
||
470 | 2 | if ($this->errorCode === null) { |
|
471 | 2 | return null; |
|
472 | } |
||
473 | |||
474 | 2 | switch ($this->errorCode) |
|
475 | { |
||
476 | 2 | case CrateConst::ERR_INVALID_SQL: |
|
477 | 1 | $ansiErrorCode = 42000; |
|
478 | 1 | break; |
|
479 | |||
480 | default: |
||
481 | 1 | $ansiErrorCode = 'Not available'; |
|
482 | 1 | break; |
|
483 | } |
||
484 | |||
485 | return [ |
||
486 | 2 | $ansiErrorCode, |
|
487 | 2 | $this->errorCode, |
|
488 | 2 | $this->errorMessage |
|
489 | ]; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * {@inheritDoc} |
||
494 | */ |
||
495 | 1 | public function setAttribute($attribute, $value) |
|
499 | |||
500 | /** |
||
501 | * {@inheritDoc} |
||
502 | */ |
||
503 | 1 | public function getAttribute($attribute) |
|
507 | |||
508 | /** |
||
509 | * {@inheritDoc} |
||
510 | */ |
||
511 | 2 | public function columnCount() |
|
512 | { |
||
513 | 2 | if (!$this->hasExecuted()) { |
|
514 | 2 | $this->execute(); |
|
515 | } |
||
516 | |||
517 | 2 | return count($this->collection->getColumns(false)); |
|
518 | } |
||
519 | |||
520 | /** |
||
521 | * {@inheritDoc} |
||
522 | */ |
||
523 | 1 | public function getColumnMeta($column) |
|
527 | |||
528 | /** |
||
529 | * {@inheritDoc} |
||
530 | */ |
||
531 | 14 | public function setFetchMode($mode, $params = null) |
|
532 | { |
||
533 | 14 | $args = func_get_args(); |
|
534 | 14 | $argCount = count($args); |
|
535 | |||
536 | switch ($mode) |
||
537 | { |
||
538 | 14 | case PDO::FETCH_COLUMN: |
|
539 | 3 | if ($argCount != 2) { |
|
540 | 1 | throw new Exception\InvalidArgumentException('fetch mode requires the colno argument'); |
|
541 | } |
||
542 | |||
543 | 2 | if (!is_int($params)) { |
|
544 | 1 | throw new Exception\InvalidArgumentException('colno must be an integer'); |
|
545 | } |
||
546 | |||
547 | 1 | $this->options['fetchMode'] = $mode; |
|
548 | 1 | $this->options['fetchColumn'] = $params; |
|
549 | 1 | break; |
|
550 | |||
551 | 11 | case PDO::FETCH_ASSOC: |
|
552 | 9 | case PDO::FETCH_NUM: |
|
553 | 7 | case PDO::FETCH_BOTH: |
|
554 | 5 | case PDO::FETCH_BOUND: |
|
555 | 3 | case PDO::FETCH_NAMED: |
|
556 | 1 | case PDO::FETCH_OBJ: |
|
557 | 10 | if ($params !== null) { |
|
558 | 5 | throw new Exception\InvalidArgumentException('fetch mode doesn\'t allow any extra arguments'); |
|
559 | } |
||
560 | |||
561 | 5 | $this->options['fetchMode'] = $mode; |
|
562 | 5 | break; |
|
563 | |||
564 | default: |
||
565 | 1 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
|
566 | } |
||
567 | 6 | } |
|
568 | |||
569 | /** |
||
570 | * {@inheritDoc} |
||
571 | */ |
||
572 | 2 | public function nextRowset() |
|
573 | { |
||
574 | 2 | if (!$this->hasExecuted()) { |
|
575 | 2 | $this->execute(); |
|
576 | } |
||
577 | |||
578 | 2 | if (!$this->isSuccessful()) { |
|
579 | 1 | return false; |
|
580 | } |
||
581 | |||
582 | 1 | $this->collection->next(); |
|
583 | 1 | return $this->collection->valid(); |
|
584 | } |
||
585 | |||
586 | /** |
||
587 | * {@inheritDoc} |
||
588 | */ |
||
589 | 1 | public function closeCursor() |
|
595 | |||
596 | /** |
||
597 | * {@inheritDoc} |
||
598 | */ |
||
599 | 1 | public function debugDumpParams() |
|
603 | |||
604 | /** |
||
605 | * {@Inheritdoc} |
||
606 | */ |
||
607 | 1 | public function getIterator() |
|
611 | |||
612 | 8 | private function typedValue($value, $data_type) |
|
613 | { |
||
614 | switch ($data_type) |
||
615 | { |
||
616 | 8 | case PDO::PARAM_FLOAT: |
|
617 | 8 | case PDO::PARAM_DOUBLE: |
|
618 | 1 | return (float) $value; |
|
619 | |||
620 | 8 | case PDO::PARAM_INT: |
|
648 | |||
649 | /** |
||
650 | * Generate object from array |
||
651 | * @param array $columns |
||
652 | * @param array $row |
||
653 | */ |
||
654 | private function getObjectResult(array $columns, array $row) |
||
663 | } |
||
664 |