| Total Complexity | 50 |
| Total Lines | 322 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 2 | Features | 0 |
Complex classes like PDO 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 PDO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class PDO implements Source |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected array $config = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var null|_PDO |
||
| 24 | */ |
||
| 25 | protected $db = null; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | private string $pdo_class = _PDO::class; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param array $config |
||
| 32 | * @param string $pdo_class |
||
| 33 | */ |
||
| 34 | public function __construct(array $config = [], string $pdo_class = _PDO::class) |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @psalm-suppress MoreSpecificReturnType |
||
| 61 | * @psalm-suppress PropertyTypeCoercion |
||
| 62 | * @psalm-suppress LessSpecificReturnStatement |
||
| 63 | * @return _PDO |
||
| 64 | */ |
||
| 65 | private function handler(): object |
||
| 66 | { |
||
| 67 | if ($this->db) { |
||
| 68 | return $this->db; |
||
| 69 | } |
||
| 70 | |||
| 71 | $scheme = $this->config['scheme'] ?? 'mysql'; |
||
| 72 | $host = $this->config['host']; |
||
| 73 | $db = $this->config['db']; |
||
| 74 | $user = $this->config['user']; |
||
| 75 | $pass = $this->config['pass'] ?? ''; |
||
| 76 | $port = ($this->config['port'] ?? false) ? ";port={$this->config['port']}" : ''; |
||
| 77 | if (empty($this->config['query'])) { |
||
| 78 | $modifiers = ''; |
||
| 79 | } else { |
||
| 80 | $modifiers = ';' . str_replace('&', ';', $this->config['query']); |
||
| 81 | } |
||
| 82 | $opts = [ |
||
| 83 | _PDO::ATTR_EMULATE_PREPARES => false, |
||
| 84 | _PDO::ATTR_ERRMODE => _PDO::ERRMODE_EXCEPTION, |
||
| 85 | |||
| 86 | 'useUnicode' => true, |
||
| 87 | 'characterEncoding' => 'UTF-8', |
||
| 88 | ]; |
||
| 89 | $dsn = "{$scheme}:host={$host}{$port}{$modifiers};dbname={$db}"; |
||
| 90 | $class = $this->pdo_class; |
||
| 91 | try { |
||
| 92 | $this->db = new $class($dsn, $user, $pass, $opts); |
||
| 93 | |||
| 94 | // @TODO use this? |
||
| 95 | // $this->db->setAttribute( _PDO::ATTR_EMULATE_PREPARES, false); |
||
| 96 | } catch (\PDOException $e) { |
||
| 97 | throw new ConnectionError("Unable to connect to $host : $db with user $user"); |
||
| 98 | } |
||
| 99 | return $this->db; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param mixed $query |
||
| 104 | * @param array $params |
||
| 105 | * @return array|false false on error |
||
| 106 | */ |
||
| 107 | public function query($query, array $params = []) |
||
| 108 | { |
||
| 109 | Log::debug("PDO:QUERY [$query]"); |
||
| 110 | $result = $this->handler()->query($query); |
||
| 111 | return $result ? $result->fetchAll(_PDO::FETCH_ASSOC) : false; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $table |
||
| 116 | * @param array $conditions |
||
| 117 | * @param array $options |
||
| 118 | * @return null|array |
||
| 119 | * @throws \Error on finding more than 1 match |
||
| 120 | */ |
||
| 121 | public function one(string $table, array $conditions, array $options = []): ?array |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $table |
||
| 140 | * @param array $conditions |
||
| 141 | * @param array $options |
||
| 142 | * @return iterable |
||
| 143 | */ |
||
| 144 | public function find(string $table, array $conditions, array $options = []): iterable |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array $conditions |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | private function where(array $conditions): string |
||
| 183 | { |
||
| 184 | if (empty($conditions)) { |
||
| 185 | return ""; |
||
| 186 | } |
||
| 187 | $fun = function($o, $v) use ($conditions) : string { |
||
| 188 | if (is_array($conditions[$v])) { |
||
| 189 | $qa = []; |
||
| 190 | $index = 0; |
||
| 191 | foreach ($conditions[$v] as $value) { |
||
| 192 | $i = 1 + $index++; |
||
| 193 | $qa[] = ":c_{$v}_{$i}"; |
||
| 194 | } |
||
| 195 | $qs = join(', ', $qa); |
||
| 196 | return "{$o} AND {$v} IN ( $qs )"; |
||
| 197 | } else { |
||
| 198 | return "{$o} AND {$v} = :c_{$v}"; |
||
| 199 | } |
||
| 200 | }; |
||
| 201 | $where = trim(array_reduce(array_keys($conditions), $fun, ""), ' AND '); |
||
| 202 | return "WHERE {$where} "; |
||
| 203 | } |
||
| 204 | |||
| 205 | private function limit(array $options): string |
||
| 206 | { |
||
| 207 | return array_key_exists('limit', $options) ? "LIMIT :o_limit OFFSET :o_offset " : ''; |
||
| 208 | } |
||
| 209 | |||
| 210 | private function order(array $options): string |
||
| 211 | { |
||
| 212 | if (array_key_exists('order', $options)) { |
||
| 213 | // @TODO Add more protection? |
||
| 214 | return "ORDER BY {$options['order']} "; |
||
| 215 | } |
||
| 216 | return ''; |
||
| 217 | } |
||
| 218 | |||
| 219 | private function boundDebugString(array $conditions, array $options, array $data = []): string |
||
| 220 | { |
||
| 221 | $out = []; |
||
| 222 | foreach ($conditions as $k => $v) { |
||
| 223 | if (is_array($v)) { |
||
| 224 | $v = join(',', $v); |
||
| 225 | } |
||
| 226 | $out[] = "c_{$k}:'{$v}'"; |
||
| 227 | } |
||
| 228 | foreach ($data as $k => $v) { |
||
| 229 | $out[] = "d_{$k}:{$v}"; |
||
| 230 | } |
||
| 231 | foreach ($options as $k => $v) { |
||
| 232 | $out[] = "o_{$k}:{$v}"; |
||
| 233 | } |
||
| 234 | return join(", ", $out); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param array $options |
||
| 239 | * @param \PDOStatement $stmt |
||
| 240 | */ |
||
| 241 | private function bindPaginationToStatement(array $options, $stmt): void |
||
| 242 | { |
||
| 243 | if (array_key_exists('limit', $options)) { |
||
| 244 | $stmt->bindValue(":o_offset", (int) ($options['offset'] ?? 0), _PDO::PARAM_INT); |
||
| 245 | $stmt->bindValue(":o_limit", (int) $options['limit'], _PDO::PARAM_INT); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $table |
||
| 251 | * @param array $conditions |
||
| 252 | * @param array $data |
||
| 253 | * @param array $options |
||
| 254 | * @return int |
||
| 255 | */ |
||
| 256 | public function update(string $table, array $conditions, array $data, array $options = []): int |
||
| 278 | } |
||
| 279 | |||
| 280 | private function data(array $data): string |
||
| 281 | { |
||
| 282 | /** |
||
| 283 | * @param string $o |
||
| 284 | * @param mixed $v |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | $f = static fn(string $o, $v): string => "{$o}, {$v} = :d_{$v}"; |
||
| 288 | return trim((string) array_reduce(array_keys($data), $f, ""), ", "); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $table |
||
| 293 | * @param array $data |
||
| 294 | * @param array $options |
||
| 295 | * @return null|string |
||
| 296 | */ |
||
| 297 | public function insert(string $table, array $data, array $options = []): ?string |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $table |
||
| 315 | * @param array $conditions |
||
| 316 | * @param array $options |
||
| 317 | * @return int |
||
| 318 | */ |
||
| 319 | public function delete(string $table, array $conditions, array $options = []): int |
||
| 339 |