| Total Complexity | 52 |
| Total Lines | 246 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 15 | class PDOStatement extends BaseStatement |
||
| 16 | { |
||
| 17 | private $parent; |
||
| 18 | |||
| 19 | public $statement; |
||
| 20 | |||
| 21 | public $timeout; |
||
| 22 | |||
| 23 | public $bindMap = []; |
||
| 24 | |||
| 25 | public $cursor = -1; |
||
| 26 | |||
| 27 | public $cursorOrientation = PDO::FETCH_ORI_NEXT; |
||
| 28 | |||
| 29 | public $resultSet = []; |
||
| 30 | |||
| 31 | public $fetchStyle = PDO::FETCH_BOTH; |
||
| 32 | |||
| 33 | public function __construct(PDO $parent, Statement $statement, array $driverOptions = []) |
||
| 34 | { |
||
| 35 | $this->parent = $parent; |
||
| 36 | $this->statement = $statement; |
||
| 37 | $this->timeout = $driverOptions['timeout'] ?? -1; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function errorCode() |
||
| 41 | { |
||
| 42 | return $this->statement->errno; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function errorInfo() |
||
| 46 | { |
||
| 47 | return $this->statement->error; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function rowCount() |
||
| 51 | { |
||
| 52 | return $this->statement->affected_rows; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function bindParam($parameter, &$variable, $type = null, $maxlen = null, $driverdata = null) |
||
| 56 | { |
||
| 57 | if (! is_string($parameter) && ! is_int($parameter)) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | $parameter = ltrim($parameter, ':'); |
||
| 62 | $this->bindMap[$parameter] = &$variable; |
||
| 63 | |||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function bindValue($parameter, $variable, $type = null) |
||
| 68 | { |
||
| 69 | if (! is_string($parameter) && ! is_int($parameter)) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (is_object($variable)) { |
||
| 74 | if (! method_exists($variable, '__toString')) { |
||
| 75 | return false; |
||
| 76 | } else { |
||
| 77 | $variable = (string) $variable; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $parameter = ltrim($parameter, ':'); |
||
| 82 | $this->bindMap[$parameter] = $variable; |
||
| 83 | |||
| 84 | return true; |
||
| 85 | } |
||
| 86 | |||
| 87 | private function afterExecute() |
||
| 88 | { |
||
| 89 | $this->cursor = -1; |
||
| 90 | $this->bindMap = []; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function execute($inputParameters = null) |
||
| 94 | { |
||
| 95 | if (! empty($inputParameters)) { |
||
| 96 | foreach ($inputParameters as $key => $value) { |
||
| 97 | $this->bindParam($key, $value); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $inputParameters = []; |
||
| 102 | if (! empty($this->statement->bindKeyMap)) { |
||
| 103 | foreach ($this->statement->bindKeyMap as $nameKey => $numKey) { |
||
|
|
|||
| 104 | if (isset($this->bindMap[$nameKey])) { |
||
| 105 | $inputParameters[$numKey] = $this->bindMap[$nameKey]; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } else { |
||
| 109 | $inputParameters = $this->bindMap; |
||
| 110 | } |
||
| 111 | |||
| 112 | $result = $this->statement->execute($inputParameters, $this->timeout); |
||
| 113 | $this->resultSet = ($ok = $result !== false) ? $result : []; |
||
| 114 | $this->afterExecute(); |
||
| 115 | |||
| 116 | if ($result === false) { |
||
| 117 | throw new \PDOException($this->errorInfo(), $this->errorCode()); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $ok; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function setFetchMode($fetchStyle, $params = null) |
||
| 124 | { |
||
| 125 | $this->fetchStyle = $fetchStyle; |
||
| 126 | } |
||
| 127 | |||
| 128 | private function __executeWhenStringQueryEmpty() |
||
| 129 | { |
||
| 130 | if (is_string($this->statement) && empty($this->resultSet)) { |
||
| 131 | $this->resultSet = $this->parent->client->query($this->statement); |
||
| 132 | $this->afterExecute(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | private function transBoth($rawData) |
||
| 150 | } |
||
| 151 | |||
| 152 | private function transStyle( |
||
| 153 | $rawData, |
||
| 154 | $fetchStyle = null, |
||
| 155 | $fetchArgument = null, |
||
| 156 | $ctorArgs = null |
||
| 157 | ) |
||
| 158 | { |
||
| 159 | if (! is_array($rawData)) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | if (empty($rawData)) { |
||
| 163 | return $rawData; |
||
| 164 | } |
||
| 165 | |||
| 166 | $fetchStyle = is_null($fetchStyle) ? $this->fetchStyle : $fetchStyle; |
||
| 167 | $ctorArgs = is_null($ctorArgs) ? [] : $ctorArgs; |
||
| 168 | |||
| 169 | $resultSet = []; |
||
| 170 | switch ($fetchStyle) { |
||
| 171 | case PDO::FETCH_BOTH: |
||
| 172 | $resultSet = $this->transBoth($rawData); |
||
| 173 | break; |
||
| 174 | case PDO::FETCH_COLUMN: |
||
| 175 | $resultSet = array_column( |
||
| 176 | is_numeric($fetchArgument) ? $this->transBoth($rawData) : $rawData, |
||
| 177 | $fetchArgument |
||
| 178 | ); |
||
| 179 | break; |
||
| 180 | case PDO::FETCH_OBJ: |
||
| 181 | foreach ($rawData as $row) { |
||
| 182 | $resultSet[] = (object) $row; |
||
| 183 | } |
||
| 184 | break; |
||
| 185 | case PDO::FETCH_NUM: |
||
| 186 | foreach ($rawData as $row) { |
||
| 187 | $resultSet[] = array_values($row); |
||
| 188 | } |
||
| 189 | break; |
||
| 190 | case PDO::FETCH_ASSOC: |
||
| 191 | default: |
||
| 192 | return $rawData; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $resultSet; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function fetch( |
||
| 199 | $fetchStyle = null, |
||
| 200 | $cursorOrientation = null, |
||
| 201 | $cursorOffset = null, |
||
| 202 | $fetchArgument = null |
||
| 203 | ) |
||
| 204 | { |
||
| 205 | $this->__executeWhenStringQueryEmpty(); |
||
| 206 | |||
| 207 | $cursorOrientation = is_null($cursorOrientation) ? PDO::FETCH_ORI_NEXT : $cursorOrientation; |
||
| 208 | $cursorOffset = is_null($cursorOffset) ? 0 : (int) $cursorOffset; |
||
| 209 | |||
| 210 | switch ($cursorOrientation) { |
||
| 211 | case PDO::FETCH_ORI_ABS: |
||
| 212 | $this->cursor = $cursorOffset; |
||
| 213 | break; |
||
| 214 | case PDO::FETCH_ORI_REL: |
||
| 215 | $this->cursor += $cursorOffset; |
||
| 216 | break; |
||
| 217 | case PDO::FETCH_ORI_NEXT: |
||
| 218 | default: |
||
| 219 | $this->cursor++; |
||
| 220 | } |
||
| 221 | |||
| 222 | if (isset($this->resultSet[$this->cursor])) { |
||
| 223 | $result = $this->resultSet[$this->cursor]; |
||
| 224 | unset($this->resultSet[$this->cursor]); |
||
| 225 | } else { |
||
| 226 | $result = false; |
||
| 227 | } |
||
| 228 | |||
| 229 | if (empty($result)) { |
||
| 230 | return $result; |
||
| 231 | } else { |
||
| 232 | return $this->transStyle([$result], $fetchStyle, $fetchArgument)[0]; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns a single column from the next row of a result set or FALSE if there are no more rows. |
||
| 238 | * |
||
| 239 | * @param int|null $columnNumber |
||
| 240 | * |
||
| 241 | * 0-indexed number of the column you wish to retrieve from the row. |
||
| 242 | * If no value is supplied, PDOStatement::fetchColumn() fetches the first column. |
||
| 243 | * |
||
| 244 | * @return bool|mixed |
||
| 245 | */ |
||
| 246 | public function fetchColumn($columnNumber = null) |
||
| 252 | } |
||
| 253 | |||
| 254 | public function fetchAll($fetchStyle = null, $fetchArgument = null, $ctorArgs = null) |
||
| 261 | } |
||
| 262 | } |
||
| 263 |