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) |
|
198 | { |
||
199 | 2 | $input_parameters_array = ArrayUtils::toArray($input_parameters); |
|
200 | 2 | $zero_based = isset($input_parameters_array[0]); |
|
201 | 2 | foreach ($input_parameters_array as $parameter => $value) { |
|
202 | 1 | if (is_int($parameter) && $zero_based) { |
|
203 | 1 | $parameter++; |
|
204 | } |
||
205 | 1 | $this->bindValue($parameter, $value); |
|
206 | } |
||
207 | |||
208 | // parameter binding might be unordered, so sort it before execute |
||
209 | 2 | ksort($this->parameters); |
|
210 | |||
211 | 2 | $result = $this->request->__invoke($this, $this->sql, array_values($this->parameters)); |
|
212 | |||
213 | 2 | if (is_array($result)) { |
|
214 | 1 | $this->errorCode = $result['code']; |
|
215 | 1 | $this->errorMessage = $result['message']; |
|
216 | |||
217 | 1 | return false; |
|
218 | } |
||
219 | |||
220 | 1 | $this->collection = $result; |
|
221 | 1 | return true; |
|
222 | } |
||
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 | default: |
||
266 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * {@inheritDoc} |
||
272 | */ |
||
273 | 2 | public function bindParam( |
|
274 | $parameter, |
||
275 | & $variable, |
||
276 | $data_type = PDO::PARAM_STR, |
||
277 | $length = null, |
||
278 | $driver_options = null |
||
279 | ) { |
||
280 | 2 | if (is_numeric($parameter)) { |
|
281 | 2 | if ($parameter == 0) { |
|
282 | 1 | throw new Exception\UnsupportedException("0-based parameter binding not supported, use 1-based"); |
|
283 | } |
||
284 | 1 | $this->parameters[$parameter-1] = &$variable; |
|
285 | } else { |
||
286 | $namedParameterKey = substr($parameter, 0, 1) === ':' ? substr($parameter, 1) : $parameter; |
||
287 | if (in_array($namedParameterKey, $this->namedToPositionalMap, true)) { |
||
288 | foreach ($this->namedToPositionalMap as $key => $value) { |
||
289 | if ($value == $namedParameterKey) { |
||
290 | $this->parameters[$key] = &$variable; |
||
291 | } |
||
292 | } |
||
293 | } else { |
||
294 | throw new Exception\OutOfBoundsException( |
||
295 | sprintf('The named parameter "%s" does not exist', $parameter) |
||
296 | ); |
||
297 | } |
||
298 | } |
||
299 | 1 | } |
|
300 | |||
301 | /** |
||
302 | * {@inheritDoc} |
||
303 | */ |
||
304 | 1 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
|
305 | { |
||
306 | 1 | $type = $type ?: PDO::PARAM_STR; |
|
307 | |||
308 | 1 | $this->columnBinding[$column] = [ |
|
309 | 1 | 'ref' => &$param, |
|
310 | 1 | 'type' => $type, |
|
311 | 1 | 'maxlen' => $maxlen, |
|
312 | 1 | 'driverdata' => $driverdata |
|
313 | ]; |
||
314 | 1 | } |
|
315 | |||
316 | /** |
||
317 | * {@inheritDoc} |
||
318 | */ |
||
319 | 4 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
|
320 | { |
||
321 | 4 | $value = $this->typedValue($value, $data_type); |
|
322 | 4 | $this->bindParam($parameter, $value, $data_type); |
|
323 | 4 | } |
|
324 | |||
325 | /** |
||
326 | * {@inheritDoc} |
||
327 | */ |
||
328 | 2 | public function rowCount() |
|
329 | { |
||
330 | 2 | if (!$this->hasExecuted()) { |
|
331 | 2 | $this->execute(); |
|
332 | } |
||
333 | |||
334 | 2 | if (!$this->isSuccessful()) { |
|
335 | 1 | return 0; |
|
336 | } |
||
337 | |||
338 | 1 | return $this->collection->count(); |
|
339 | } |
||
340 | |||
341 | /** |
||
342 | * {@inheritDoc} |
||
343 | */ |
||
344 | 5 | public function fetchColumn($column_number = 0) |
|
345 | { |
||
346 | 5 | if (!is_int($column_number)) { |
|
347 | 1 | throw new Exception\InvalidArgumentException('column_number must be a valid integer'); |
|
348 | } |
||
349 | |||
350 | 4 | if (!$this->hasExecuted()) { |
|
351 | 4 | $this->execute(); |
|
352 | } |
||
353 | |||
354 | 4 | if (!$this->isSuccessful()) { |
|
355 | 1 | return false; |
|
356 | } |
||
357 | |||
358 | 3 | if (!$this->collection->valid()) { |
|
359 | 2 | return false; |
|
360 | } |
||
361 | |||
362 | 2 | $row = $this->collection->current(); |
|
363 | 2 | $this->collection->next(); |
|
364 | |||
365 | 2 | if ($column_number >= count($row)) { |
|
366 | 1 | throw new Exception\OutOfBoundsException( |
|
367 | 1 | sprintf('The column "%d" with the zero-based does not exist', $column_number) |
|
368 | ); |
||
369 | } |
||
370 | |||
371 | 1 | return $row[$column_number]; |
|
372 | } |
||
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 | } |
||
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 | 10 | 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 | ); |
||
429 | } |
||
430 | |||
431 | 1 | return $this->collection->map(function (array $row) use ($columnIndex) { |
|
432 | 1 | return $row[$columnIndex]; |
|
433 | 1 | }); |
|
434 | |||
435 | default: |
||
436 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
437 | } |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * {@inheritDoc} |
||
442 | */ |
||
443 | 1 | public function fetchObject($class_name = null, $ctor_args = null) |
|
444 | { |
||
445 | 1 | throw new Exception\UnsupportedException; |
|
446 | } |
||
447 | |||
448 | /** |
||
449 | * {@inheritDoc} |
||
450 | */ |
||
451 | 1 | public function errorCode() |
|
452 | { |
||
453 | 1 | return $this->errorCode; |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * {@inheritDoc} |
||
458 | */ |
||
459 | 2 | public function errorInfo() |
|
460 | { |
||
461 | 2 | if ($this->errorCode === null) { |
|
462 | 2 | return null; |
|
463 | } |
||
464 | |||
465 | 2 | switch ($this->errorCode) |
|
466 | { |
||
467 | 2 | case CrateConst::ERR_INVALID_SQL: |
|
468 | 1 | $ansiErrorCode = 42000; |
|
469 | 1 | break; |
|
470 | |||
471 | default: |
||
472 | 1 | $ansiErrorCode = 'Not available'; |
|
473 | 1 | break; |
|
474 | } |
||
475 | |||
476 | return [ |
||
477 | 2 | $ansiErrorCode, |
|
478 | 2 | $this->errorCode, |
|
479 | 2 | $this->errorMessage |
|
480 | ]; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * {@inheritDoc} |
||
485 | */ |
||
486 | 1 | public function setAttribute($attribute, $value) |
|
487 | { |
||
488 | 1 | throw new Exception\UnsupportedException('This driver doesn\'t support setting attributes'); |
|
489 | } |
||
490 | |||
491 | /** |
||
492 | * {@inheritDoc} |
||
493 | */ |
||
494 | 1 | public function getAttribute($attribute) |
|
495 | { |
||
496 | 1 | throw new Exception\UnsupportedException('This driver doesn\'t support getting attributes'); |
|
497 | } |
||
498 | |||
499 | /** |
||
500 | * {@inheritDoc} |
||
501 | */ |
||
502 | 2 | public function columnCount() |
|
503 | { |
||
504 | 2 | if (!$this->hasExecuted()) { |
|
505 | 2 | $this->execute(); |
|
506 | } |
||
507 | |||
508 | 2 | return count($this->collection->getColumns(false)); |
|
509 | } |
||
510 | |||
511 | /** |
||
512 | * {@inheritDoc} |
||
513 | */ |
||
514 | 1 | public function getColumnMeta($column) |
|
515 | { |
||
516 | 1 | throw new Exception\UnsupportedException; |
|
517 | } |
||
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 | 9 | case PDO::FETCH_NUM: |
|
544 | 7 | case PDO::FETCH_BOTH: |
|
545 | 5 | case PDO::FETCH_BOUND: |
|
546 | 3 | 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 | default: |
||
555 | 1 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
|
556 | } |
||
557 | 6 | } |
|
558 | |||
559 | /** |
||
560 | * {@inheritDoc} |
||
561 | */ |
||
562 | 2 | public function nextRowset() |
|
563 | { |
||
564 | 2 | if (!$this->hasExecuted()) { |
|
565 | 2 | $this->execute(); |
|
566 | } |
||
567 | |||
568 | 2 | if (!$this->isSuccessful()) { |
|
569 | 1 | return false; |
|
570 | } |
||
571 | |||
572 | 1 | $this->collection->next(); |
|
573 | 1 | return $this->collection->valid(); |
|
574 | } |
||
575 | |||
576 | /** |
||
577 | * {@inheritDoc} |
||
578 | */ |
||
579 | 1 | public function closeCursor() |
|
580 | { |
||
581 | 1 | $this->errorCode = 0; |
|
582 | 1 | $this->collection = null; |
|
583 | 1 | return true; |
|
584 | } |
||
585 | |||
586 | /** |
||
587 | * {@inheritDoc} |
||
588 | */ |
||
589 | 1 | public function debugDumpParams() |
|
590 | { |
||
591 | 1 | throw new Exception\UnsupportedException('Not supported, use var_dump($stmt) instead'); |
|
592 | } |
||
593 | |||
594 | /** |
||
595 | * {@Inheritdoc} |
||
596 | */ |
||
597 | 1 | public function getIterator() |
|
601 | |||
602 | 8 | private function typedValue($value, $data_type) |
|
603 | { |
||
604 | switch ($data_type) |
||
605 | { |
||
606 | 8 | case PDO::PARAM_FLOAT: |
|
607 | 8 | case PDO::PARAM_DOUBLE: |
|
608 | 1 | return (float) $value; |
|
609 | |||
610 | 8 | case PDO::PARAM_INT: |
|
611 | 7 | case PDO::PARAM_LONG: |
|
612 | 3 | return (int) $value; |
|
613 | |||
614 | 7 | case PDO::PARAM_NULL: |
|
615 | 2 | return null; |
|
616 | |||
617 | 6 | case PDO::PARAM_BOOL: |
|
618 | 2 | return (bool) $value; |
|
619 | |||
620 | 5 | case PDO::PARAM_STR: |
|
639 | } |
||
640 |