| Total Complexity | 52 |
| Total Lines | 320 |
| Duplicated Lines | 0 % |
| Coverage | 1.8% |
| Changes | 0 | ||
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 |
||
| 42 | class SQLAnywhereStatement implements IteratorAggregate, Statement |
||
| 43 | { |
||
| 44 | /** @var resource The connection resource. */ |
||
| 45 | private $conn; |
||
| 46 | |||
| 47 | /** @var string Name of the default class to instantiate when fetching class instances. */ |
||
| 48 | private $defaultFetchClass = '\stdClass'; |
||
| 49 | |||
| 50 | /** @var mixed[] Constructor arguments for the default class to instantiate when fetching class instances. */ |
||
| 51 | private $defaultFetchClassCtorArgs = []; |
||
| 52 | |||
| 53 | /** @var int Default fetch mode to use. */ |
||
| 54 | private $defaultFetchMode = FetchMode::MIXED; |
||
| 55 | |||
| 56 | /** @var resource The result set resource to fetch. */ |
||
| 57 | private $result; |
||
| 58 | |||
| 59 | /** @var resource The prepared SQL statement to execute. */ |
||
| 60 | private $stmt; |
||
| 61 | |||
| 62 | /** @var mixed[] The references to bound parameter values. */ |
||
| 63 | private $boundValues = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Prepares given statement for given connection. |
||
| 67 | * |
||
| 68 | * @param resource $conn The connection resource to use. |
||
| 69 | * @param string $sql The SQL statement to prepare. |
||
| 70 | * |
||
| 71 | * @throws SQLAnywhereException |
||
| 72 | */ |
||
| 73 | public function __construct($conn, $sql) |
||
| 74 | { |
||
| 75 | if (! is_resource($conn)) { |
||
| 76 | throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->conn = $conn; |
||
| 80 | $this->stmt = sasql_prepare($conn, $sql); |
||
| 81 | |||
| 82 | if (! is_resource($this->stmt)) { |
||
| 83 | throw SQLAnywhereException::fromSQLAnywhereError($conn); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | * |
||
| 90 | * @throws SQLAnywhereException |
||
| 91 | */ |
||
| 92 | public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void |
||
| 93 | { |
||
| 94 | switch ($type) { |
||
| 95 | case ParameterType::INTEGER: |
||
| 96 | case ParameterType::BOOLEAN: |
||
| 97 | $type = 'i'; |
||
| 98 | break; |
||
| 99 | |||
| 100 | case ParameterType::LARGE_OBJECT: |
||
| 101 | $type = 'b'; |
||
| 102 | break; |
||
| 103 | |||
| 104 | case ParameterType::NULL: |
||
| 105 | case ParameterType::STRING: |
||
| 106 | case ParameterType::BINARY: |
||
| 107 | $type = 's'; |
||
| 108 | break; |
||
| 109 | |||
| 110 | default: |
||
| 111 | throw new SQLAnywhereException('Unknown type: ' . $type); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->boundValues[$column] =& $variable; |
||
| 115 | |||
| 116 | if (! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) { |
||
| 117 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function bindValue($param, $value, $type = ParameterType::STRING) : void |
||
| 125 | { |
||
| 126 | $this->bindParam($param, $value, $type); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public function closeCursor() : void |
||
| 133 | { |
||
| 134 | sasql_stmt_reset($this->stmt); |
||
|
1 ignored issue
–
show
|
|||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function columnCount() |
||
| 141 | { |
||
| 142 | return sasql_stmt_field_count($this->stmt); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function errorCode() |
||
| 149 | { |
||
| 150 | return sasql_stmt_errno($this->stmt); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function errorInfo() |
||
| 157 | { |
||
| 158 | return sasql_stmt_error($this->stmt); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | * |
||
| 164 | * @throws SQLAnywhereException |
||
| 165 | */ |
||
| 166 | public function execute($params = null) : void |
||
| 167 | { |
||
| 168 | if (is_array($params)) { |
||
| 169 | $hasZeroIndex = array_key_exists(0, $params); |
||
| 170 | |||
| 171 | foreach ($params as $key => $val) { |
||
| 172 | if ($hasZeroIndex && is_int($key)) { |
||
| 173 | $this->bindValue($key + 1, $val); |
||
| 174 | } else { |
||
| 175 | $this->bindValue($key, $val); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if (! sasql_stmt_execute($this->stmt)) { |
||
| 181 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->result = sasql_stmt_result_metadata($this->stmt); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | * |
||
| 190 | * @throws SQLAnywhereException |
||
| 191 | */ |
||
| 192 | public function fetch($fetchMode = null, ...$args) |
||
| 193 | { |
||
| 194 | if (! is_resource($this->result)) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | $fetchMode = $fetchMode ?: $this->defaultFetchMode; |
||
| 199 | |||
| 200 | switch ($fetchMode) { |
||
| 201 | case FetchMode::COLUMN: |
||
| 202 | return $this->fetchColumn(); |
||
| 203 | |||
| 204 | case FetchMode::ASSOCIATIVE: |
||
| 205 | return sasql_fetch_assoc($this->result); |
||
| 206 | |||
| 207 | case FetchMode::MIXED: |
||
| 208 | return sasql_fetch_array($this->result, SASQL_BOTH); |
||
| 209 | |||
| 210 | case FetchMode::CUSTOM_OBJECT: |
||
| 211 | $className = $this->defaultFetchClass; |
||
| 212 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
| 213 | |||
| 214 | if (count($args) > 0) { |
||
| 215 | $className = $args[0]; |
||
| 216 | $ctorArgs = $args[1] ?? []; |
||
| 217 | } |
||
| 218 | |||
| 219 | $result = sasql_fetch_object($this->result); |
||
| 220 | |||
| 221 | if ($result instanceof stdClass) { |
||
| 222 | $result = $this->castObject($result, $className, $ctorArgs); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $result; |
||
| 226 | |||
| 227 | case FetchMode::NUMERIC: |
||
| 228 | return sasql_fetch_row($this->result); |
||
| 229 | |||
| 230 | case FetchMode::STANDARD_OBJECT: |
||
| 231 | return sasql_fetch_object($this->result); |
||
| 232 | |||
| 233 | default: |
||
| 234 | throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function fetchAll($fetchMode = null, ...$args) |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function fetchColumn($columnIndex = 0) |
||
| 271 | { |
||
| 272 | $row = $this->fetch(FetchMode::NUMERIC); |
||
| 273 | |||
| 274 | if ($row === false) { |
||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | if (! array_key_exists($columnIndex, $row)) { |
||
| 279 | throw DBALException::invalidColumnIndex($columnIndex, count($row)); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | 46 | public function getIterator() |
|
| 287 | { |
||
| 288 | 46 | return new StatementIterator($this); |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public function rowCount() : int |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function setFetchMode($fetchMode, ...$args) : void |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Casts a stdClass object to the given class name mapping its' properties. |
||
| 319 | * |
||
| 320 | * @param stdClass $sourceObject Object to cast from. |
||
| 321 | * @param string|object $destinationClass Name of the class or class instance to cast to. |
||
| 322 | * @param mixed[] $ctorArgs Arguments to use for constructing the destination class instance. |
||
| 323 | * |
||
| 324 | * @return object |
||
| 325 | * |
||
| 326 | * @throws SQLAnywhereException |
||
| 327 | */ |
||
| 328 | private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = []) |
||
| 362 | } |
||
| 363 | } |
||
| 364 |