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 | 68 | private function replaceNamedParametersWithPositionals($sql) |
|
124 | |||
125 | /** |
||
126 | * Determines if the statement has been executed |
||
127 | * |
||
128 | * @internal |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 30 | 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 | 29 | 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) |
|
177 | { |
||
178 | 1 | foreach ($this->columnBinding as $column => &$metadata) { |
|
|
|||
179 | |||
180 | 1 | $index = $this->collection->getColumnIndex($column); |
|
181 | 1 | if ($index === null) { |
|
182 | // todo: I would like to throw an exception and tell someone they screwed up |
||
183 | // but i think that would violate the PDO api |
||
184 | continue; |
||
185 | } |
||
186 | |||
187 | // Update by reference |
||
188 | 1 | $value = $this->typedValue($row[$index], $metadata['type']); |
|
189 | 1 | $metadata['ref'] = $value; |
|
190 | } |
||
191 | |||
192 | 1 | } |
|
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) |
|
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() |
|
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 | 12 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = []) |
|
378 | { |
||
379 | 12 | if (!$this->hasExecuted()) { |
|
380 | 12 | $this->execute(); |
|
381 | } |
||
382 | |||
383 | 12 | if (!$this->isSuccessful()) { |
|
384 | 1 | return false; |
|
385 | } |
||
386 | |||
387 | 11 | $fetch_style = $fetch_style ?: $this->getFetchStyle(); |
|
388 | |||
389 | switch ($fetch_style) |
||
390 | { |
||
391 | 11 | case PDO::FETCH_NUM: |
|
392 | 1 | return $this->collection->getRows(); |
|
393 | |||
394 | 10 | case PDO::FETCH_NAMED: |
|
395 | 9 | case PDO::FETCH_ASSOC: |
|
396 | 2 | $columns = array_flip($this->collection->getColumns()); |
|
397 | |||
398 | return $this->collection->map(function (array $row) use ($columns) { |
||
399 | 2 | return array_combine($columns, $row); |
|
400 | 2 | }); |
|
401 | |||
402 | 8 | case PDO::FETCH_BOTH: |
|
403 | 2 | $columns = array_flip($this->collection->getColumns()); |
|
404 | |||
405 | return $this->collection->map(function (array $row) use ($columns) { |
||
406 | 2 | return array_merge($row, array_combine($columns, $row)); |
|
407 | 2 | }); |
|
408 | |||
409 | 6 | case PDO::FETCH_FUNC: |
|
410 | 2 | if (!is_callable($fetch_argument)) { |
|
411 | 1 | throw new Exception\InvalidArgumentException('Second argument must be callable'); |
|
412 | } |
||
413 | |||
414 | return $this->collection->map(function (array $row) use ($fetch_argument) { |
||
415 | 1 | return call_user_func_array($fetch_argument, $row); |
|
416 | 1 | }); |
|
417 | |||
418 | 4 | case PDO::FETCH_COLUMN: |
|
419 | 3 | $columnIndex = $fetch_argument ?: $this->options['fetchColumn']; |
|
420 | |||
421 | 3 | if (!is_int($columnIndex)) { |
|
422 | 1 | throw new Exception\InvalidArgumentException('Second argument must be a integer'); |
|
423 | } |
||
424 | |||
425 | 2 | $columns = $this->collection->getColumns(false); |
|
426 | 2 | if (!isset($columns[$columnIndex])) { |
|
427 | 1 | throw new Exception\OutOfBoundsException( |
|
428 | 1 | sprintf('Column with the index %d does not exist.', $columnIndex) |
|
429 | ); |
||
430 | } |
||
431 | |||
432 | 1 | return $this->collection->map(function (array $row) use ($columnIndex) { |
|
433 | 1 | return $row[$columnIndex]; |
|
434 | 1 | }); |
|
435 | |||
436 | default: |
||
437 | 1 | throw new Exception\UnsupportedException('Unsupported fetch style'); |
|
438 | } |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * {@inheritDoc} |
||
443 | */ |
||
444 | 1 | public function fetchObject($class_name = null, $ctor_args = null) |
|
448 | |||
449 | /** |
||
450 | * {@inheritDoc} |
||
451 | */ |
||
452 | 1 | public function errorCode() |
|
456 | |||
457 | /** |
||
458 | * {@inheritDoc} |
||
459 | */ |
||
460 | 2 | public function errorInfo() |
|
461 | { |
||
462 | 2 | if ($this->errorCode === null) { |
|
463 | 2 | return null; |
|
464 | } |
||
465 | |||
466 | 2 | switch ($this->errorCode) |
|
467 | { |
||
468 | 2 | case CrateConst::ERR_INVALID_SQL: |
|
469 | 1 | $ansiErrorCode = 42000; |
|
470 | 1 | break; |
|
471 | |||
472 | default: |
||
473 | 1 | $ansiErrorCode = 'Not available'; |
|
474 | 1 | break; |
|
475 | } |
||
476 | |||
477 | return [ |
||
478 | 2 | $ansiErrorCode, |
|
479 | 2 | $this->errorCode, |
|
480 | 2 | $this->errorMessage |
|
481 | ]; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * {@inheritDoc} |
||
486 | */ |
||
487 | 1 | public function setAttribute($attribute, $value) |
|
491 | |||
492 | /** |
||
493 | * {@inheritDoc} |
||
494 | */ |
||
495 | 1 | public function getAttribute($attribute) |
|
499 | |||
500 | /** |
||
501 | * {@inheritDoc} |
||
502 | */ |
||
503 | 1 | public function columnCount() |
|
504 | { |
||
505 | 1 | if (!$this->hasExecuted()) { |
|
506 | 1 | $this->execute(); |
|
507 | } |
||
508 | |||
509 | 1 | return count($this->collection->getColumns()); |
|
510 | } |
||
511 | |||
512 | /** |
||
513 | * {@inheritDoc} |
||
514 | */ |
||
515 | 1 | public function getColumnMeta($column) |
|
519 | |||
520 | /** |
||
521 | * {@inheritDoc} |
||
522 | */ |
||
523 | 14 | public function setFetchMode($mode, $params = null) |
|
524 | { |
||
525 | 14 | $args = func_get_args(); |
|
526 | 14 | $argCount = count($args); |
|
527 | |||
528 | switch ($mode) |
||
529 | { |
||
530 | 14 | case PDO::FETCH_COLUMN: |
|
531 | 3 | if ($argCount != 2) { |
|
532 | 1 | throw new Exception\InvalidArgumentException('fetch mode requires the colno argument'); |
|
533 | } |
||
534 | |||
535 | 2 | if (!is_int($params)) { |
|
536 | 1 | throw new Exception\InvalidArgumentException('colno must be an integer'); |
|
537 | } |
||
538 | |||
539 | 1 | $this->options['fetchMode'] = $mode; |
|
540 | 1 | $this->options['fetchColumn'] = $params; |
|
541 | 1 | break; |
|
542 | |||
543 | 11 | case PDO::FETCH_ASSOC: |
|
544 | 9 | case PDO::FETCH_NUM: |
|
545 | 7 | case PDO::FETCH_BOTH: |
|
546 | 5 | case PDO::FETCH_BOUND: |
|
547 | 3 | case PDO::FETCH_NAMED: |
|
548 | 10 | if ($params !== null) { |
|
549 | 5 | throw new Exception\InvalidArgumentException('fetch mode doesn\'t allow any extra arguments'); |
|
550 | } |
||
551 | |||
552 | 5 | $this->options['fetchMode'] = $mode; |
|
553 | 5 | break; |
|
554 | |||
555 | default: |
||
556 | 1 | throw new Exception\UnsupportedException('Invalid fetch mode specified'); |
|
557 | } |
||
558 | 6 | } |
|
559 | |||
560 | /** |
||
561 | * {@inheritDoc} |
||
562 | */ |
||
563 | 2 | public function nextRowset() |
|
564 | { |
||
565 | 2 | if (!$this->hasExecuted()) { |
|
566 | 2 | $this->execute(); |
|
567 | } |
||
568 | |||
569 | 2 | if (!$this->isSuccessful()) { |
|
570 | 1 | return false; |
|
571 | } |
||
572 | |||
573 | 1 | $this->collection->next(); |
|
574 | 1 | return $this->collection->valid(); |
|
575 | } |
||
576 | |||
577 | /** |
||
578 | * {@inheritDoc} |
||
579 | */ |
||
580 | 1 | public function closeCursor() |
|
586 | |||
587 | /** |
||
588 | * {@inheritDoc} |
||
589 | */ |
||
590 | 1 | public function debugDumpParams() |
|
594 | |||
595 | /** |
||
596 | * {@Inheritdoc} |
||
597 | */ |
||
598 | 1 | public function getIterator() |
|
602 | |||
603 | 8 | private function typedValue($value, $data_type) |
|
604 | { |
||
605 | switch ($data_type) |
||
606 | { |
||
607 | 8 | case PDO::PARAM_FLOAT: |
|
608 | 8 | case PDO::PARAM_DOUBLE: |
|
609 | 1 | return (float) $value; |
|
610 | |||
611 | 8 | case PDO::PARAM_INT: |
|
612 | 7 | case PDO::PARAM_LONG: |
|
613 | 3 | return (int) $value; |
|
640 | } |
||
641 |