Complex classes like DBALMysqlResource 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 DBALMysqlResource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class DBALMysqlResource extends AbstractDBALResource |
||
11 | { |
||
12 | /** |
||
13 | * @var DriverInterface |
||
14 | */ |
||
15 | private $driver; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 31 | protected function getDriverOptions() |
|
21 | { |
||
22 | return [ |
||
23 | 31 | \PDO::MYSQL_ATTR_LOCAL_INFILE => true |
|
24 | ]; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 2 | public function deleteUsingTempPK($deleteTable, $tmpTable, $primaryKey = 'id') |
|
35 | |||
36 | /** |
||
37 | * @param string $deleteTable |
||
38 | * @param string $tmpTable |
||
39 | * @param string|string[] $primaryKey |
||
40 | * @return string |
||
41 | */ |
||
42 | 3 | public function getDeleteUsingTempPkSql($deleteTable, $tmpTable, $primaryKey) |
|
43 | { |
||
44 | 3 | $deleteTable = $this->connection->quoteIdentifier($deleteTable); |
|
45 | 3 | $tmpTable = $this->connection->quoteIdentifier($tmpTable); |
|
46 | 3 | if (!is_array($primaryKey)) { |
|
47 | 2 | $primaryKey = [$primaryKey]; |
|
48 | } |
||
49 | 3 | $primaryKey = array_map([$this->connection, 'quoteIdentifier'], $primaryKey); |
|
50 | 3 | $conditionParts = []; |
|
51 | 3 | foreach ($primaryKey as $key) { |
|
52 | 3 | $conditionParts[] = "`main_table`.$key=`tmp_table`.$key"; |
|
53 | } |
||
54 | 3 | $condition = implode('AND', $conditionParts); |
|
55 | return <<<MYSQL |
||
56 | 3 | DELETE main_table FROM $deleteTable AS main_table |
|
57 | 3 | JOIN $tmpTable AS tmp_table ON $condition |
|
58 | MYSQL; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | * @param string $delimiter |
||
64 | * @param string $enclosure |
||
65 | * @param string $escape |
||
66 | * @param string $termination |
||
67 | * @param bool $optionallyEnclosed |
||
68 | */ |
||
69 | 6 | public function loadData( |
|
85 | |||
86 | /** |
||
87 | * @param string $table |
||
88 | * @param string $file |
||
89 | * @param bool|false $local |
||
90 | * @param array $columns |
||
91 | * @param array $set |
||
92 | * @param string $delimiter |
||
93 | * @param string $enclosure |
||
94 | * @param string $escape |
||
95 | * @param string $termination |
||
96 | * @param bool|true $optionallyEnclosed |
||
97 | * @return string |
||
98 | */ |
||
99 | 7 | public function getLoadDataSql( |
|
100 | $table, |
||
101 | $file, |
||
102 | $local = false, |
||
103 | array $columns = [], |
||
104 | array $set = [], |
||
105 | $delimiter = ",", |
||
106 | $enclosure = '"', |
||
107 | $escape = "\\", |
||
108 | $termination = '\n', |
||
109 | $optionallyEnclosed = true |
||
110 | ) { |
||
111 | 7 | $localKey = $local ? 'LOCAL' : ''; |
|
112 | 7 | $table = $this->connection->quoteIdentifier($table); |
|
113 | 7 | $optionalKey = $optionallyEnclosed ? 'OPTIONALLY' : ''; |
|
114 | 7 | if (!empty($columns)) { |
|
115 | 1 | $columns = '(' . implode(',', $columns) . ')'; |
|
116 | } else { |
||
117 | 6 | $columns = ''; |
|
118 | } |
||
119 | 7 | if (!empty($set)) { |
|
120 | 1 | $setParts = []; |
|
121 | 1 | foreach ($set as $key => $val) { |
|
122 | 1 | $setParts[] = "$key=$val"; |
|
123 | } |
||
124 | 1 | $set = 'SET ' . implode(',', $setParts); |
|
125 | } else { |
||
126 | 6 | $set = ''; |
|
127 | } |
||
128 | 7 | $escape = $this->connection->quote($escape); |
|
129 | return <<<MYSQL |
||
130 | 7 | LOAD DATA $localKey INFILE '$file' |
|
131 | 7 | INTO TABLE $table |
|
132 | FIELDS |
||
133 | 7 | TERMINATED BY '$delimiter' |
|
134 | 7 | $optionalKey ENCLOSED BY '$enclosure' |
|
135 | 7 | ESCAPED BY $escape |
|
136 | LINES |
||
137 | 7 | TERMINATED BY '$termination' |
|
138 | 7 | $columns |
|
139 | 7 | $set |
|
140 | MYSQL; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 9 | public function move( |
|
157 | |||
158 | /** |
||
159 | * @param string $fromTable |
||
160 | * @param string $toTable |
||
161 | * @param array $columns |
||
162 | * @param array $conditions |
||
163 | * @param array $orderBy |
||
164 | * @param string $dir |
||
165 | * @return string |
||
166 | */ |
||
167 | 18 | public function getMoveSql( |
|
168 | $fromTable, |
||
169 | $toTable, |
||
170 | array $columns = [], |
||
171 | array $conditions = [], |
||
172 | array $orderBy = [], |
||
173 | $dir = 'ASC' |
||
174 | ) { |
||
175 | 18 | $selectColumns = '*'; |
|
176 | 18 | $onDuplicate = ''; |
|
177 | 18 | $fromTable = $this->connection->quoteIdentifier($fromTable); |
|
178 | 18 | $toTable = $this->connection->quoteIdentifier($toTable); |
|
179 | 18 | if (!empty($columns)) { |
|
180 | 18 | $columns = array_map([$this->connection, 'quoteIdentifier'], $columns); |
|
181 | 18 | $selectColumns = implode(',', $columns); |
|
182 | 18 | $duplicateParts = array_map(function ($var) { |
|
183 | 18 | return "$var=VALUES($var)"; |
|
184 | 18 | }, $columns); |
|
185 | 18 | $columns = '(' . $selectColumns . ')'; |
|
186 | 18 | $onDuplicate = 'ON DUPLICATE KEY UPDATE ' . implode(',', $duplicateParts); |
|
187 | } else { |
||
188 | $columns = ''; |
||
189 | } |
||
190 | 18 | if (!empty($conditions)) { |
|
191 | 10 | $conditionParts = []; |
|
192 | 10 | foreach ($conditions as $key => $val) { |
|
193 | 10 | $key = $this->connection->quoteIdentifier($key); |
|
194 | 10 | if (is_array($val)) { |
|
195 | 9 | $conditionParts[] = $this->getParsedCondition("$fromTable.$key", $val); |
|
196 | } else { |
||
197 | 1 | $val = $this->connection->quote($val); |
|
198 | 6 | $conditionParts[] = "$fromTable.$key=$val"; |
|
199 | } |
||
200 | } |
||
201 | 6 | $conditions = 'WHERE ' . implode('AND', $conditionParts); |
|
202 | } else { |
||
203 | 8 | $conditions = ''; |
|
204 | } |
||
205 | 14 | if (!empty($orderBy)) { |
|
206 | 8 | $orderBy = array_map([$this->connection, 'quoteIdentifier'], $orderBy); |
|
207 | 8 | $orderBy = implode(',', $orderBy); |
|
208 | } else { |
||
209 | 6 | $orderBy = 'NULL'; |
|
210 | } |
||
211 | return <<<MYSQL |
||
212 | 14 | INSERT INTO $toTable $columns |
|
213 | 14 | SELECT $selectColumns FROM $fromTable |
|
214 | 14 | $conditions |
|
215 | 14 | ORDER BY $orderBy $dir |
|
216 | 14 | $onDuplicate |
|
217 | MYSQL; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Return valid condition |
||
222 | * @param $column |
||
223 | * @param array $value |
||
224 | * @throws ParsingException |
||
225 | * @return string |
||
226 | */ |
||
227 | 9 | protected function getParsedCondition($column, array $value) |
|
228 | { |
||
229 | 9 | if (count($value) > 1) { |
|
230 | 1 | throw new ParsingException("Condition should contain only 1 element"); |
|
231 | } |
||
232 | 8 | $operation = key($value); |
|
233 | 8 | $actualValue = current($value); |
|
234 | switch ($operation) { |
||
235 | 8 | case 'neq': |
|
236 | 1 | $string = "$column<>{$this->connection->quote($actualValue)}"; |
|
237 | 1 | break; |
|
238 | 7 | case 'eq': |
|
239 | 1 | $string = "$column={$this->connection->quote($actualValue)}"; |
|
240 | 1 | break; |
|
241 | 6 | case 'in': |
|
242 | 2 | if (!is_array($actualValue)) { |
|
243 | 1 | throw new ParsingException("Can not use 'in' operation with non array."); |
|
244 | } |
||
245 | 1 | $actualValue = array_map([$this->connection, 'quote'], $actualValue); |
|
246 | 1 | $glued = implode(',', $actualValue); |
|
247 | 1 | $string = "$column in ($glued)"; |
|
248 | 1 | break; |
|
249 | 4 | case 'nin': |
|
250 | 3 | if (!is_array($actualValue)) { |
|
251 | 1 | throw new ParsingException("Can not use 'nin' operation with non array."); |
|
252 | } |
||
253 | 2 | $actualValue = array_map([$this->connection, 'quote'], $actualValue); |
|
254 | 2 | $glued = implode(',', $actualValue); |
|
255 | 2 | $string = "$column not in ($glued)"; |
|
256 | 2 | break; |
|
257 | default: |
||
258 | 1 | throw new ParsingException(sprintf("Could not resolve condition operation %s.", $operation)); |
|
259 | } |
||
260 | 5 | return $string; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | 3 | public function dumpData($table, array $columns = [], $limit = 1000, $offset = 0) |
|
278 | |||
279 | /** |
||
280 | * @param string $table |
||
281 | * @param array $columns |
||
282 | * @return string |
||
283 | */ |
||
284 | 4 | public function getDumpDataSql($table, array $columns = []) |
|
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | 11 | public function createTmpTable($name, array $columns) |
|
310 | |||
311 | /** |
||
312 | * @param string $name |
||
313 | * @param array $columns |
||
314 | * @return array |
||
315 | * @throws \Doctrine\DBAL\DBALException |
||
316 | */ |
||
317 | 12 | public function getCreateTableSql($name, array $columns) |
|
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | 31 | protected function getDriver() |
|
352 | } |
||
353 |
A high number of parameters is generally an indication that you should consider creating a dedicated object for the parameters.
Let’s take a look at an example:
could be refactored to: