Complex classes like DBPreparedQuery 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 DBPreparedQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DBPreparedQuery extends DBQuery { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * DB query template. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $query = ""; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Parameters SQL types string ("idsb"). |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | public $types = ""; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * List of the DB SQL query parameters. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public $params = []; |
||
| 39 | |||
| 40 | |||
| 41 | /* Service variables */ |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Creates and initialize DBPreparedQuery object. |
||
| 45 | * |
||
| 46 | * @param string $query DB SQL query template. |
||
| 47 | * @param string $types Parameters SQL types string ("idsb"). |
||
| 48 | * @param array $params List of the DB SQL query parameters. |
||
| 49 | */ |
||
| 50 | public function __construct($query = "", $types = "", $params = []) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Verify if current DBPreparedQuery is have parameters for binding. |
||
| 63 | * |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | public function isBindable() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Verify if current DBPreparedQuery is valid for the execution. |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function isValid() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Executes SQL query. |
||
| 83 | * |
||
| 84 | * @param bool $debug Debug mode flag. |
||
| 85 | * |
||
| 86 | * @return mixed Statement object or FALSE if an error occurred if SELECT |
||
| 87 | * query executed or number of affected rows on success if other |
||
| 88 | * type of query executed. |
||
| 89 | */ |
||
| 90 | public function go($debug = false) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Checks query parameters types correspondence. |
||
| 104 | * |
||
| 105 | * @param array $params Parameters of the query. |
||
| 106 | * @param string $types Types of the parameters ("idsb"). |
||
| 107 | * |
||
| 108 | * @throws DBCoreException |
||
| 109 | */ |
||
| 110 | private static function checkParameterTypes($params, $types) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return qwestion marks string for IN(...) SQL construction. |
||
| 169 | * |
||
| 170 | * @param int $length Length of the result string. |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public static function sqlQMString($length) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return fields and qwestion marks string for SET field1=?, ... SQL construction. |
||
| 180 | * |
||
| 181 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 182 | * @param string $idFieldName Name of the primary key field. |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public static function sqlQMValuesString($fieldsList, $idFieldName = "") { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Return fields and values string for SET field1=value1, ... SQL construction. |
||
| 199 | * |
||
| 200 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 201 | * @param string $idFieldName Name of the primary key field. |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function sqlValuesString($fieldsList, $idFieldName) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns SQL types string. |
||
| 218 | * Type specification chars: |
||
| 219 | * i - corresponding variable has type integer |
||
| 220 | * d - corresponding variable has type double |
||
| 221 | * s - corresponding variable has type string |
||
| 222 | * b - corresponding variable is a blob and will be sent in packets. |
||
| 223 | * |
||
| 224 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 225 | * @param string $idFieldName Name of the primary key field. |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public static function sqlTypesString($fieldsList, $idFieldName = "") { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns SQL types string of single type. |
||
| 247 | * |
||
| 248 | * @param string $type SQL type. |
||
| 249 | * @param int $length Length of the SQL types string. |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | * @throws DBFieldTypeException If invalid type passed. |
||
| 253 | */ |
||
| 254 | public static function sqlSingleTypeString($type, $length) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Push values to the DBPreparedQuery SQL query field end. |
||
| 267 | * |
||
| 268 | * @param array $values List of pairs key => value or SQL query parts with |
||
| 269 | * parameters. |
||
| 270 | * @param string $separator Join separator. |
||
| 271 | */ |
||
| 272 | public function sqlPushValues($values, $separator = ", ") { |
||
| 296 | |||
| 297 | } |
||
| 298 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: