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 | 8 | } |
|
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 | 6 | 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 | 1 | return $this->getObjectResult($this->collection->getColumns(false), $row); |
|
267 | 1 | ||
268 | default: |
||
269 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | 2 | /** |
|
274 | * {@inheritDoc} |
||
275 | */ |
||
276 | public function bindParam( |
||
277 | $parameter, |
||
278 | & $variable, |
||
279 | $data_type = PDO::PARAM_STR, |
||
280 | 2 | $length = null, |
|
281 | 2 | $driver_options = null |
|
282 | 1 | ) { |
|
283 | if (is_numeric($parameter)) { |
||
284 | 1 | if ($parameter == 0) { |
|
285 | 1 | throw new Exception\UnsupportedException("0-based parameter binding not supported, use 1-based"); |
|
286 | } |
||
287 | $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 | 1 | ); |
|
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | 1 | /** |
|
305 | * {@inheritDoc} |
||
306 | 1 | */ |
|
307 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
||
308 | 1 | { |
|
309 | 1 | $type = $type ?: PDO::PARAM_STR; |
|
310 | 1 | ||
311 | 1 | $this->columnBinding[$column] = [ |
|
312 | 'ref' => &$param, |
||
313 | 1 | 'type' => $type, |
|
314 | 1 | 'maxlen' => $maxlen, |
|
315 | 'driverdata' => $driverdata |
||
316 | ]; |
||
317 | } |
||
318 | |||
319 | 4 | /** |
|
320 | * {@inheritDoc} |
||
321 | 4 | */ |
|
322 | 4 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
|
323 | 4 | { |
|
324 | $value = $this->typedValue($value, $data_type); |
||
325 | $this->bindParam($parameter, $value, $data_type); |
||
326 | } |
||
327 | |||
328 | 2 | /** |
|
329 | * {@inheritDoc} |
||
330 | 2 | */ |
|
331 | 2 | public function rowCount() |
|
332 | 2 | { |
|
333 | if (!$this->hasExecuted()) { |
||
334 | 2 | $this->execute(); |
|
335 | 1 | } |
|
336 | |||
337 | if (!$this->isSuccessful()) { |
||
338 | 1 | return 0; |
|
339 | } |
||
340 | |||
341 | return $this->collection->count(); |
||
342 | } |
||
343 | |||
344 | 5 | /** |
|
345 | * {@inheritDoc} |
||
346 | 5 | */ |
|
347 | 1 | public function fetchColumn($column_number = 0) |
|
348 | { |
||
349 | if (!is_int($column_number)) { |
||
350 | 4 | throw new Exception\InvalidArgumentException('column_number must be a valid integer'); |
|
351 | 4 | } |
|
352 | 4 | ||
353 | if (!$this->hasExecuted()) { |
||
354 | 4 | $this->execute(); |
|
355 | 1 | } |
|
356 | |||
357 | if (!$this->isSuccessful()) { |
||
358 | 3 | return false; |
|
359 | 2 | } |
|
360 | |||
361 | if (!$this->collection->valid()) { |
||
362 | 2 | return false; |
|
363 | 2 | } |
|
364 | |||
365 | 2 | $row = $this->collection->current(); |
|
366 | 1 | $this->collection->next(); |
|
367 | 1 | ||
368 | 1 | if ($column_number >= count($row)) { |
|
369 | throw new Exception\OutOfBoundsException( |
||
370 | sprintf('The column "%d" with the zero-based does not exist', $column_number) |
||
371 | 1 | ); |
|
372 | } |
||
373 | |||
374 | return $row[$column_number]; |
||
375 | } |
||
376 | |||
377 | 13 | /** |
|
378 | * {@inheritDoc} |
||
379 | 13 | */ |
|
380 | 13 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = []) |
|
381 | 13 | { |
|
382 | if (!$this->hasExecuted()) { |
||
383 | 13 | $this->execute(); |
|
384 | 1 | } |
|
385 | |||
386 | if (!$this->isSuccessful()) { |
||
387 | 12 | return false; |
|
388 | } |
||
389 | |||
390 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
||
391 | 12 | ||
392 | 1 | switch ($fetch_style) |
|
393 | { |
||
394 | 11 | case PDO::FETCH_NUM: |
|
395 | 11 | return $this->collection->getRows(); |
|
396 | 2 | ||
397 | case PDO::FETCH_NAMED: |
||
398 | 2 | case PDO::FETCH_ASSOC: |
|
399 | 2 | $columns = $this->collection->getColumns(false); |
|
400 | return $this->collection->map(function (array $row) use ($columns) { |
||
401 | 9 | return array_combine($columns, $row); |
|
402 | 3 | }); |
|
403 | |||
404 | case PDO::FETCH_BOTH: |
||
405 | 3 | $columns = $this->collection->getColumns(false); |
|
406 | 3 | ||
407 | return $this->collection->map(function (array $row) use ($columns) { |
||
408 | 6 | return array_merge($row, array_combine($columns, $row)); |
|
409 | 2 | }); |
|
410 | 1 | ||
411 | case PDO::FETCH_FUNC: |
||
412 | if (!is_callable($fetch_argument)) { |
||
413 | throw new Exception\InvalidArgumentException('Second argument must be callable'); |
||
414 | 1 | } |
|
415 | 1 | ||
416 | return $this->collection->map(function (array $row) use ($fetch_argument) { |
||
417 | 4 | return call_user_func_array($fetch_argument, $row); |
|
418 | 3 | }); |
|
419 | |||
420 | 3 | case PDO::FETCH_COLUMN: |
|
421 | 1 | $columnIndex = $fetch_argument ?: $this->options['fetchColumn']; |
|
422 | |||
423 | if (!is_int($columnIndex)) { |
||
424 | 2 | throw new Exception\InvalidArgumentException('Second argument must be a integer'); |
|
425 | 2 | } |
|
426 | 1 | ||
427 | 1 | $columns = $this->collection->getColumns(false); |
|
428 | 1 | if (!isset($columns[$columnIndex])) { |
|
429 | throw new Exception\OutOfBoundsException( |
||
430 | sprintf('Column with the index %d does not exist.', $columnIndex) |
||
431 | 1 | ); |
|
432 | 1 | } |
|
433 | 1 | ||
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 | 1 | ||
444 | default: |
||
445 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
446 | } |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * {@inheritDoc} |
||
451 | 1 | */ |
|
452 | public function fetchObject($class_name = null, $ctor_args = null) |
||
453 | 1 | { |
|
454 | throw new Exception\UnsupportedException; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * {@inheritDoc} |
||
459 | 2 | */ |
|
460 | public function errorCode() |
||
464 | |||
465 | 2 | /** |
|
466 | * {@inheritDoc} |
||
467 | 2 | */ |
|
468 | 1 | public function errorInfo() |
|
469 | 1 | { |
|
470 | if ($this->errorCode === null) { |
||
471 | 1 | return null; |
|
472 | 1 | } |
|
473 | 1 | ||
474 | 2 | switch ($this->errorCode) |
|
475 | { |
||
476 | case CrateConst::ERR_INVALID_SQL: |
||
477 | 2 | $ansiErrorCode = 42000; |
|
478 | 2 | break; |
|
479 | 2 | ||
480 | 2 | default: |
|
481 | $ansiErrorCode = 'Not available'; |
||
482 | break; |
||
483 | } |
||
484 | |||
485 | return [ |
||
486 | 1 | $ansiErrorCode, |
|
487 | $this->errorCode, |
||
488 | 1 | $this->errorMessage |
|
489 | ]; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * {@inheritDoc} |
||
494 | 1 | */ |
|
495 | public function setAttribute($attribute, $value) |
||
496 | 1 | { |
|
497 | throw new Exception\UnsupportedException('This driver doesn\'t support setting attributes'); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * {@inheritDoc} |
||
502 | 2 | */ |
|
503 | public function getAttribute($attribute) |
||
504 | 2 | { |
|
505 | 2 | throw new Exception\UnsupportedException('This driver doesn\'t support getting attributes'); |
|
506 | 2 | } |
|
507 | |||
508 | 2 | /** |
|
509 | * {@inheritDoc} |
||
510 | */ |
||
511 | public function columnCount() |
||
512 | { |
||
513 | if (!$this->hasExecuted()) { |
||
514 | 1 | $this->execute(); |
|
515 | } |
||
516 | 1 | ||
517 | return count($this->collection->getColumns(false)); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * {@inheritDoc} |
||
522 | 14 | */ |
|
523 | public function getColumnMeta($column) |
||
527 | |||
528 | /** |
||
529 | 14 | * {@inheritDoc} |
|
530 | 3 | */ |
|
531 | 1 | public function setFetchMode($mode, $params = null) |
|
532 | { |
||
533 | $args = func_get_args(); |
||
534 | 2 | $argCount = count($args); |
|
535 | 1 | ||
536 | switch ($mode) |
||
537 | { |
||
538 | 1 | case PDO::FETCH_COLUMN: |
|
539 | 1 | if ($argCount != 2) { |
|
540 | 1 | throw new Exception\InvalidArgumentException('fetch mode requires the colno argument'); |
|
541 | } |
||
542 | 11 | ||
543 | 11 | if (!is_int($params)) { |
|
544 | 11 | throw new Exception\InvalidArgumentException('colno must be an integer'); |
|
545 | 11 | } |
|
546 | 11 | ||
547 | 10 | $this->options['fetchMode'] = $mode; |
|
548 | 5 | $this->options['fetchColumn'] = $params; |
|
549 | break; |
||
550 | |||
551 | 5 | case PDO::FETCH_ASSOC: |
|
552 | 5 | case PDO::FETCH_NUM: |
|
553 | case PDO::FETCH_BOTH: |
||
554 | 1 | case PDO::FETCH_BOUND: |
|
555 | 1 | case PDO::FETCH_NAMED: |
|
556 | 1 | case PDO::FETCH_OBJ: |
|
557 | 6 | if ($params !== null) { |
|
558 | throw new Exception\InvalidArgumentException('fetch mode doesn\'t allow any extra arguments'); |
||
559 | } |
||
560 | |||
561 | $this->options['fetchMode'] = $mode; |
||
562 | 2 | break; |
|
563 | |||
564 | 2 | default: |
|
565 | 2 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
|
566 | 2 | } |
|
567 | } |
||
568 | 2 | ||
569 | 1 | /** |
|
570 | * {@inheritDoc} |
||
571 | */ |
||
572 | 1 | public function nextRowset() |
|
573 | 1 | { |
|
574 | if (!$this->hasExecuted()) { |
||
575 | $this->execute(); |
||
576 | } |
||
577 | |||
578 | if (!$this->isSuccessful()) { |
||
579 | 1 | return false; |
|
580 | } |
||
581 | 1 | ||
582 | 1 | $this->collection->next(); |
|
583 | 1 | return $this->collection->valid(); |
|
584 | } |
||
585 | |||
586 | /** |
||
587 | * {@inheritDoc} |
||
588 | */ |
||
589 | 1 | public function closeCursor() |
|
590 | { |
||
591 | 1 | $this->errorCode = 0; |
|
592 | $this->collection = null; |
||
593 | return true; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | 1 | * {@inheritDoc} |
|
598 | */ |
||
599 | 1 | public function debugDumpParams() |
|
603 | |||
604 | /** |
||
605 | * {@Inheritdoc} |
||
606 | 8 | */ |
|
607 | 8 | public function getIterator() |
|
608 | 1 | { |
|
609 | return new ArrayIterator($this->fetchAll()); |
||
610 | 8 | } |
|
611 | 8 | ||
612 | 3 | private function typedValue($value, $data_type) |
|
613 | { |
||
614 | 7 | switch ($data_type) |
|
615 | 2 | { |
|
616 | case PDO::PARAM_FLOAT: |
||
617 | 6 | case PDO::PARAM_DOUBLE: |
|
618 | 2 | return (float) $value; |
|
619 | |||
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 |