Total Complexity | 48 |
Total Lines | 205 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ConnectionAdapter 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 ConnectionAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class ConnectionAdapter implements IDBConnection { |
||
41 | |||
42 | /** @var Connection */ |
||
43 | private $inner; |
||
44 | |||
45 | public function __construct(Connection $inner) { |
||
46 | $this->inner = $inner; |
||
47 | } |
||
48 | |||
49 | public function getQueryBuilder(): IQueryBuilder { |
||
50 | return $this->inner->getQueryBuilder(); |
||
51 | } |
||
52 | |||
53 | public function prepare($sql, $limit = null, $offset = null): IPreparedStatement { |
||
54 | try { |
||
55 | return new PreparedStatement( |
||
56 | $this->inner->prepare($sql, $limit, $offset) |
||
57 | ); |
||
58 | } catch (Exception $e) { |
||
59 | throw DbalException::wrap($e); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public function executeQuery(string $sql, array $params = [], $types = []): IResult { |
||
64 | try { |
||
65 | return new ResultAdapter( |
||
66 | $this->inner->executeQuery($sql, $params, $types) |
||
67 | ); |
||
68 | } catch (Exception $e) { |
||
69 | throw DbalException::wrap($e); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | public function executeUpdate(string $sql, array $params = [], array $types = []): int { |
||
74 | try { |
||
75 | return $this->inner->executeUpdate($sql, $params, $types); |
||
76 | } catch (Exception $e) { |
||
77 | throw DbalException::wrap($e); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | public function executeStatement($sql, array $params = [], array $types = []): int { |
||
82 | try { |
||
83 | return $this->inner->executeStatement($sql, $params, $types); |
||
84 | } catch (Exception $e) { |
||
85 | throw DbalException::wrap($e); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public function lastInsertId(string $table): int { |
||
90 | try { |
||
91 | return (int)$this->inner->lastInsertId($table); |
||
92 | } catch (Exception $e) { |
||
93 | throw DbalException::wrap($e); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | public function insertIfNotExist(string $table, array $input, array $compare = null) { |
||
98 | try { |
||
99 | return $this->inner->insertIfNotExist($table, $input, $compare); |
||
100 | } catch (Exception $e) { |
||
101 | throw DbalException::wrap($e); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function insertIgnoreConflict(string $table, array $values): int { |
||
106 | try { |
||
107 | return $this->inner->insertIgnoreConflict($table, $values); |
||
108 | } catch (Exception $e) { |
||
109 | throw DbalException::wrap($e); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int { |
||
114 | try { |
||
115 | return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues); |
||
116 | } catch (Exception $e) { |
||
117 | throw DbalException::wrap($e); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | public function lockTable($tableName): void { |
||
122 | try { |
||
123 | $this->inner->lockTable($tableName); |
||
124 | } catch (Exception $e) { |
||
125 | throw DbalException::wrap($e); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public function unlockTable(): void { |
||
130 | try { |
||
131 | $this->inner->unlockTable(); |
||
132 | } catch (Exception $e) { |
||
133 | throw DbalException::wrap($e); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function beginTransaction(): void { |
||
138 | try { |
||
139 | $this->inner->beginTransaction(); |
||
140 | } catch (Exception $e) { |
||
141 | throw DbalException::wrap($e); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public function inTransaction(): bool { |
||
146 | return $this->inner->inTransaction(); |
||
147 | } |
||
148 | |||
149 | public function commit(): void { |
||
150 | try { |
||
151 | $this->inner->commit(); |
||
152 | } catch (Exception $e) { |
||
153 | throw DbalException::wrap($e); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function rollBack(): void { |
||
158 | try { |
||
159 | $this->inner->rollBack(); |
||
160 | } catch (Exception $e) { |
||
161 | throw DbalException::wrap($e); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | public function getError(): string { |
||
166 | return $this->inner->getError(); |
||
167 | } |
||
168 | |||
169 | public function errorCode() { |
||
170 | return $this->inner->errorCode(); |
||
171 | } |
||
172 | |||
173 | public function errorInfo() { |
||
174 | return $this->inner->errorInfo(); |
||
175 | } |
||
176 | |||
177 | public function connect(): bool { |
||
182 | } |
||
183 | } |
||
184 | |||
185 | public function close(): void { |
||
186 | $this->inner->close(); |
||
187 | } |
||
188 | |||
189 | public function quote($input, $type = IQueryBuilder::PARAM_STR) { |
||
190 | return $this->inner->quote($input, $type); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @todo we are leaking a 3rdparty type here |
||
195 | */ |
||
196 | public function getDatabasePlatform(): AbstractPlatform { |
||
198 | } |
||
199 | |||
200 | public function dropTable(string $table): void { |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public function tableExists(string $table): bool { |
||
209 | try { |
||
210 | return $this->inner->tableExists($table); |
||
211 | } catch (Exception $e) { |
||
212 | throw DbalException::wrap($e); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | public function escapeLikeParameter(string $param): string { |
||
218 | } |
||
219 | |||
220 | public function supports4ByteText(): bool { |
||
221 | return $this->inner->supports4ByteText(); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @todo leaks a 3rdparty type |
||
226 | */ |
||
227 | public function createSchema(): Schema { |
||
228 | try { |
||
229 | return $this->inner->createSchema(); |
||
230 | } catch (Exception $e) { |
||
231 | throw DbalException::wrap($e); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | public function migrateToSchema(Schema $toSchema): void { |
||
236 | try { |
||
237 | $this->inner->migrateToSchema($toSchema); |
||
238 | } catch (Exception $e) { |
||
239 | throw DbalException::wrap($e); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | public function getInner(): Connection { |
||
245 | } |
||
246 | } |
||
247 |