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 | * Shows debug information for the SQL query without execution. |
||
| 104 | */ |
||
| 105 | public function debug() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Checks query parameters types correspondence. |
||
| 111 | * |
||
| 112 | * @param array $params Parameters of the query. |
||
| 113 | * @param string $types Types of the parameters ("idsb"). |
||
| 114 | * |
||
| 115 | * @throws DBCoreException |
||
| 116 | */ |
||
| 117 | private static function checkParameterTypes($params, $types) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return qwestion marks string for IN(...) SQL construction. |
||
| 176 | * |
||
| 177 | * @param int $length Length of the result string. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public static function sqlQMString($length) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return fields and qwestion marks string for SET field1=?, ... SQL construction. |
||
| 187 | * |
||
| 188 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 189 | * @param string $idFieldName Name of the primary key field. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public static function sqlQMValuesString($fieldsList, $idFieldName = "") { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return fields and values string for SET field1=value1, ... SQL construction. |
||
| 206 | * |
||
| 207 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 208 | * @param string $idFieldName Name of the primary key field. |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public static function sqlValuesString($fieldsList, $idFieldName) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns SQL types string. |
||
| 225 | * Type specification chars: |
||
| 226 | * i - corresponding variable has type integer |
||
| 227 | * d - corresponding variable has type double |
||
| 228 | * s - corresponding variable has type string |
||
| 229 | * b - corresponding variable is a blob and will be sent in packets. |
||
| 230 | * |
||
| 231 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 232 | * @param string $idFieldName Name of the primary key field. |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public static function sqlTypesString($fieldsList, $idFieldName = "") { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns SQL types string of single type. |
||
| 254 | * |
||
| 255 | * @param string $type SQL type. |
||
| 256 | * @param int $length Length of the SQL types string. |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | * @throws DBFieldTypeException If invalid type passed. |
||
| 260 | */ |
||
| 261 | public static function sqlSingleTypeString($type, $length) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Push values to the DBPreparedQuery SQL query field end. |
||
| 274 | * |
||
| 275 | * @param array $values List of pairs key => value or SQL query parts with |
||
| 276 | * parameters. |
||
| 277 | * @param string $separator Join separator. |
||
| 278 | */ |
||
| 279 | public function sqlPushValues($values, $separator = ", ") { |
||
| 303 | |||
| 304 | } |
||
| 305 |
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: