Complex classes like PostgreSQL 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 PostgreSQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class PostgreSQL extends _Abstract { |
||
10 | /** |
||
11 | * @var resource DB connection handler |
||
12 | */ |
||
13 | protected $handler; |
||
14 | /** |
||
15 | * @var resource |
||
16 | */ |
||
17 | protected $query_result; |
||
18 | /** |
||
19 | * @inheritdoc |
||
20 | */ |
||
21 | public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
||
38 | /** |
||
39 | * Parse host string into host, port and persistent separately |
||
40 | * |
||
41 | * Understands `p:` prefix for persistent connections |
||
42 | * |
||
43 | * @param string $host_string |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function get_host_port_and_persistent ($host_string) { |
||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public function q ($query, ...$params) { |
||
114 | /** |
||
115 | * @inheritdoc |
||
116 | * |
||
117 | * @return false|resource |
||
118 | */ |
||
119 | protected function q_internal ($query, $parameters = []) { |
||
129 | /** |
||
130 | * @param string|string[] $query |
||
131 | * |
||
132 | * @return string|string[] |
||
1 ignored issue
–
show
|
|||
133 | */ |
||
134 | protected function convert_prepared_statements_syntax ($query) { |
||
145 | /** |
||
146 | * @inheritdoc |
||
147 | * |
||
148 | * @param false|resource $query_result |
||
149 | */ |
||
150 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | public function id () { |
||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | public function affected () { |
||
178 | /** |
||
179 | * @inheritdoc |
||
180 | * |
||
181 | * @param false|resource $query_result |
||
182 | */ |
||
183 | public function free ($query_result) { |
||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | public function columns ($table, $like = false) { |
||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | public function tables ($like = false) { |
||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | protected function s_internal ($string, $single_quotes_around) { |
||
243 | /** |
||
244 | * @inheritdoc |
||
245 | */ |
||
246 | public function server () { |
||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | public function __destruct () { |
||
258 | } |
||
259 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.