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. 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 SQLAnywhereStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class SQLAnywhereStatement implements IteratorAggregate, Statement |
||
44 | { |
||
45 | /** @var resource The connection resource. */ |
||
46 | private $conn; |
||
47 | |||
48 | /** @var string Name of the default class to instantiate when fetching class instances. */ |
||
49 | private $defaultFetchClass = '\stdClass'; |
||
50 | |||
51 | /** @var mixed[] Constructor arguments for the default class to instantiate when fetching class instances. */ |
||
52 | private $defaultFetchClassCtorArgs = []; |
||
53 | |||
54 | /** @var int Default fetch mode to use. */ |
||
55 | private $defaultFetchMode = FetchMode::MIXED; |
||
56 | |||
57 | /** @var resource The result set resource to fetch. */ |
||
58 | private $result; |
||
59 | |||
60 | /** @var resource The prepared SQL statement to execute. */ |
||
61 | private $stmt; |
||
62 | |||
63 | /** @var mixed[] The references to bound parameter values. */ |
||
64 | private $boundValues = []; |
||
65 | |||
66 | /** |
||
67 | * Prepares given statement for given connection. |
||
68 | * |
||
69 | * @param resource $conn The connection resource to use. |
||
70 | * @param string $sql The SQL statement to prepare. |
||
71 | * |
||
72 | * @throws SQLAnywhereException |
||
73 | */ |
||
74 | public function __construct($conn, string $sql) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * |
||
94 | * @throws SQLAnywhereException |
||
95 | */ |
||
96 | public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void |
||
97 | { |
||
98 | assert(is_int($param)); |
||
99 | |||
100 | switch ($type) { |
||
101 | case ParameterType::INTEGER: |
||
102 | case ParameterType::BOOLEAN: |
||
103 | $type = 'i'; |
||
104 | break; |
||
105 | |||
106 | case ParameterType::LARGE_OBJECT: |
||
107 | $type = 'b'; |
||
108 | break; |
||
109 | |||
110 | case ParameterType::NULL: |
||
111 | case ParameterType::STRING: |
||
112 | case ParameterType::BINARY: |
||
113 | $type = 's'; |
||
114 | break; |
||
115 | |||
116 | default: |
||
117 | throw new SQLAnywhereException(sprintf('Unknown type %d.', $type)); |
||
118 | } |
||
119 | |||
120 | $this->boundValues[$param] =& $variable; |
||
121 | |||
122 | if (! sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) { |
||
123 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function bindValue($param, $value, int $type = ParameterType::STRING) : void |
||
131 | { |
||
132 | $this->bindParam($param, $value, $type); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function closeCursor() : void |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function columnCount() : int |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | * |
||
154 | * @throws SQLAnywhereException |
||
155 | */ |
||
156 | public function execute(?array $params = null) : void |
||
157 | { |
||
158 | if (is_array($params)) { |
||
159 | $hasZeroIndex = array_key_exists(0, $params); |
||
160 | |||
161 | foreach ($params as $key => $val) { |
||
162 | if ($hasZeroIndex && is_int($key)) { |
||
163 | $this->bindValue($key + 1, $val); |
||
164 | } else { |
||
165 | $this->bindValue($key, $val); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if (! sasql_stmt_execute($this->stmt)) { |
||
171 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
172 | } |
||
173 | |||
174 | $this->result = sasql_stmt_result_metadata($this->stmt); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | * |
||
180 | * @throws SQLAnywhereException |
||
181 | */ |
||
182 | public function fetch(?int $fetchMode = null, ...$args) |
||
183 | { |
||
184 | if (! is_resource($this->result)) { |
||
185 | return false; |
||
186 | } |
||
187 | |||
188 | $fetchMode = $fetchMode ?: $this->defaultFetchMode; |
||
189 | |||
190 | switch ($fetchMode) { |
||
191 | case FetchMode::COLUMN: |
||
192 | return $this->fetchColumn(); |
||
193 | |||
194 | case FetchMode::ASSOCIATIVE: |
||
195 | return sasql_fetch_assoc($this->result); |
||
196 | |||
197 | case FetchMode::MIXED: |
||
198 | return sasql_fetch_array($this->result, SASQL_BOTH); |
||
199 | |||
200 | case FetchMode::CUSTOM_OBJECT: |
||
201 | $className = $this->defaultFetchClass; |
||
202 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
203 | |||
204 | if (count($args) > 0) { |
||
205 | $className = $args[0]; |
||
206 | $ctorArgs = $args[1] ?? []; |
||
207 | } |
||
208 | |||
209 | $result = sasql_fetch_object($this->result); |
||
210 | |||
211 | if ($result instanceof stdClass) { |
||
212 | $result = $this->castObject($result, $className, $ctorArgs); |
||
213 | } |
||
214 | |||
215 | return $result; |
||
216 | |||
217 | case FetchMode::NUMERIC: |
||
218 | return sasql_fetch_row($this->result); |
||
219 | |||
220 | case FetchMode::STANDARD_OBJECT: |
||
221 | return sasql_fetch_object($this->result); |
||
222 | |||
223 | default: |
||
224 | throw new SQLAnywhereException(sprintf('Fetch mode is not supported %d.', $fetchMode)); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function fetchAll(?int $fetchMode = null, ...$args) : array |
||
232 | { |
||
233 | $rows = []; |
||
234 | |||
235 | switch ($fetchMode) { |
||
236 | case FetchMode::CUSTOM_OBJECT: |
||
237 | while (($row = $this->fetch(...func_get_args())) !== false) { |
||
238 | $rows[] = $row; |
||
239 | } |
||
240 | break; |
||
241 | |||
242 | case FetchMode::COLUMN: |
||
243 | while (($row = $this->fetchColumn()) !== false) { |
||
244 | $rows[] = $row; |
||
245 | } |
||
246 | break; |
||
247 | |||
248 | default: |
||
249 | while (($row = $this->fetch($fetchMode)) !== false) { |
||
250 | $rows[] = $row; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | return $rows; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function fetchColumn(int $columnIndex = 0) |
||
261 | { |
||
262 | $row = $this->fetch(FetchMode::NUMERIC); |
||
263 | |||
264 | if ($row === false) { |
||
265 | return false; |
||
266 | } |
||
267 | |||
268 | if (! array_key_exists($columnIndex, $row)) { |
||
269 | throw InvalidColumnIndex::new($columnIndex, count($row)); |
||
270 | } |
||
271 | |||
272 | return $row[$columnIndex]; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | 54 | public function getIterator() |
|
279 | { |
||
280 | 54 | return new StatementIterator($this); |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function rowCount() : int |
||
287 | { |
||
288 | return sasql_stmt_affected_rows($this->stmt); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function setFetchMode(int $fetchMode, ...$args) : void |
||
295 | { |
||
296 | $this->defaultFetchMode = $fetchMode; |
||
297 | |||
298 | if (isset($args[0])) { |
||
299 | $this->defaultFetchClass = $args[0]; |
||
300 | } |
||
301 | |||
302 | if (! isset($args[1])) { |
||
303 | return; |
||
304 | } |
||
305 | |||
306 | $this->defaultFetchClassCtorArgs = (array) $args[1]; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Casts a stdClass object to the given class name mapping its' properties. |
||
311 | * |
||
312 | * @param stdClass $sourceObject Object to cast from. |
||
313 | * @param string|object $destinationClass Name of the class or class instance to cast to. |
||
314 | * @param mixed[] $ctorArgs Arguments to use for constructing the destination class instance. |
||
315 | * |
||
316 | * @throws SQLAnywhereException |
||
317 | */ |
||
318 | private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = []) : object |
||
353 | } |
||
354 |