Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OCI8Statement 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 OCI8Statement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class OCI8Statement implements \IteratorAggregate, Statement |
||
33 | { |
||
34 | /** |
||
35 | * @var resource |
||
36 | */ |
||
37 | protected $_dbh; |
||
38 | |||
39 | /** |
||
40 | * @var resource |
||
41 | */ |
||
42 | protected $_sth; |
||
43 | |||
44 | /** |
||
45 | * @var \Doctrine\DBAL\Driver\OCI8\OCI8Connection |
||
46 | */ |
||
47 | protected $_conn; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected static $_PARAM = ':param'; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected static $fetchModeMap = [ |
||
58 | PDO::FETCH_BOTH => OCI_BOTH, |
||
59 | PDO::FETCH_ASSOC => OCI_ASSOC, |
||
60 | PDO::FETCH_NUM => OCI_NUM, |
||
61 | PDO::FETCH_COLUMN => OCI_NUM, |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var integer |
||
66 | */ |
||
67 | protected $_defaultFetchMode = PDO::FETCH_BOTH; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_paramMap = []; |
||
73 | |||
74 | /** |
||
75 | * Holds references to bound parameter values. |
||
76 | * |
||
77 | * This is a new requirement for PHP7's oci8 extension that prevents bound values from being garbage collected. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | private $boundValues = []; |
||
82 | |||
83 | /** |
||
84 | * Indicates whether the statement is in the state when fetching results is possible |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | private $result = false; |
||
89 | |||
90 | /** |
||
91 | * Creates a new OCI8Statement that uses the given connection handle and SQL statement. |
||
92 | * |
||
93 | * @param resource $dbh The connection handle. |
||
94 | * @param string $statement The SQL statement. |
||
95 | * @param \Doctrine\DBAL\Driver\OCI8\OCI8Connection $conn |
||
96 | */ |
||
97 | public function __construct($dbh, $statement, OCI8Connection $conn) |
||
105 | |||
106 | /** |
||
107 | * Converts positional (?) into named placeholders (:param<num>). |
||
108 | * |
||
109 | * Oracle does not support positional parameters, hence this method converts all |
||
110 | * positional parameters into artificially named parameters. Note that this conversion |
||
111 | * is not perfect. All question marks (?) in the original statement are treated as |
||
112 | * placeholders and converted to a named parameter. |
||
113 | * |
||
114 | * The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral. |
||
115 | * Question marks inside literal strings are therefore handled correctly by this method. |
||
116 | * This comes at a cost, the whole sql statement has to be looped over. |
||
117 | * |
||
118 | * @todo extract into utility class in Doctrine\DBAL\Util namespace |
||
119 | * @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements. |
||
120 | * |
||
121 | * @param string $statement The SQL statement to convert. |
||
122 | * |
||
123 | * @return string |
||
124 | * @throws \Doctrine\DBAL\Driver\OCI8\OCI8Exception |
||
125 | */ |
||
126 | 10 | static public function convertPositionalToNamedPlaceholders($statement) |
|
|
|||
127 | { |
||
128 | 10 | $fragmentOffset = $tokenOffset = 0; |
|
129 | 10 | $fragments = $paramMap = []; |
|
130 | 10 | $currentLiteralDelimiter = null; |
|
131 | |||
132 | do { |
||
133 | 10 | if (!$currentLiteralDelimiter) { |
|
134 | 10 | $result = self::findPlaceholderOrOpeningQuote( |
|
135 | 10 | $statement, |
|
136 | 10 | $tokenOffset, |
|
137 | 10 | $fragmentOffset, |
|
138 | 10 | $fragments, |
|
139 | 10 | $currentLiteralDelimiter, |
|
140 | 10 | $paramMap |
|
141 | ); |
||
142 | } else { |
||
143 | 8 | $result = self::findClosingQuote($statement, $tokenOffset, $currentLiteralDelimiter); |
|
144 | } |
||
145 | 10 | } while ($result); |
|
146 | |||
147 | 10 | if ($currentLiteralDelimiter) { |
|
148 | throw new OCI8Exception(sprintf( |
||
149 | 'The statement contains non-terminated string literal starting at offset %d', |
||
150 | $tokenOffset - 1 |
||
151 | )); |
||
152 | } |
||
153 | |||
154 | 10 | $fragments[] = substr($statement, $fragmentOffset); |
|
155 | 10 | $statement = implode('', $fragments); |
|
156 | |||
157 | 10 | return [$statement, $paramMap]; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Finds next placeholder or opening quote. |
||
162 | * |
||
163 | * @param string $statement The SQL statement to parse |
||
164 | * @param string $tokenOffset The offset to start searching from |
||
165 | * @param int $fragmentOffset The offset to build the next fragment from |
||
166 | * @param string[] $fragments Fragments of the original statement not containing placeholders |
||
167 | * @param string|null $currentLiteralDelimiter The delimiter of the current string literal |
||
168 | * or NULL if not currently in a literal |
||
169 | * @param array<int, string> $paramMap Mapping of the original parameter positions to their named replacements |
||
170 | * @return bool Whether the token was found |
||
171 | */ |
||
172 | 10 | private static function findPlaceholderOrOpeningQuote( |
|
173 | $statement, |
||
174 | &$tokenOffset, |
||
175 | &$fragmentOffset, |
||
176 | &$fragments, |
||
177 | &$currentLiteralDelimiter, |
||
178 | &$paramMap |
||
179 | ) { |
||
180 | 10 | $token = self::findToken($statement, $tokenOffset, '/[?\'"]/'); |
|
181 | |||
182 | 10 | if (!$token) { |
|
183 | 10 | return false; |
|
184 | } |
||
185 | |||
186 | 10 | if ($token === '?') { |
|
187 | 10 | $position = count($paramMap) + 1; |
|
188 | 10 | $param = ':param' . $position; |
|
189 | 10 | $fragments[] = substr($statement, $fragmentOffset, $tokenOffset - $fragmentOffset); |
|
190 | 10 | $fragments[] = $param; |
|
191 | 10 | $paramMap[$position] = $param; |
|
192 | 10 | $tokenOffset += 1; |
|
193 | 10 | $fragmentOffset = $tokenOffset; |
|
194 | |||
195 | 10 | return true; |
|
196 | } |
||
197 | |||
198 | 8 | $currentLiteralDelimiter = $token; |
|
199 | 8 | ++$tokenOffset; |
|
200 | |||
201 | 8 | return true; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Finds closing quote |
||
206 | * |
||
207 | * @param string $statement The SQL statement to parse |
||
208 | * @param string $tokenOffset The offset to start searching from |
||
209 | * @param string|null $currentLiteralDelimiter The delimiter of the current string literal |
||
210 | * or NULL if not currently in a literal |
||
211 | * @return bool Whether the token was found |
||
212 | */ |
||
213 | 8 | private static function findClosingQuote( |
|
214 | $statement, |
||
215 | &$tokenOffset, |
||
216 | &$currentLiteralDelimiter |
||
217 | ) { |
||
218 | 8 | $token = self::findToken( |
|
219 | 8 | $statement, |
|
220 | 8 | $tokenOffset, |
|
221 | 8 | '/' . preg_quote($currentLiteralDelimiter, '/') . '/' |
|
222 | ); |
||
223 | |||
224 | 8 | if (!$token) { |
|
225 | return false; |
||
226 | } |
||
227 | |||
228 | 8 | $currentLiteralDelimiter = false; |
|
229 | 8 | ++$tokenOffset; |
|
230 | |||
231 | 8 | return true; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * Finds the token described by regex starting from the given offset. Updates the offset with the position |
||
236 | * where the token was found. |
||
237 | * |
||
238 | * @param string $statement The SQL statement to parse |
||
239 | * @param string $offset The offset to start searching from |
||
240 | * @param string $regex The regex containing token pattern |
||
241 | * @return string|null Token or NULL if not found |
||
242 | */ |
||
243 | 10 | private static function findToken($statement, &$offset, $regex) |
|
244 | { |
||
245 | 10 | if (preg_match($regex, $statement, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
|
246 | 10 | $offset = $matches[0][1]; |
|
247 | 10 | return $matches[0][0]; |
|
248 | } |
||
249 | |||
250 | 10 | return null; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function bindValue($param, $value, $type = null) |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function bindParam($column, &$variable, $type = null, $length = null) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function closeCursor() |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function columnCount() |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | View Code Duplication | public function errorCode() |
|
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function errorInfo() |
||
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | */ |
||
335 | public function execute($params = null) |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
||
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | public function getIterator() |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
||
452 | |||
453 | /** |
||
454 | * {@inheritdoc} |
||
455 | */ |
||
456 | public function fetchColumn($columnIndex = 0) |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | public function rowCount() |
||
480 | } |
||
481 |