Complex classes like SQLServerConnector 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 SQLServerConnector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SQLServerConnector extends DBConnector |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Connection to the DBMS. |
||
21 | * |
||
22 | * @var resource |
||
23 | */ |
||
24 | protected $dbConn = null; |
||
25 | |||
26 | /** |
||
27 | * Stores the affected rows of the last query. |
||
28 | * Used by sqlsrv functions only, as sqlsrv_rows_affected |
||
29 | * accepts a result instead of a database handle. |
||
30 | * |
||
31 | * @var integer |
||
32 | */ |
||
33 | protected $lastAffectedRows; |
||
34 | |||
35 | /** |
||
36 | * Name of the currently selected database |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $selectedDatabase = null; |
||
41 | |||
42 | public function connect($parameters, $selectDB = false) |
||
82 | |||
83 | /** |
||
84 | * Start transaction. READ ONLY not supported. |
||
85 | */ |
||
86 | public function transactionStart() |
||
94 | |||
95 | /** |
||
96 | * Commit everything inside this transaction so far |
||
97 | */ |
||
98 | public function transactionEnd() |
||
106 | |||
107 | /** |
||
108 | * Rollback or revert to a savepoint if your queries encounter problems |
||
109 | * If you encounter a problem at any point during a transaction, you may |
||
110 | * need to rollback that particular query, or return to a savepoint |
||
111 | */ |
||
112 | public function transactionRollback() |
||
119 | |||
120 | public function affectedRows() |
||
124 | |||
125 | public function getLastError() |
||
138 | |||
139 | public function isActive() |
||
143 | |||
144 | public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) |
||
188 | |||
189 | public function query($sql, $errorLevel = E_USER_ERROR) |
||
193 | |||
194 | public function selectDatabase($name) |
||
200 | |||
201 | public function __destruct() |
||
207 | |||
208 | public function getVersion() |
||
212 | |||
213 | public function getGeneratedID($table) |
||
217 | |||
218 | public function getSelectedDatabase() |
||
222 | |||
223 | public function unloadDatabase() |
||
228 | |||
229 | /** |
||
230 | * Quotes a string, including the "N" prefix so unicode |
||
231 | * strings are saved to the database correctly. |
||
232 | * |
||
233 | * @param string $value String to be encoded |
||
234 | * @return string Processed string ready for DB |
||
235 | */ |
||
236 | public function quoteString($value) |
||
240 | |||
241 | public function escapeString($value) |
||
247 | } |
||
248 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.