Total Complexity | 52 |
Total Lines | 313 |
Duplicated Lines | 16.61 % |
Coverage | 0% |
Changes | 0 |
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 SQLAnywhereStatement 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.
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 SQLAnywhereStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class SQLAnywhereStatement implements IteratorAggregate, Statement |
||
35 | { |
||
36 | /** |
||
37 | * @var resource The connection resource. |
||
38 | */ |
||
39 | private $conn; |
||
40 | |||
41 | /** |
||
42 | * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
43 | */ |
||
44 | private $defaultFetchClass = '\stdClass'; |
||
45 | |||
46 | /** |
||
47 | * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
48 | */ |
||
49 | private $defaultFetchClassCtorArgs = []; |
||
50 | |||
51 | /** |
||
52 | * @var int Default fetch mode to use. |
||
53 | */ |
||
54 | private $defaultFetchMode = PDO::FETCH_BOTH; |
||
55 | |||
56 | /** |
||
57 | * @var resource The result set resource to fetch. |
||
58 | */ |
||
59 | private $result; |
||
60 | |||
61 | /** |
||
62 | * @var resource The prepared SQL statement to execute. |
||
63 | */ |
||
64 | private $stmt; |
||
65 | |||
66 | /** |
||
67 | * Constructor. |
||
68 | * |
||
69 | * Prepares given statement for given connection. |
||
70 | * |
||
71 | * @param resource $conn The connection resource to use. |
||
72 | * @param string $sql The SQL statement to prepare. |
||
73 | * |
||
74 | * @throws SQLAnywhereException |
||
75 | */ |
||
76 | public function __construct($conn, $sql) |
||
77 | { |
||
78 | if ( ! is_resource($conn)) { |
||
79 | throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn); |
||
80 | } |
||
81 | |||
82 | $this->conn = $conn; |
||
83 | $this->stmt = sasql_prepare($conn, $sql); |
||
|
|||
84 | |||
85 | if ( ! is_resource($this->stmt)) { |
||
86 | throw SQLAnywhereException::fromSQLAnywhereError($conn); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | * |
||
93 | * @throws SQLAnywhereException |
||
94 | */ |
||
95 | public function bindParam($column, &$variable, $type = null, $length = null) |
||
96 | { |
||
97 | switch ($type) { |
||
98 | case PDO::PARAM_INT: |
||
99 | case PDO::PARAM_BOOL: |
||
100 | $type = 'i'; |
||
101 | break; |
||
102 | case PDO::PARAM_LOB: |
||
103 | $type = 'b'; |
||
104 | break; |
||
105 | case PDO::PARAM_NULL: |
||
106 | case PDO::PARAM_STR: |
||
107 | $type = 's'; |
||
108 | break; |
||
109 | default: |
||
110 | throw new SQLAnywhereException('Unknown type: ' . $type); |
||
111 | } |
||
112 | |||
113 | if ( ! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) { |
||
114 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
115 | } |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function bindValue($param, $value, $type = null) |
||
124 | { |
||
125 | return $this->bindParam($param, $value, $type); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | * |
||
131 | * @throws SQLAnywhereException |
||
132 | */ |
||
133 | public function closeCursor() |
||
134 | { |
||
135 | if (!sasql_stmt_reset($this->stmt)) { |
||
136 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
137 | } |
||
138 | |||
139 | return true; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function columnCount() |
||
146 | { |
||
147 | return sasql_stmt_field_count($this->stmt); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function errorCode() |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function errorInfo() |
||
162 | { |
||
163 | return sasql_stmt_error($this->stmt); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | * |
||
169 | * @throws SQLAnywhereException |
||
170 | */ |
||
171 | public function execute($params = null) |
||
172 | { |
||
173 | View Code Duplication | if (is_array($params)) { |
|
174 | $hasZeroIndex = array_key_exists(0, $params); |
||
175 | |||
176 | foreach ($params as $key => $val) { |
||
177 | $key = ($hasZeroIndex && is_numeric($key)) ? $key + 1 : $key; |
||
178 | |||
179 | $this->bindValue($key, $val); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if ( ! sasql_stmt_execute($this->stmt)) { |
||
184 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
185 | } |
||
186 | |||
187 | $this->result = sasql_stmt_result_metadata($this->stmt); |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | * |
||
195 | * @throws SQLAnywhereException |
||
196 | */ |
||
197 | public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
||
198 | { |
||
199 | if ( ! is_resource($this->result)) { |
||
200 | return false; |
||
201 | } |
||
202 | |||
203 | $fetchMode = $fetchMode ?: $this->defaultFetchMode; |
||
204 | |||
205 | switch ($fetchMode) { |
||
206 | case PDO::FETCH_ASSOC: |
||
207 | return sasql_fetch_assoc($this->result); |
||
208 | case PDO::FETCH_BOTH: |
||
209 | return sasql_fetch_array($this->result, SASQL_BOTH); |
||
210 | case PDO::FETCH_CLASS: |
||
211 | $className = $this->defaultFetchClass; |
||
212 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
213 | |||
214 | if (func_num_args() >= 2) { |
||
215 | $args = func_get_args(); |
||
216 | $className = $args[1]; |
||
217 | $ctorArgs = isset($args[2]) ? $args[2] : []; |
||
218 | } |
||
219 | |||
220 | $result = sasql_fetch_object($this->result); |
||
221 | |||
222 | if ($result instanceof \stdClass) { |
||
223 | $result = $this->castObject($result, $className, $ctorArgs); |
||
224 | } |
||
225 | |||
226 | return $result; |
||
227 | case PDO::FETCH_NUM: |
||
228 | return sasql_fetch_row($this->result); |
||
229 | case PDO::FETCH_OBJ: |
||
230 | return sasql_fetch_object($this->result); |
||
231 | default: |
||
232 | throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | View Code Duplication | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
240 | { |
||
241 | $rows = []; |
||
242 | |||
243 | switch ($fetchMode) { |
||
244 | case PDO::FETCH_CLASS: |
||
245 | while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { |
||
246 | $rows[] = $row; |
||
247 | } |
||
248 | break; |
||
249 | case PDO::FETCH_COLUMN: |
||
250 | while ($row = $this->fetchColumn()) { |
||
251 | $rows[] = $row; |
||
252 | } |
||
253 | break; |
||
254 | default: |
||
255 | while ($row = $this->fetch($fetchMode)) { |
||
256 | $rows[] = $row; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return $rows; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | View Code Duplication | public function fetchColumn($columnIndex = 0) |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function getIterator() |
||
281 | { |
||
282 | return new StatementIterator($this); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function rowCount() |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Casts a stdClass object to the given class name mapping its' properties. |
||
305 | * |
||
306 | * @param \stdClass $sourceObject Object to cast from. |
||
307 | * @param string|object $destinationClass Name of the class or class instance to cast to. |
||
308 | * @param array $ctorArgs Arguments to use for constructing the destination class instance. |
||
309 | * |
||
310 | * @return object |
||
311 | * |
||
312 | * @throws SQLAnywhereException |
||
313 | */ |
||
314 | private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = []) |
||
347 | } |
||
348 | } |
||
349 |