Complex classes like SQLite 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 SQLite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class SQLite extends _Abstract { |
||
13 | /** |
||
14 | * @var \SQLite3 Instance of DB connection |
||
15 | */ |
||
16 | protected $instance; |
||
17 | /** |
||
18 | * @param string $database Ignored for SQLite |
||
19 | * @param string $user Ignored for SQLite |
||
20 | * @param string $password Ignored for SQLite |
||
21 | * @param string $host Path to database file, relatively to website root or absolute |
||
22 | * @param string $prefix |
||
23 | */ |
||
24 | 32 | public function __construct ($database, $user = '', $password = '', $host = '', $prefix = '') { |
|
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | 32 | public function q ($query, ...$params) { |
|
48 | /** |
||
49 | * Convert small subset of MySQL queries into SQLite-compatible syntax |
||
50 | * |
||
51 | * @param string $query |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 32 | protected function convert_sql ($query) { |
|
58 | /** |
||
59 | * @inheritdoc |
||
60 | * |
||
61 | * @return false|SQLite3Result |
||
62 | */ |
||
63 | 32 | protected function q_internal ($query, $parameters = []) { |
|
81 | /** |
||
82 | * @inheritdoc |
||
83 | * |
||
84 | * @param false|SQLite3Result $query_result |
||
85 | */ |
||
86 | 32 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
102 | /** |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | 24 | public function id () { |
|
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 1 | public function affected () { |
|
114 | /** |
||
115 | * @inheritdoc |
||
116 | * |
||
117 | * @param false|SQLite3Result $query_result |
||
118 | */ |
||
119 | 27 | public function free ($query_result) { |
|
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | 3 | public function columns ($table, $like = false) { |
|
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | 1 | public function tables ($like = false) { |
|
163 | 1 | if ($like) { |
|
164 | 1 | $like = $this->s($like); |
|
165 | 1 | return $this->qfas( |
|
166 | "SELECT `name` |
||
167 | FROM `sqlite_master` |
||
168 | WHERE |
||
169 | `type` = 'table' AND |
||
170 | `name` != 'sqlite_sequence' AND |
||
171 | 1 | `name` LIKE $like |
|
172 | ORDER BY `name` ASC" |
||
173 | 1 | ) ?: []; |
|
174 | } else { |
||
175 | 1 | return $this->qfas( |
|
176 | 1 | "SELECT `name` |
|
177 | FROM `sqlite_master` |
||
178 | WHERE |
||
179 | `type` = 'table' AND |
||
180 | `name` != 'sqlite_sequence' |
||
181 | ORDER BY `name` ASC" |
||
182 | 1 | ) ?: []; |
|
183 | } |
||
184 | } |
||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 29 | protected function s_internal ($string, $single_quotes_around) { |
|
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 1 | public function server () { |
|
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 1 | public function __destruct () { |
|
207 | } |
||
208 |