| Total Complexity | 64 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TableGateway 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.
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 TableGateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class TableGateway extends AbstractTableGateway implements TableGatewayInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Select statement |
||
| 26 | * |
||
| 27 | * @param array $where |
||
| 28 | * |
||
| 29 | * @return array With all results |
||
| 30 | */ |
||
| 31 | public function select(Array $where = []) |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Insert statement |
||
| 111 | * |
||
| 112 | * @param array $data |
||
| 113 | * |
||
| 114 | * @throws RuntimeException from internal execute() |
||
| 115 | * @throws LogicException |
||
| 116 | * |
||
| 117 | * @return resource|object |
||
| 118 | */ |
||
| 119 | public function insert(Array $data) |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Update statement |
||
| 185 | * |
||
| 186 | * @param array $set |
||
| 187 | * @param array $where |
||
| 188 | * |
||
| 189 | * @throws RuntimeException from internal execute() |
||
| 190 | * @throws LogicException |
||
| 191 | * @throws SecurityException |
||
| 192 | * |
||
| 193 | * @return resource|object |
||
| 194 | */ |
||
| 195 | public function update(Array $set, Array $where) |
||
| 196 | { |
||
| 197 | $parsed_set = []; |
||
| 198 | |||
| 199 | if (!count($set)) |
||
| 200 | throw new \LogicException("You cannot update rows without SET clause"); |
||
| 201 | |||
| 202 | if (!count($where)) |
||
| 203 | throw new SecurityException("You cannot update rows without WHERE clause!"); |
||
|
|
|||
| 204 | |||
| 205 | $bind_values = []; |
||
| 206 | |||
| 207 | $driver = $this->getDb()->getDriverName(); |
||
| 208 | |||
| 209 | $k = 0; |
||
| 210 | |||
| 211 | foreach ($set as $key => $value) |
||
| 212 | { |
||
| 213 | $k++; |
||
| 214 | |||
| 215 | if (is_null($value)) |
||
| 216 | $parsed_set[] = "$key = NULL"; |
||
| 217 | elseif ($value instanceof SQLFunction) |
||
| 218 | $parsed_set[] = "$key = " . $value->getStatement(); |
||
| 219 | elseif (is_array($value)) |
||
| 220 | { |
||
| 221 | $parsed_in = []; |
||
| 222 | |||
| 223 | foreach ($value as $in_value) |
||
| 224 | { |
||
| 225 | switch ($driver) |
||
| 226 | { |
||
| 227 | case 'Oci8': |
||
| 228 | |||
| 229 | # [POSSIBLE BUG] - To Future revision (What about non-string values ?) |
||
| 230 | if (is_string($in_value)) |
||
| 231 | $parsed_in[] = ":$k"; |
||
| 232 | |||
| 233 | $bind_values[":$k"] = $in_value; |
||
| 234 | break; |
||
| 235 | |||
| 236 | case 'Mysqli' || 'Sqlsrv': |
||
| 237 | $parsed_in[] = "?"; |
||
| 238 | $bind_values[] = $in_value; |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | |||
| 242 | $k++; |
||
| 243 | } |
||
| 244 | |||
| 245 | $parsed_set[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 246 | } |
||
| 247 | else |
||
| 248 | { |
||
| 249 | switch ($driver) |
||
| 250 | { |
||
| 251 | case 'Oci8': |
||
| 252 | $parsed_set[] = "$key = :$k"; |
||
| 253 | $bind_values[":$k"] = $value; |
||
| 254 | break; |
||
| 255 | |||
| 256 | case 'Mysqli' || 'Sqlsrv': |
||
| 257 | $parsed_set[] = "$key = ?"; |
||
| 258 | $bind_values[] = $value; |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $parsed_set = implode(",\r\n\t", $parsed_set); |
||
| 265 | |||
| 266 | $parsed_where = []; |
||
| 267 | |||
| 268 | foreach ($where as $key => $value) |
||
| 269 | { |
||
| 270 | $k++; |
||
| 271 | |||
| 272 | if (is_null($value)) |
||
| 273 | $parsed_where[] = "$key IS NULL"; |
||
| 274 | elseif ($value instanceof SQLFunction) |
||
| 275 | $parsed_where[] = "$key = " . $value->getStatement(); |
||
| 276 | elseif (is_array($value)) |
||
| 277 | { |
||
| 278 | $parsed_in = []; |
||
| 279 | |||
| 280 | foreach ($value as $in_value) |
||
| 281 | { |
||
| 282 | switch ($driver) |
||
| 283 | { |
||
| 284 | case 'Oci8': |
||
| 285 | $parsed_in[] = ":$k"; |
||
| 286 | $bind_values[":$k"] = $in_value; |
||
| 287 | break; |
||
| 288 | |||
| 289 | case 'Mysqli' || 'Sqlsrv': |
||
| 290 | $parsed_in[] = "?"; |
||
| 291 | $bind_values[] = $in_value; |
||
| 292 | break; |
||
| 293 | } |
||
| 294 | |||
| 295 | $k++; |
||
| 296 | } |
||
| 297 | |||
| 298 | $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 299 | } |
||
| 300 | else |
||
| 301 | { |
||
| 302 | switch ($driver) |
||
| 303 | { |
||
| 304 | case 'Oci8': |
||
| 305 | $parsed_where[] = "$key = :$k"; |
||
| 306 | $bind_values[":$k"] = $value; |
||
| 307 | break; |
||
| 308 | |||
| 309 | case 'Mysqli' || 'Sqlsrv': |
||
| 310 | $parsed_where[] = "$key = ?"; |
||
| 311 | $bind_values[] = $value; |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | $parsed_where = implode(" AND\r\n\t", $parsed_where); |
||
| 318 | |||
| 319 | $table = $this->entity->getTableName(); |
||
| 320 | |||
| 321 | $sql = "UPDATE {$table} \r\nSET \r\n\t$parsed_set \r\nWHERE \r\n\t$parsed_where"; |
||
| 322 | |||
| 323 | return $this->getDb()->execute($sql, $bind_values); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Delete statement |
||
| 328 | * |
||
| 329 | * @param array $where |
||
| 330 | * |
||
| 331 | * @throws RuntimeException from internal execute() |
||
| 332 | * @throws SecurityException |
||
| 333 | * |
||
| 334 | * @return resource|object |
||
| 335 | */ |
||
| 336 | public function delete(Array $where) |
||
| 407 | } |
||
| 408 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths