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) |
|
97 | { |
||
98 | 1 | $this->sql = $this->replaceNamedParametersWithPositionals($sql); |
|
99 | 1 | $this->pdo = $pdo; |
|
100 | 1 | $this->options = array_merge($this->options, $options); |
|
101 | 1 | $this->request = $request; |
|
102 | 1 | } |
|
103 | |||
104 | 67 | private function replaceNamedParametersWithPositionals($sql) |
|
105 | { |
||
106 | 67 | if (strpos($sql, ':') === false) { |
|
107 | 67 | return $sql; |
|
108 | } |
||
109 | 1 | $pattern = '/:((?:[\w|\d|_](?=([^\'\\\]*(\\\.|\'([^\'\\\]*\\\.)*[^\'\\\]*\'))*[^\']*$))*)/'; |
|
110 | |||
111 | 1 | $idx = 0; |
|
112 | $callback = function ($matches) use (&$idx) { |
||
113 | 1 | $value = $matches[1]; |
|
114 | 1 | if (empty($value)) { |
|
115 | 1 | return $matches[0]; |
|
116 | } |
||
117 | 1 | $this->namedToPositionalMap[$value] = $idx; |
|
118 | 1 | $idx++; |
|
119 | 1 | return '?'; |
|
120 | 1 | }; |
|
121 | |||
122 | 1 | return preg_replace_callback($pattern, $callback, $sql); |
|
123 | } |
||
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() |
|
145 | { |
||
146 | 29 | if (!$this->hasExecuted()) { |
|
147 | // @codeCoverageIgnoreStart |
||
148 | throw new Exception\LogicException('The statement has not been executed yet'); |
||
149 | // @codeCoverageIgnoreEnd |
||
150 | } |
||
151 | |||
152 | 29 | return $this->collection !== null; |
|
153 | } |
||
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( |
|
297 | |||
298 | /** |
||
299 | * {@inheritDoc} |
||
300 | */ |
||
301 | 1 | public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) |
|
312 | |||
313 | /** |
||
314 | * {@inheritDoc} |
||
315 | */ |
||
316 | 4 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
|
321 | |||
322 | /** |
||
323 | * {@inheritDoc} |
||
324 | */ |
||
325 | 2 | public function rowCount() |
|
337 | |||
338 | /** |
||
339 | * {@inheritDoc} |
||
340 | */ |
||
341 | 5 | public function fetchColumn($column_number = 0) |
|
370 | |||
371 | /** |
||
372 | * {@inheritDoc} |
||
373 | */ |
||
374 | 12 | public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = []) |
|
437 | |||
438 | /** |
||
439 | * {@inheritDoc} |
||
440 | */ |
||
441 | 1 | public function fetchObject($class_name = null, $ctor_args = null) |
|
445 | |||
446 | /** |
||
447 | * {@inheritDoc} |
||
448 | */ |
||
449 | 1 | public function errorCode() |
|
453 | |||
454 | /** |
||
455 | * {@inheritDoc} |
||
456 | */ |
||
457 | 2 | public function errorInfo() |
|
480 | |||
481 | /** |
||
482 | * {@inheritDoc} |
||
483 | */ |
||
484 | 1 | public function setAttribute($attribute, $value) |
|
488 | |||
489 | /** |
||
490 | * {@inheritDoc} |
||
491 | */ |
||
492 | 1 | public function getAttribute($attribute) |
|
496 | |||
497 | /** |
||
498 | * {@inheritDoc} |
||
499 | */ |
||
500 | 1 | public function columnCount() |
|
508 | |||
509 | /** |
||
510 | * {@inheritDoc} |
||
511 | */ |
||
512 | 1 | public function getColumnMeta($column) |
|
516 | |||
517 | /** |
||
518 | * {@inheritDoc} |
||
519 | */ |
||
520 | 14 | public function setFetchMode($mode, $params = null) |
|
556 | |||
557 | /** |
||
558 | * {@inheritDoc} |
||
559 | */ |
||
560 | 2 | public function nextRowset() |
|
573 | |||
574 | /** |
||
575 | * {@inheritDoc} |
||
576 | */ |
||
577 | 1 | public function closeCursor() |
|
583 | |||
584 | /** |
||
585 | * {@inheritDoc} |
||
586 | */ |
||
587 | 1 | public function debugDumpParams() |
|
591 | |||
592 | /** |
||
593 | * {@Inheritdoc} |
||
594 | */ |
||
595 | 1 | public function getIterator() |
|
599 | |||
600 | 8 | private function typedValue($value, $data_type) |
|
637 | } |
||
638 |